Skip to content

Commit

Permalink
fix: don't use default on row_question if default table_answer exists
Browse files Browse the repository at this point in the history
  • Loading branch information
open-dynaMIX authored and anehx committed Nov 25, 2020
1 parent 24d2899 commit 30ce974
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
21 changes: 3 additions & 18 deletions caluma/caluma_form/domain_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,9 @@ def _set_default_answers_for_form(form, document):
question.sub_form, document
)
continue
new_answer = question.default_answer.copy(document_family=document.family)
if question.type == models.Question.TYPE_TABLE:
# In case of table questions, we have to evaluate what default to use.
# The default value in a row_document wins over the default_answer of a
# row_form question
for row_doc in new_answer.documents.iterator():
row_doc_answers = []
for sub_question in question.row_form.questions.filter(
default_answer__isnull=False
).iterator():
if not row_doc.answers.filter(question=sub_question).exists():
row_doc_answers.append(
sub_question.default_answer.copy(
document_family=document.family
)
)
row_doc.answers.add(*row_doc_answers)
answers.append(new_answer)
answers.append(
question.default_answer.copy(document_family=document.family)
)
document.answers.add(*answers)
return document

Expand Down
9 changes: 6 additions & 3 deletions caluma/caluma_form/tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,9 +999,9 @@ def test_save_document_table_answer_setting_family(
assert to_be_deleted_table_row.family == to_be_deleted_document.family


@pytest.mark.parametrize("default_on_table,value", [(True, 1979), (False, 23)])
@pytest.mark.parametrize("default_on_table", [True, False])
def test_save_document_table_answer_default_answer(
db, form_and_document, answer_factory, default_on_table, value
db, form_and_document, answer_factory, default_on_table
):
form, document, questions_dict, answers_dict = form_and_document(use_table=True)

Expand All @@ -1027,7 +1027,10 @@ def test_save_document_table_answer_default_answer(
assert Document.objects.filter(form=form).count() == 2

assert doc.answers.count() == 1
assert doc.answers.first().documents.first().answers.first().value == value
if default_on_table:
assert doc.answers.first().documents.first().answers.first().value == 1979
else:
assert doc.answers.first().documents.first().answers.first() is None


@pytest.mark.parametrize("answer__value", [1.1])
Expand Down

0 comments on commit 30ce974

Please sign in to comment.