Repository navigation
Conversation
overwrite creation functions to inject correct values
- Create the usage view's assignment through baker, not questions.add() - Fix the comment on why the dropout test bypasses save()
|
Question is, if we want an explicit test for the behaviour of this custom baker class, or if we say that its tested with the all the test cases, that rely on it working. If it (for some reason) breaks, a bunch of tests would fail and we wouldn't directly know, why. |
If all the tests work, that's good enough for me |
richardebeling
left a comment
There was a problem hiding this comment.
Lgtm 👍. The EvapBaker is a bit hacky, but it's very encapsulated hackiness limited to 30 lines of test-only code, so I think that's fair.
| def instance(self, attrs: dict[str, Any], *args: Any, **kwargs: Any) -> Any: | ||
| if self.model is Question: | ||
| if "allows_additional_textanswers" not in self.explicit_attrs: | ||
| attrs["allows_additional_textanswers"] = attrs["type"] not in [QuestionType.TEXT, QuestionType.HEADING] |
There was a problem hiding this comment.
Why not attrs["type"] in self.RATING_QUESTION_TYPES ? (Same for 132 below)
There was a problem hiding this comment.
Also, I'm wondering: This makes it deterministicically always true or always false -- for tests that don't care about the value, should we maybe also just draw it randomly from the set of allowed values? So for (text, heading), always false, but for all other types, may be true or false?
counts_for_grade, allows_additional_textanswers, and question type automatically
Fixes #2761
As far as I understand baker works like this:
%%{init: {"theme": "neutral"}}%% flowchart TD start(["baker.make(Model, **attrs)"]) --> loop subgraph make["_make()"] loop{{"for each field of Model"}} loop --> passed{"value passed<br/>by the test?"} passed -- yes --> use["use it"] passed -- no --> optional{"default, blank=True<br/>or null=True?"} optional -- yes --> skip["skip it,<br/>Django fills it in later"]:::problem optional -- no --> fk{"ForeignKey?"} fk -- yes --> child["baker.make(RelatedModel)<br/>child is created and saved first"] fk -- no --> random["generate a random value<br/>(random choice if the field has choices)"]:::problem use & skip & child & random --> loop end child -. recursion .-> start loop -- all fields done --> build subgraph inst["instance()"] build["Model(**attrs)<br/>skipped fields: default, else None / empty string"] build --> save["save()<br/>the model's own checks run"] end save --> done(["finished object"]) style make fill:#fafafa,stroke:#d4d4d8 style inst fill:#fafafa,stroke:#d4d4d8 classDef problem fill:#ffffff,stroke:#3f3f46,stroke-width:2px,stroke-dasharray:4 3So in
instance()we can influence an object (or change its values), after all its child objects have been created. At this point we do not know, what attributes have been passed by test and which were filled in.With
_make()we have a entry point, right before the child objects are being created (and the object itself), so here we can influence how these are created (by setting or changing values).