Skip to content

Baker: Fill counts_for_grade, allows_additional_textanswers, and question type automatically - #2833

Open
jooooosef wants to merge 8 commits into
e-valuation:mainfrom
jooooosef:2761-valid-baker-question-generation
Open

jooooosef wants to merge 8 commits into
e-valuation:mainfrom
jooooosef:2761-valid-baker-question-generation

Conversation

@jooooosef

@jooooosef jooooosef commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

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 3
Loading

So 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).

@jooooosef
jooooosef requested a review from janno42 November 23, 2026 19:41
@jooooosef

Copy link
Copy Markdown
Collaborator Author

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.

@richardebeling

Copy link
Copy Markdown
Member

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 richardebeling left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not attrs["type"] in self.RATING_QUESTION_TYPES ? (Same for 132 below)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@richardebeling richardebeling changed the title valid baker question generation Baker: Fill counts_for_grade, allows_additional_textanswers, and question type automatically Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Make baker produce valid QuestionAssignment/Question defaults

2 participants