-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update tests.py #717
Update tests.py #717
Conversation
Unit Tests for Cart Functionality This pull request adds comprehensive unit tests for the Cart class in the cart.py module. The tests cover various scenarios to ensure the correctness and robustness of the Cart implementation. The test cases included in this pull request are as follows: Cart Initialization Test: This test verifies that the cart is properly initialized and empty when a request is made. Add Product Test: This test checks the functionality of adding products to the cart, including verifying the correct quantity and subtotal price calculations. Save Cart Test: This test ensures that the cart is marked as modified after it is saved. Remove Product Test: This test validates the ability to remove products from the cart and confirms that the cart is empty after removal. Get Cart Products Test: This test checks if the get_cart_products method returns the expected list of products in the cart. Get Subtotal Price Test: This test verifies the accuracy of calculating the subtotal price of all products in the cart. Get Shipping Cost Test: This test ensures that the get_shipping_cost method returns the correct shipping cost based on the contents of the cart. (Please note that the expected shipping cost value needs to be provided.) Cart Iteration Test: This test validates that iterating over the cart items returns the correct product details, including quantity, price, and total price. These tests are designed to cover different aspects of the Cart functionality, including initialization, adding/removing products, calculating prices, and iterating over cart items. They provide comprehensive coverage to catch any potential bugs or issues in the Cart implementation. Please review these unit tests and let me know if any further changes or additions are required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really great work!
@josh-bristow, we're really in need of improved test coverage like you have provided in this pull request. Thank you so much. :-) Would you be able to help out with some other unit-test coverage issues? I've labeled them as help wanted and good first issue in our backlog: https://github.com/WesternFriend/WF-website/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no! For some reason, when the tests run, there are several errors:
AttributeError: 'WSGIRequest' object has no attribute 'session'
Did the tests run successfully in your local branch with python manage.py test
?
Let's see if we can resolve those errors so the test will pass by manually specifying the session
property.
Session and authentication attributes must be supplied by the test itself if required for the view to function properly.
Co-authored-by: Brylie Christopher Oxley <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests still seem to be failing. Please make sure you can run the tests locally with python manage.py test cart
and that they all pass successfully.
I think the errors in the tests are due to the test ‘client’ not having a session when making the request. There are some Stack Overflow threads and possibly official documentation for adding a ‘session’ object to the test client. |
This comment was marked as outdated.
This comment was marked as outdated.
ChatGPT recommends using the Django test Client. The Django test from django.test import TestCase, Client
from unittest.mock import patch
from .models import Product # import the Product model
from .cart import Cart # import your Cart class
from decimal import Decimal
class CartTestCase(TestCase):
def setUp(self):
self.client = Client()
self.product_mock = Product(id=1, title='product1', price=Decimal(10.00))
@patch.object(Product.objects, 'filter')
def test_add_product(self, mock_filter):
mock_filter.return_value = [self.product_mock]
session = self.client.session
cart = Cart(session)
cart.add(self.product_mock, quantity=2, update_quantity=False)
expected_cart = {
str(self.product_mock.id): {
"product_title": self.product_mock.title,
"product_id": str(self.product_mock.id),
"quantity": 2,
"price": str(self.product_mock.price),
}
}
self.assertEqual(cart.cart, expected_cart) The test |
ChatGPT: The In the context of Django and testing, this is typically used to replace complex behavior such as database queries, calls to external services, or other functions that you don't want to actually execute during a test. Instead, you want to simulate or "mock" their behavior. This allows you to test your own code in isolation, without depending on external systems or side effects. When you use
For example, The In the provided code, this is used to simulate the behavior of the |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Code Climate has analyzed commit c164f53 and detected 0 issues on this pull request. The test coverage on the diff in this pull request is 87.5% (50% is the threshold). This pull request will bring the total coverage in the repository to 67.4% (1.2% change). View more on Code Climate. |
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #717 +/- ##
==========================================
+ Coverage 66.26% 67.45% +1.18%
==========================================
Files 99 99
Lines 2911 2916 +5
==========================================
+ Hits 1929 1967 +38
+ Misses 982 949 -33
☔ View full report in Codecov by Sentry. |
@all-contributors please add @josh-bristow for code, test |
I've put up a pull request to add @josh-bristow! 🎉 |
Closes #681
Unit Tests for Cart Functionality
This pull request adds comprehensive unit tests for the Cart class in the cart.py module. The tests cover various scenarios to ensure the correctness and robustness of the Cart implementation.
The test cases included in this pull request are as follows:
Cart Initialization Test: This test verifies that the cart is properly initialized and empty when a request is made.
Add Product Test: This test checks the functionality of adding products to the cart, including verifying the correct quantity and subtotal price calculations.
Save Cart Test: This test ensures that the cart is marked as modified after it is saved.
Remove Product Test: This test validates the ability to remove products from the cart and confirms that the cart is empty after removal.
Get Cart Products Test: This test checks if the get_cart_products method returns the expected list of products in the cart.
Get Subtotal Price Test: This test verifies the accuracy of calculating the subtotal price of all products in the cart.
Get Shipping Cost Test: This test ensures that the get_shipping_cost method returns the correct shipping cost based on the contents of the cart. (Please note that the expected shipping cost value needs to be provided.)
Cart Iteration Test: This test validates that iterating over the cart items returns the correct product details, including quantity, price, and total price.
These tests are designed to cover different aspects of the Cart functionality, including initialization, adding/removing products, calculating prices, and iterating over cart items. They provide comprehensive coverage to catch any potential bugs or issues in the Cart implementation.
Please review these unit tests and let me know if any further changes or additions are required.