diff --git a/manuels/tests/baker_recipes.py b/manuels/tests/baker_recipes.py new file mode 100644 index 0000000..efce0db --- /dev/null +++ b/manuels/tests/baker_recipes.py @@ -0,0 +1,23 @@ +import random + +from model_bakery import seq +from model_bakery.recipe import Recipe + + +def random_bool(): + return random.choice([True, False]) + + +book = Recipe( + "Book", + title=seq("book title "), + field=seq("field "), + authors=seq("author "), + previously_acquired=random_bool, + consumable=random_bool, +) + +supply = Recipe( + "SuppliesRequirement", + field=seq("field "), +) diff --git a/manuels/tests/selenium.py b/manuels/tests/selenium.py index e77a522..da40080 100644 --- a/manuels/tests/selenium.py +++ b/manuels/tests/selenium.py @@ -42,11 +42,7 @@ class SeleniumTestCase(LiveServerTestCase): return f"{self.live_server_url}{url}" -class TeacherSeleniumTestCase(SeleniumTestCase): - @classmethod - def setUpClass(cls): - super().setUpClass() - +class TeacherTestMixin: def setUp(self) -> None: super().setUp() self.teacher = Teacher.objects.create( @@ -56,6 +52,6 @@ class TeacherSeleniumTestCase(SeleniumTestCase): email="teacher@example.com", ) - def go_to_teacher_url(self): - url = self.teacher.get_absolute_url() - self.get(url) + +class TeacherSeleniumTestCase(TeacherTestMixin, SeleniumTestCase): + pass diff --git a/manuels/tests/test_inscription.py b/manuels/tests/test_inscription.py index fdb47ec..2575366 100644 --- a/manuels/tests/test_inscription.py +++ b/manuels/tests/test_inscription.py @@ -6,8 +6,6 @@ from manuels.tests.selenium import SeleniumTestCase class InscriptionTestCase(SeleniumTestCase): - headless = False - def setUp(self) -> None: super().setUp() url = reverse("home_page") diff --git a/manuels/tests/test_teacher_base_view.py b/manuels/tests/test_teacher_base_view.py index 16d74f7..e644481 100644 --- a/manuels/tests/test_teacher_base_view.py +++ b/manuels/tests/test_teacher_base_view.py @@ -1,38 +1,47 @@ from django.test import TestCase +from model_bakery import baker -from manuels.tests.selenium import TeacherSeleniumTestCase +from manuels.tests.selenium import TeacherSeleniumTestCase, TeacherTestMixin -class TeacherBaseViewTestCase(TestCase): +class TeacherBaseViewTestCase(TeacherTestMixin, TestCase): def test_view_is_rendered(self): - # Go to url - # check response is 200 - assert False + url = self.teacher.get_absolute_url() + res = self.client.get(url) + assert res.status_code == 200 - def test_view_contains_books(self): - # Create books linked to teacher - # go to url - # check response is 200 - # check books are in context - assert False - - def test_view_contains_supplies(self): - # create supplies requirements linked to teacher - # go to url - # check response is 200 - # check supplies are in context - assert False + def view_is_passed_teacher(self): + baker.make_recipe("manuels.tests.book", _quantity=5, teacher=self.teacher) + baker.make_recipe("manuels.tests.book", _quantity=5) + baker.make_recipe("manuels.tests.supply", _quantity=5, teacher=self.teacher) + baker.make_recipe("manuels.tests.supply", _quantity=5) + url = self.teacher.get_absolute_url() + res = self.client.get(url) + assert res.status_code == 200 + assert res.context["teacher"] == self.teacher class TeacherBaseViewSeleniumTestCase(TeacherSeleniumTestCase): def test_books_are_displayed(self): - # Create books linked to teacher - # go to url - # check book title is displayed - assert False + teacher_books = baker.make_recipe( + "manuels.tests.book", _quantity=5, teacher=self.teacher + ) + other_books = baker.make_recipe("manuels.tests.book", _quantity=5) + url = self.teacher.get_absolute_url() + self.get(url) + for book in teacher_books: + assert book.title in self.selenium.page_source + for book in other_books: + assert book.title not in self.selenium.page_source def test_supplies_are_displayed(self): - # Create books linked to teacher - # go to url - # check supplies name is displayed - assert False + teacher_supplies = baker.make_recipe( + "manuels.tests.supply", _quantity=5, teacher=self.teacher + ) + other_supplies = baker.make_recipe("manuels.tests.supply", _quantity=5) + url = self.teacher.get_absolute_url() + self.get(url) + for supply in teacher_supplies: + assert supply.supplies in self.selenium.page_source + for supply in other_supplies: + assert supply.supplies not in self.selenium.page_source