Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
add unit test for orders
Browse files Browse the repository at this point in the history
  • Loading branch information
Pravin030399 committed Jun 2, 2024
1 parent 6763b93 commit f5ea6f7
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 3 deletions.
Empty file added orders/test/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions orders/test/test_forms.py
Original file line number Diff line number Diff line change
@@ -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())
69 changes: 69 additions & 0 deletions orders/test/test_models.py
Original file line number Diff line number Diff line change
@@ -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)
58 changes: 58 additions & 0 deletions orders/test/test_views.py
Original file line number Diff line number Diff line change
@@ -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())
3 changes: 0 additions & 3 deletions orders/tests.py

This file was deleted.

0 comments on commit f5ea6f7

Please sign in to comment.