Skip to content

Commit

Permalink
Add another factory to allow us to fix a consultation/theme and test
Browse files Browse the repository at this point in the history
  • Loading branch information
nmenezes0 committed Mar 15, 2024
1 parent f58dd45 commit 91deda6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
48 changes: 35 additions & 13 deletions tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pytest
import factory
import yaml
import random
from consultation_analyser.consultations import models

# import datetime
# from factory.fuzzy import FuzzyDate
import faker as _faker

faker = _faker.Faker()
Expand Down Expand Up @@ -32,17 +31,15 @@ def all_questions(self):
return list(self.questions.values())


class QuestionFactory(factory.django.DjangoModelFactory):
class ConsultationFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Question
model = models.Consultation

text = factory.LazyAttribute(lambda o: o.question["text"])
slug = factory.LazyAttribute(lambda o: o.question["slug"])
multiple_choice_options = factory.LazyAttribute(lambda o: o.question["multiple_choice_options"])
has_free_text = factory.LazyAttribute(lambda o: o.question["has_free_text"])
name = faker.sentence()
slug = faker.slug()

class Params:
question = FakeConsultationData().question()
fixed_slug = factory.Trait(slug="consultation-slug")


class SectionFactory(factory.django.DjangoModelFactory):
Expand All @@ -52,13 +49,29 @@ class Meta:
name = faker.sentence()
slug = faker.slug()

class Params:
fixed_slug = factory.Trait(slug="section-slug", consultation=factory.SubFactory(ConsultationFactory,fixed_slug=True))

class ConsultationFactory(factory.django.DjangoModelFactory):

class QuestionFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Consultation
model = models.Question

name = faker.sentence()
slug = faker.slug()
text = factory.LazyAttribute(lambda o: o.question["text"])
slug = factory.LazyAttribute(lambda o: o.question["slug"])
multiple_choice_options = factory.LazyAttribute(lambda o: o.question["multiple_choice_options"])
has_free_text = factory.LazyAttribute(lambda o: o.question["has_free_text"])

class Params:
question = FakeConsultationData().question()

fixed_slug = factory.Trait(
section=factory.SubFactory(SectionFactory, fixed_slug=True),
slug="question-slug",
text="Is this an interesting question?",
multiple_choice_options=[],
has_free_text=False,
)


class ConsultationResponseFactory(factory.django.DjangoModelFactory):
Expand All @@ -70,6 +83,7 @@ class ThemeFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Theme

# TODO - may need to be changed once ML pipeline is in
label = faker.sentence()
summary = f"Summary: {label}"
keywords = label.lower().strip(".").split(" ")
Expand All @@ -90,3 +104,11 @@ class Meta:
)

question = factory.SubFactory(QuestionFactory)
consultation_response = factory.SubFactory(ConsultationResponseFactory)

class Params:
specific_theme = factory.Trait(
consultation_response=factory.SubFactory(ConsultationResponseFactory),
question=factory.SubFactory(QuestionFactory, fixed_slug=True),
theme=factory.SubFactory(ThemeFactory, summary="Summary theme 1", keywords=["summary", "one"]),
)
22 changes: 4 additions & 18 deletions tests/integration/test_question_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,15 @@
from tests import factories


def generate_question_with_themes():
consultation = factories.ConsultationFactory(slug="c-slug")
consultation_response = factories.ConsultationResponseFactory()
section = factories.SectionFactory(consultation=consultation, slug="s-slug")
question = factories.QuestionFactory(
section=section,
slug="question-slug",
text="Is this an interesting question?",
multiple_choice_options=[],
has_free_text=False,
)
theme_1 = factories.ThemeFactory(summary="Summary theme 1", keywords=["summary", "one"])
theme_2 = factories.ThemeFactory(summary="Summary theme 2", keywords=["summary", "two"])
factories.AnswerFactory(question=question, theme=theme_1, consultation_response=consultation_response)
factories.AnswerFactory(question=question, theme=theme_2, consultation_response=consultation_response)


@pytest.mark.django_db
def test_get_question_summary_page(client):
generate_question_with_themes()
question_summary_url = "/consultation/c-slug/section/s-slug/question/question-slug/"

factories.AnswerFactory(specific_theme=True)
question_summary_url = "/consultation/consultation-slug/section/section-slug/question/question-slug/"
response = client.get(question_summary_url)
page_content = str(response.content)
assert "Is this an interesting question?" in page_content
assert "Summary theme 1" in page_content
assert "Summary theme 2" in page_content
# assert "Summary theme 2" in page_content

0 comments on commit 91deda6

Please sign in to comment.