Skip to content

Commit

Permalink
Merge pull request #819 from WesternFriend/improve-order-tests
Browse files Browse the repository at this point in the history
Improve order tests
  • Loading branch information
brylie authored Jul 23, 2023
2 parents 80e5d92 + a398747 commit 2f9c298
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 11 deletions.
125 changes: 123 additions & 2 deletions orders/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from unittest.mock import MagicMock, patch
from django.test import Client, TestCase
from unittest.mock import MagicMock, Mock, patch
from django.contrib.sessions.middleware import SessionMiddleware
from django.test import Client, RequestFactory, TestCase
from django.urls import reverse
from cart.cart import Cart
from cart.tests import scaffold_product_index_page

from orders.views import create_cart_order_items
from store.factories import ProductFactory


from .models import Order, OrderItem
Expand All @@ -17,6 +23,24 @@ def setUpTestData(cls) -> None:
purchaser_meeting_or_organization="Western Friend",
shipping_cost=4.00,
)
cls.order.save()

# Create OrderItems
cls.order_item_one = OrderItem.objects.create(
order=cls.order,
product_title="Product 1",
product_id=1,
price=10.00,
quantity=2,
)

cls.order_item_two = OrderItem.objects.create(
order=cls.order,
product_title="Product 2",
product_id=2,
price=20.00,
quantity=3,
)

def test_purchaser_full_name(self) -> None:
# Test the method
Expand All @@ -39,6 +63,13 @@ def test_str_method(self) -> None:
"Order 1",
)

def test_order_get_total_cost(self):
order_item_one_total = self.order_item_one.quantity * self.order_item_one.price
order_item_two_total = self.order_item_two.quantity * self.order_item_two.price
expected_total = order_item_one_total + order_item_two_total

self.assertEqual(self.order.get_total_cost(), expected_total)


class OrderItemModelTest(TestCase):
def setUp(self) -> None:
Expand Down Expand Up @@ -137,3 +168,93 @@ def test_order_create_view_post_request_form_invalid(self) -> None:

# Check if the correct template was used
self.assertTemplateUsed(response, "orders/create.html")


class CreateCartOrderItemsTest(TestCase):
def setUp(self) -> None:
# Create an order instance
self.order = Order.objects.create(
purchaser_given_name="John",
purchaser_family_name="Doe",
purchaser_meeting_or_organization="Western Friend",
shipping_cost=4.00,
)

# Create a request instance using RequestFactory
self.factory = RequestFactory()
self.request = self.factory.get("/")

# Add session to request
middleware = SessionMiddleware(Mock())
middleware.process_request(self.request)
self.request.session.save()

# Create a cart instance
self.cart = Cart(self.request)

product_index_page = scaffold_product_index_page()

# create some test products using the ProductFactory
self.product1 = ProductFactory.build()
self.product2 = ProductFactory.build()

product_index_page.add_child(instance=self.product1)
product_index_page.add_child(instance=self.product2)

# Add products to the cart
self.product1_quantity = 2
self.product2_quantity = 3
self.cart.add(
self.product1,
quantity=self.product1_quantity,
)
self.cart.add(
self.product2,
quantity=self.product2_quantity,
)

def test_create_cart_order_items(self) -> None:
# Call the function with the order and cart
create_cart_order_items(
self.order,
self.cart,
)

# Assert that the OrderItems were correctly created
self.assertEqual(
OrderItem.objects.count(),
2,
)
item1 = OrderItem.objects.get(
order=self.order,
product_id=self.product1.id,
)
self.assertEqual(
item1.product_title,
self.product1.title,
)
self.assertEqual(
item1.price,
self.product1.price,
)
self.assertEqual(
item1.quantity,
self.product1_quantity,
)

item2 = OrderItem.objects.get(
order=self.order,
product_id=self.product2.id,
)
self.assertEqual(
item2.product_title,
self.product2.title,
)
self.assertEqual(
item2.price,
self.product2.price,
)
self.assertEqual(
item2.quantity,
self.product2_quantity,
)
25 changes: 16 additions & 9 deletions orders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@

from cart.cart import Cart

from .models import OrderItem
from .models import Order, OrderItem


def create_cart_order_items(
order: Order,
cart: Cart,
) -> None:
for item in cart:
OrderItem.objects.create(
order=order,
product_title=item["product_title"],
product_id=item["product_id"],
price=item["price"],
quantity=item["quantity"],
)


def order_create(request: HttpRequest) -> HttpResponse:
Expand All @@ -27,14 +41,7 @@ def order_create(request: HttpRequest) -> HttpResponse:
if form.is_valid():
order = form.save()

for item in cart:
OrderItem.objects.create(
order=order,
product_title=item["product_title"],
product_id=item["product_id"],
price=item["price"],
quantity=item["quantity"],
)
create_cart_order_items(order, cart)

# TODO: consider moving this to the payment app
# so it can be cleared after successful payment.
Expand Down

0 comments on commit 2f9c298

Please sign in to comment.