From f5ea6f71d98a6e34fdc83ec93ee5b31d8d977f15 Mon Sep 17 00:00:00 2001 From: Pravin030399 Date: Sun, 2 Jun 2024 22:13:08 +0800 Subject: [PATCH] add unit test for orders --- orders/test/__init__.py | 0 orders/test/test_forms.py | 42 +++++++++++++++++++++++ orders/test/test_models.py | 69 ++++++++++++++++++++++++++++++++++++++ orders/test/test_views.py | 58 ++++++++++++++++++++++++++++++++ orders/tests.py | 3 -- 5 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 orders/test/__init__.py create mode 100644 orders/test/test_forms.py create mode 100644 orders/test/test_models.py create mode 100644 orders/test/test_views.py delete mode 100644 orders/tests.py diff --git a/orders/test/__init__.py b/orders/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/orders/test/test_forms.py b/orders/test/test_forms.py new file mode 100644 index 0000000..95b7109 --- /dev/null +++ b/orders/test/test_forms.py @@ -0,0 +1,42 @@ +from django.test import TestCase +from dashboard.models import Menu +from branches.models import Branch +from orders.models import Order, PaymentMethod +from orders.forms import OrderPlaceForm + + +class OrderPlaceFormTest(TestCase): + + def setUp(self): + self.branch = Branch.objects.create(branch_name='Pravin Branch', branch_address='pravin Address', branch_contact='Pravin Contact') + self.menu = Menu.objects.create(name='Test Menu', description='Test Description', price=10.00) + + def test_order_place_form_valid(self): + form_data = { + 'street': 'Pravin Street', + 'city': 'Klang City', + 'state': ' Selangor', + 'zipcode': '41200', + 'mobile': '0124596788', + 'payment_method': PaymentMethod.CASH, + 'total_amount': 10.00, + 'branch': self.branch.id, + 'menu': self.menu.id, + } + form = OrderPlaceForm(data=form_data) + self.assertTrue(form.is_valid()) + + def test_order_place_form_invalid(self): + form_data = { + 'street': 'Pravin Street', + 'city': 'Klang City', + 'state': ' Selangor', + 'zipcode': '41200', + 'mobile': '', + 'payment_method': PaymentMethod.CASH, + 'total_amount': 10.00, + 'branch': self.branch.id, + 'menu': self.menu.id, + } + form = OrderPlaceForm(data=form_data) + self.assertFalse(form.is_valid()) diff --git a/orders/test/test_models.py b/orders/test/test_models.py new file mode 100644 index 0000000..72c15dc --- /dev/null +++ b/orders/test/test_models.py @@ -0,0 +1,69 @@ +from django.test import TestCase +from django.contrib.auth.models import User +from branches.models import Branch +from dashboard.models import Menu +from orders.models import Order, PaymentMethod, OrderStatus + + +class OrderModelTest(TestCase): + + def setUp(self): + self.user = User.objects.create_user(username='pravin', password='pg030399') + self.branch = Branch.objects.create(branch_name='Pravin Branch', branch_address='Pravin Address', branch_contact='Pravin Contact') + self.menu = Menu.objects.create(name='Pravin Menu', description='Pravin Description', price=10.00) + + def test_order_creation(self): + order = Order.objects.create( + street='Pravin Street', + city='Klang City', + state='Selangor', + zipcode='41200', + mobile='0124596788', + total_amount=10.00, + payment_method=PaymentMethod.CASH, + user=self.user, + branch=self.branch, + menu=self.menu, + ) + self.assertEqual(order.street, 'Pravin Street') + self.assertEqual(order.city, 'Klang City') + self.assertEqual(order.state, 'Selangor') + self.assertEqual(order.zipcode, '41200') + self.assertEqual(order.mobile, '0124596788') + self.assertEqual(order.total_amount, 10.00) + self.assertEqual(order.payment_method, PaymentMethod.CASH) + self.assertEqual(order.order_status, OrderStatus.NEW) + self.assertEqual(order.user, self.user) + self.assertEqual(order.branch, self.branch) + self.assertEqual(order.menu, self.menu) + + def test_get_name(self): + order = Order.objects.create( + street='Pravin Street', + city='Klang City', + state='Selangor', + zipcode='41200', + mobile='0124596788', + total_amount=10.00, + payment_method=PaymentMethod.CASH, + user=self.user, + branch=self.branch, + menu=self.menu, + ) + self.assertEqual(order.get_name(), f'{self.user.first_name} {self.user.last_name}') + + def test_get_address(self): + order = Order.objects.create( + street='Pravin Street', + city='Klang City', + state='Selangor', + zipcode='41200', + mobile='0124596788', + total_amount=10.00, + payment_method=PaymentMethod.CASH, + user=self.user, + branch=self.branch, + menu=self.menu, + ) + expected_address = 'Pravin Street, Klang City, Selangor, 41200' + self.assertEqual(order.get_address(), expected_address) diff --git a/orders/test/test_views.py b/orders/test/test_views.py new file mode 100644 index 0000000..5eba666 --- /dev/null +++ b/orders/test/test_views.py @@ -0,0 +1,58 @@ +from django.test import TestCase +from django.urls import reverse +from django.contrib.auth.models import User +from django.test.client import Client +from dashboard.models import Menu +from orders.models import Order +from orders.forms import OrderPlaceForm + +class OrderPlaceViewTests(TestCase): + + def setUp(self): + self.user = User.objects.create_user(username='pravin', password='pg030399') + self.menu = Menu.objects.create(name='Pravin Menu', description='Pravin Description', price=10.00) + self.client = Client() + self.client.login(username='pravin', password='pg030399') + + def test_get_order_place_view(self): + response = self.client.get(reverse('orders:order') + f'?menu_id={self.menu.id}') + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'order-place.html') + self.assertIsInstance(response.context['form'], OrderPlaceForm) + self.assertEqual(response.context['form'].fields['menu'].queryset.count(), 1) + self.assertEqual(response.context['menu'], self.menu) + +def test_post_order_place_view_invalid(self): + form_data = { + 'street': 'Pravin Street', + 'city': 'Klang City', + 'state': ' Selangor', + 'zipcode': '41200', + 'mobile': '0124596788', + 'payment_method': 'CASH', + 'total_amount': 10.00, + 'branch': self.menu.branch_id, + 'menu': self.menu.id, + } + response = self.client.post(reverse('orders:order'), data=form_data) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'order-place.html') + self.assertTrue(response.context['form'].errors) + self.assertFalse(Order.objects.filter(user=self.user, menu=self.menu).exists()) + +def test_post_order_place_view_valid(self): + form_data = { + 'street': 'Pravin Street', + 'city': 'Klang City', + 'state': ' Selangor', + 'zipcode': '41200', + 'mobile': '0124596788', + 'payment_method': 'CASH', + 'total_amount': 10.00, + 'branch': self.menu.branch_id, + 'menu': self.menu.id, + } + response = self.client.post(reverse('orders:order'), data=form_data) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'order-confirm.html') + self.assertTrue(Order.objects.filter(user=self.user, menu=self.menu).exists()) diff --git a/orders/tests.py b/orders/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/orders/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here.