Skip to content

Commit

Permalink
Merge pull request #843 from WesternFriend/store-tests
Browse files Browse the repository at this point in the history
Store tests
  • Loading branch information
brylie authored Aug 3, 2023
2 parents bc229ce + d23e3df commit 4b3672a
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 5 deletions.
77 changes: 75 additions & 2 deletions store/factories.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
4 changes: 2 additions & 2 deletions store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
96 changes: 95 additions & 1 deletion store/tests.py
Original file line number Diff line number Diff line change
@@ -1 +1,95 @@
# Create your tests here.
from django.test import TestCase

from home.models import HomePage
from store.models import Product, ProductIndexPage, StoreIndexPage
from cart.forms import CartAddProductForm
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,
)


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,
)


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,
)

0 comments on commit 4b3672a

Please sign in to comment.