From 9985df6b9cf8eb0b77a62db366282b58134c1194 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Wed, 8 Jan 2025 20:31:31 +0000 Subject: Add tests for cart functionality and refactor related files - Implement test for verifying that the cart contains the correctly priced resource. - Update `ResourceModelFactory` to remove commented price-related code. - Modify HTML meta description for improved clarity. - Adjust `pyproject.toml` to format settings for clarity and organization. --- alphabetlearning/payments/tests/test_views.py | 35 +++++++++++++++++++++++++++ alphabetlearning/resources/factories.py | 2 -- alphabetlearning/templates/base.html | 2 +- pyproject.toml | 21 ++++++++-------- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/alphabetlearning/payments/tests/test_views.py b/alphabetlearning/payments/tests/test_views.py index 24bb19a..aebb040 100644 --- a/alphabetlearning/payments/tests/test_views.py +++ b/alphabetlearning/payments/tests/test_views.py @@ -21,3 +21,38 @@ def test_add_resource_to_cart(client, resource, user): response = client.get(url) # resdirects to the shopping cart assert response.status_code == 302 + + +@pytest.mark.django_db # Marks the test function as utilizing the Django database. +def test_cart_contains_resource_with_correct_price(client, resource, user): + # Create a Price object associated with the given resource, with a specified price and Stripe price ID. + # strip works with cents, so 2000 is 2.00 + price = Price.objects.create(resource=resource, price=2000, stripe_price_id="price_1") + + # Add the created price object to the resource's price objects. + # This is a many-to-many relationship, so we use the add method to add the price object to the resource's price_obj field. + resource.price_obj.add(price) + + # Generate the URL for adding the resource to the shopping basket using a reverse lookup. + addurl = reverse("payments:add_to_basket", kwargs={"resource_id": resource.id}) + + # Generate the URL for viewing the shopping cart. + carturl = reverse("payments:cart_detail") + + # Log in the user for the test client to simulate an authenticated user session. + client.force_login(user) + + # Send a GET request to the addurl to attempt to add the resource to the basket and store the response. + addresponse = client.get(addurl) + + # Check that the response status code indicates a successful redirection (302). + assert addresponse.status_code == 302 + + # Send a GET request to the carturl to retrieve the cart's contents and store the response. + cartresponse = client.get(carturl) + + # Assert that the resource's name is included in the cart response content, confirming it was added. + assert resource.name in str(cartresponse.content) + + # Assert that the correct price (formatted as '2.00') is displayed in the cart response content. + assert "2.00" in str(cartresponse.content) diff --git a/alphabetlearning/resources/factories.py b/alphabetlearning/resources/factories.py index 0f0194b..c8d2df4 100644 --- a/alphabetlearning/resources/factories.py +++ b/alphabetlearning/resources/factories.py @@ -37,11 +37,9 @@ class ResourceModelFactory(factory.django.DjangoModelFactory): model = Resource name = factory.Sequence(lambda n: f"Default Resource {n}") - # price = factory.Faker("pydecimal", left_digits=4, right_digits=2, positive=True) thumbnail_filenames = factory.List( [factory.Faker("file_name", extension="jpg") for _ in range(3)] ) - # price_obj = factory.RelatedFactoryList("alphabetlearning.payments.factories.PriceFactory", size=1) resource_type = factory.SubFactory(ResourceTypeModelFactory) main_resource_category = factory.SubFactory(ResourceCategoryModelFactory) subcategories = factory.RelatedFactoryList(ResourceCategoryModelFactory, size=2) diff --git a/alphabetlearning/templates/base.html b/alphabetlearning/templates/base.html index 2e28333..0fe30c5 100644 --- a/alphabetlearning/templates/base.html +++ b/alphabetlearning/templates/base.html @@ -12,7 +12,7 @@ {% endblock title %} - + {% block extra_css %} diff --git a/pyproject.toml b/pyproject.toml index cd75303..399897b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -99,6 +99,7 @@ django_settings_module = "config.settings.local" [tool.ruff] # Exclude a variety of commonly ignored directories. +line-length = 100 exclude = [ ".bzr", ".direnv", @@ -157,15 +158,13 @@ extend-unsafe-fixes = [ # Allow unused variables when underscore-prefixed. dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" -[tool.ruff.format] -# Same as Django: https://github.com/cookiecutter/cookiecutter-django/issues/4792. -line-length = 100 -indent-width = 4 -target-version = "py312" -quote-style = "double" -indent-style = "space" -skip-magic-trailing-comma = false -line-ending = "auto" +# [tool.ruff.format] +# # Same as Django: https://github.com/cookiecutter/cookiecutter-django/issues/4792. +# target-version = "py312" +# quote-style = "double" +# indent-style = "space" +# skip-magic-trailing-comma = false +# line-ending = "auto" -[tool.ruff.lint.isort] -force-single-line = true +# [tool.ruff.lint.isort] +# force-single-line = true -- cgit v1.2.3