From 3b6db89747fd7c53aac6c22b17cdc72a0a53ae98 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 3 Aug 2023 22:23:04 +0300 Subject: [PATCH 1/3] Add store factories and tests --- store/factories.py | 77 ++++++++++++++++++++++++++++++++++++++++++++-- store/tests.py | 58 +++++++++++++++++++++++++++++++++- 2 files changed, 132 insertions(+), 3 deletions(-) diff --git a/store/factories.py b/store/factories.py index 076ab36b1..ffd5e66dc 100644 --- a/store/factories.py +++ b/store/factories.py @@ -1,7 +1,62 @@ +from typing import Any import factory from factory.django import DjangoModelFactory from wagtail.rich_text import RichText -from .models import Product + +from home.factories import HomePageFactory +from home.models import HomePage +from .models import Product, StoreIndexPage, ProductIndexPage + + +class StoreIndexPageFactory(DjangoModelFactory): + class Meta: + model = StoreIndexPage + + title = factory.Sequence(lambda n: f"Store Index Page {n}") + intro = RichText("Store Index Page intro") + + @classmethod + def _create( + cls, + model_class: type[StoreIndexPage], + *args: Any, + **kwargs: Any, + ) -> StoreIndexPage: + instance = model_class(*args, **kwargs) # type: ignore + + # Get the HomePage instance if it exists, otherwise create one. + home_page = HomePage.objects.first() + if home_page is None: + home_page = HomePageFactory() + + home_page.add_child(instance=instance) + + return instance + + +class ProductIndexPageFactory(DjangoModelFactory): + class Meta: + model = ProductIndexPage + + title = factory.Sequence(lambda n: f"Product Index Page {n}") + + @classmethod + def _create( + cls, + model_class: type[ProductIndexPage], + *args: Any, + **kwargs: Any, + ) -> ProductIndexPage: + instance = model_class(*args, **kwargs) # type: ignore + + # Get the StoreIndexPage instance if it exists, otherwise create one. + store_index_page = StoreIndexPage.objects.first() + if store_index_page is None: + store_index_page = StoreIndexPageFactory() + + store_index_page.add_child(instance=instance) + + return instance class ProductFactory(DjangoModelFactory): @@ -16,8 +71,26 @@ class Meta: right_digits=2, positive=True, ) - available = factory.Iterator([True, False]) + available = factory.Iterator([True, False]) # type: ignore # TODO: add a MockWagtailImage class # and use it here # image = factory.LazyAttribute(lambda _: get_test_image_file()) + + @classmethod + def _create( + cls, + model_class: type[Product], + *args: Any, + **kwargs: Any, + ) -> Product: + instance = model_class(*args, **kwargs) + + # Get the ProductIndexPage instance if it exists, otherwise create one. + product_index_page = ProductIndexPage.objects.first() + if product_index_page is None: + product_index_page = ProductIndexPageFactory() + + product_index_page.add_child(instance=instance) + + return instance diff --git a/store/tests.py b/store/tests.py index a39b155ac..21e667093 100644 --- a/store/tests.py +++ b/store/tests.py @@ -1 +1,57 @@ -# Create your tests here. +from django.test import TestCase + +from home.models import HomePage +from store.models import Product, ProductIndexPage, StoreIndexPage +from .factories import ( + StoreIndexPageFactory, + ProductIndexPageFactory, + ProductFactory, +) + + +class TestStoreIndexPageFactory(TestCase): + def test_store_index_page_factory(self) -> None: + """Test that a Topic can be created.""" + store_index_page = StoreIndexPageFactory.create() + + self.assertIsInstance( + store_index_page, + StoreIndexPage, + ) + + self.assertIsInstance( + store_index_page.get_parent().specific, + HomePage, + ) + + +class TestProductIndexPageFactory(TestCase): + def test_product_index_page_factory(self) -> None: + """Test that a Topic can be created.""" + product_index_page = ProductIndexPageFactory.create() + + self.assertIsInstance( + product_index_page, + ProductIndexPage, + ) + + self.assertIsInstance( + product_index_page.get_parent().specific, + StoreIndexPage, + ) + + +class TestProductFactory(TestCase): + def test_product_factory(self) -> None: + """Test that a Topic can be created.""" + product = ProductFactory.create() + + self.assertIsInstance( + product, + Product, + ) + + self.assertIsInstance( + product.get_parent().specific, + ProductIndexPage, + ) From 9d616b9a8c95e5b18b269b838df5515702c4465b Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 3 Aug 2023 22:25:25 +0300 Subject: [PATCH 2/3] Add TestStoreIndexPageGetContext --- store/models.py | 2 +- store/tests.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/store/models.py b/store/models.py index d89fdbcc5..c8ebdae86 100644 --- a/store/models.py +++ b/store/models.py @@ -32,7 +32,7 @@ def get_context( context = super().get_context(request) context["products"] = Product.objects.all() - context["cart_add_product_form"] = CartAddProductForm + context["cart_add_product_form"] = CartAddProductForm() return context diff --git a/store/tests.py b/store/tests.py index 21e667093..9a3fc3e2b 100644 --- a/store/tests.py +++ b/store/tests.py @@ -2,6 +2,7 @@ from home.models import HomePage from store.models import Product, ProductIndexPage, StoreIndexPage +from cart.forms import CartAddProductForm from .factories import ( StoreIndexPageFactory, ProductIndexPageFactory, @@ -55,3 +56,24 @@ def test_product_factory(self) -> None: product.get_parent().specific, ProductIndexPage, ) + + +class TestStoreIndexPageGetContext(TestCase): + def test_store_index_page_get_context(self) -> None: + """Test that a Topic can be created.""" + store_index_page = StoreIndexPageFactory.create() + context = store_index_page.get_context(request=None) + + self.assertIn( + "products", + context, + ) + + self.assertIn( + "cart_add_product_form", + context, + ) + self.assertIsInstance( + context["cart_add_product_form"], + CartAddProductForm, + ) From d23e3df3d0f18421189918fad497b2edd0ff086c Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Thu, 3 Aug 2023 22:26:59 +0300 Subject: [PATCH 3/3] Add TestProductGetContext --- store/models.py | 2 +- store/tests.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/store/models.py b/store/models.py index c8ebdae86..7e93efa3e 100644 --- a/store/models.py +++ b/store/models.py @@ -77,7 +77,7 @@ def get_context( ) -> dict: context = super().get_context(request) - context["cart_add_product_form"] = CartAddProductForm + context["cart_add_product_form"] = CartAddProductForm() return context diff --git a/store/tests.py b/store/tests.py index 9a3fc3e2b..9d0b61e0e 100644 --- a/store/tests.py +++ b/store/tests.py @@ -77,3 +77,19 @@ def test_store_index_page_get_context(self) -> None: context["cart_add_product_form"], CartAddProductForm, ) + + +class TestProductGetContext(TestCase): + def test_product_get_context(self) -> None: + """Test that a Topic can be created.""" + product = ProductFactory.create() + context = product.get_context(request=None) + + self.assertIn( + "cart_add_product_form", + context, + ) + self.assertIsInstance( + context["cart_add_product_form"], + CartAddProductForm, + )