From 04264414a83a14adc797fa6aadcff515ccd92e17 Mon Sep 17 00:00:00 2001 From: Christian Zosel Date: Tue, 31 Dec 2024 12:07:01 +0100 Subject: [PATCH] chore: add test for prefetching --- caluma/caluma_form/tests/test_question.py | 22 ++++++++++++++++++++++ caluma/conftest.py | 13 +++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/caluma/caluma_form/tests/test_question.py b/caluma/caluma_form/tests/test_question.py index 7595688ad..f13cbb6ed 100644 --- a/caluma/caluma_form/tests/test_question.py +++ b/caluma/caluma_form/tests/test_question.py @@ -1198,3 +1198,25 @@ def test_save_action_button_question(db, snapshot, question, schema_executor): result = schema_executor(query, variable_values=inp) assert not bool(result.errors) snapshot.assert_match(result.data) + + +def test_init_of_calc_questions_queries( + db, + form, + form_and_document, + form_question_factory, + django_assert_num_queries, +): + (form, document, questions_dict, _) = form_and_document( + use_table=True, use_subform=True, table_row_count=10 + ) + + form_question_factory( + form=form, + question__slug="calc_question", + question__type=Question.TYPE_CALCULATED_FLOAT, + question__calc_expression="'table'|answer|mapby('column')|sum + 'top_question'|answer + 'sub_question'|answer", + ) + + with django_assert_num_queries(38): + api.save_answer(questions_dict["top_question"], document, value="1") diff --git a/caluma/conftest.py b/caluma/conftest.py index 4485d84de..6f6c0c51f 100644 --- a/caluma/conftest.py +++ b/caluma/conftest.py @@ -272,7 +272,7 @@ def fallback_factory(factory, **kwargs): return existing return factory(**kwargs) - def factory(use_table=False, use_subform=False): + def factory(use_table=False, use_subform=False, table_row_count=1): form = fallback_factory( form_factory, slug="top_form", meta={"is-top-form": True, "level": 0} ) @@ -324,11 +324,12 @@ def factory(use_table=False, use_subform=False): document=document, question=questions["table"] ) - row_doc = document_factory(form=row_form, family=document) - answers["column"] = answer_factory( - document=row_doc, question=questions["column"] - ) - answers["table"].documents.add(row_doc) + for _ in range(table_row_count): + row_doc = document_factory(form=row_form, family=document) + answers["column"] = answer_factory( + document=row_doc, question=questions["column"] + ) + answers["table"].documents.add(row_doc) if use_subform: sub_form = fallback_factory(