Skip to content

Commit

Permalink
Merge pull request #35 from City-of-Turku/bugfix/check-condition-met-…
Browse files Browse the repository at this point in the history
…for-question-with-multiple-sub-question-conditions

Bugfix/check condition met for question with multiple sub question conditions
  • Loading branch information
juuso-j authored Mar 11, 2024
2 parents 57388af + 0c222b4 commit bf52b65
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 3 deletions.
8 changes: 5 additions & 3 deletions profiles/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def sub_question_condition_met(sub_question_condition, user):


def question_condition_met(question_condition_qs, user):
conditions_met = True
for question_condition in question_condition_qs:
if question_condition.sub_question_condition:
user_answers = Answer.objects.filter(
Expand All @@ -104,9 +105,10 @@ def question_condition_met(question_condition_qs, user):
option_conditions = question_condition.option_conditions.all().values_list(
"id", flat=True
)
if set(user_answers).intersection(set(option_conditions)):
return True
return False

if not set(user_answers).intersection(set(option_conditions)):
return False
return conditions_met


@transaction.atomic
Expand Down
26 changes: 26 additions & 0 deletions profiles/tests/api/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,32 @@ def test_get_questions_with_conditions(
assert json_data["results"][1]["number"] == "3"


@pytest.mark.django_db
def test_question_with_multiple_subquestion_conditions_is_false(
api_client, m_c_users, m_c_answers, m_c_questions, m_c_question_conditions
):
user = m_c_users.get(username="condition false")
token = Token.objects.create(user=user)
condition_url = reverse("profiles:question-check-if-question-condition-met")
api_client.credentials(HTTP_AUTHORIZATION="Token " + token.key)
condition_question = m_c_questions.get(number="44")
response = api_client.post(condition_url, {"question": condition_question.id})
assert response.json()["condition_met"] is False


@pytest.mark.django_db
def test_question_with_multiple_subquestion_conditions_is_true(
api_client, m_c_users, m_c_answers, m_c_questions, m_c_question_conditions
):
user = m_c_users.get(username="condition true")
token = Token.objects.create(user=user)
condition_url = reverse("profiles:question-check-if-question-condition-met")
api_client.credentials(HTTP_AUTHORIZATION="Token " + token.key)
condition_question = m_c_questions.get(number="44")
response = api_client.post(condition_url, {"question": condition_question.id})
assert response.json()["condition_met"] is True


@pytest.mark.django_db
def test_question_condition_is_met(
api_client, users, answers, questions, question_conditions
Expand Down
106 changes: 106 additions & 0 deletions profiles/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,112 @@ def options_with_multiple_results(questions_test_result, results_test_result):
return Option.objects.all()


@pytest.mark.django_db
@pytest.fixture
def m_c_questions():
Question.objects.create(question="Question with subquesitons", number="42")
Question.objects.create(
question="Dont ask me if multiple subquestion are answered no", number="44"
)

return Question.objects.all()


@pytest.mark.django_db
@pytest.fixture
def m_c_sub_questions(m_c_questions):
question = m_c_questions.get(number="42")
SubQuestion.objects.create(question=question, description="yes/no", order_number=0)
SubQuestion.objects.create(question=question, description="yes/no", order_number=1)
SubQuestion.objects.create(question=question, description="yes/no", order_number=2)
return SubQuestion.objects.all()


@pytest.mark.django_db
@pytest.fixture
def m_c_options(m_c_questions, m_c_sub_questions):
for sub_question in m_c_sub_questions:
Option.objects.create(value="no", sub_question=sub_question)
Option.objects.create(value="yes", sub_question=sub_question)
return Option.objects.all()


@pytest.mark.django_db
@pytest.fixture
def m_c_question_conditions(m_c_questions, m_c_sub_questions, m_c_options):
question = m_c_questions.get(number="44")
question_condition = m_c_questions.get(number="42")
sub_question_condition = m_c_sub_questions.get(order_number=1)
option_condition = m_c_options.get(sub_question=sub_question_condition, value="no")
condition = QuestionCondition.objects.create(
question=question,
question_condition=question_condition,
sub_question_condition=sub_question_condition,
)
condition.option_conditions.add(option_condition)
sub_question_condition = m_c_sub_questions.get(order_number=2)
option_condition = m_c_options.get(sub_question=sub_question_condition, value="no")
condition = QuestionCondition.objects.create(
question=question,
question_condition=question_condition,
sub_question_condition=sub_question_condition,
)
condition.option_conditions.add(option_condition)
return QuestionCondition.objects.all()


@pytest.mark.django_db
@pytest.fixture
def m_c_users():
User.objects.create(username="condition false")
User.objects.create(username="condition true")
return User.objects.all()


@pytest.mark.django_db
@pytest.fixture
def m_c_answers(
m_c_users, m_c_questions, m_c_sub_questions, m_c_options, m_c_question_conditions
):
user = m_c_users.get(username="condition false")
question = m_c_questions.get(number="42")
sub_question = m_c_sub_questions.get(order_number=0)
option = m_c_options.get(sub_question=sub_question, value="yes")
Answer.objects.create(
user=user, question=question, sub_question=sub_question, option=option
)
sub_question = m_c_sub_questions.get(order_number=1)
option = m_c_options.get(sub_question=sub_question, value="no")
Answer.objects.create(
user=user, question=question, sub_question=sub_question, option=option
)
sub_question = m_c_sub_questions.get(order_number=2)
option = m_c_options.get(sub_question=sub_question, value="yes")
Answer.objects.create(
user=user, question=question, sub_question=sub_question, option=option
)

user = m_c_users.get(username="condition true")
question = m_c_questions.get(number="42")
sub_question = m_c_sub_questions.get(order_number=0)
option = m_c_options.get(sub_question=sub_question, value="yes")
Answer.objects.create(
user=user, question=question, sub_question=sub_question, option=option
)
sub_question = m_c_sub_questions.get(order_number=1)
option = m_c_options.get(sub_question=sub_question, value="no")
Answer.objects.create(
user=user, question=question, sub_question=sub_question, option=option
)
sub_question = m_c_sub_questions.get(order_number=2)
option = m_c_options.get(sub_question=sub_question, value="no")
Answer.objects.create(
user=user, question=question, sub_question=sub_question, option=option
)

return Answer.objects.all()


@pytest.mark.django_db
@pytest.fixture
def questions():
Expand Down

0 comments on commit bf52b65

Please sign in to comment.