Skip to content

Instantly share code, notes, and snippets.

@dlyapun
Created November 23, 2017 16:29
Show Gist options
  • Save dlyapun/21364ab0c012ca50ec3813b12f92c2df to your computer and use it in GitHub Desktop.
Save dlyapun/21364ab0c012ca50ec3813b12f92c2df to your computer and use it in GitHub Desktop.
UNIT-TESTS DJANGO
# -*- coding: utf-8 -*-
from django.core.management import call_command
from django.core.urlresolvers import reverse
from django.test import TestCase, Client
from django.contrib.admin.sites import AdminSite
from common.test.factories import MealOptionFactory, MealOptionCategoryFactory, MealFactory, RestaurantFactory, \
MealCategoryFactory, UserFactory
from gloodny.models import *
from django.forms.models import model_to_dict
class ChangeMealAdminTests(TestCase):
"""
ChangeMealAdminTests TestCase - class for checking the behavior of Meal after admin actions on admin-panel
"""
OLD_MEAL_TITLE = ""
OLD_MEAL_PRICE = ""
OLD_MEAL_OPTION_CATEGORIES = []
NEW_MEAL_TITLE = 'NEW TITLE'
NEW_MEAL_PRICE = Decimal('99.99')
NEW_MEAL_OPTION_CATEGORIES = [2, ]
def setUp(self):
"""
Initialization testing data via factories. Create superuser for login to admin.
Initialization post url to admin page, action method and meal instance data.
Checking for not equal new and old meal option categories for created meal
:return: None
"""
self.site = AdminSite()
# Initialization of test data
self.user = UserFactory(username='root', email="root@root.com", password='root', is_superuser=True, is_staff=True)
self.restaurant = RestaurantFactory(on_gloodny=True)
self.meal = MealFactory(restaurant=self.restaurant, title='Burger', price=40.0)
self.meal_option_category = MealOptionCategoryFactory(restaurant=self.restaurant)
self.meal_option_category_two = MealOptionCategoryFactory(restaurant=self.restaurant)
self.meal_category = MealCategoryFactory(restaurant=self.restaurant)
self.meal_option = MealOptionFactory(name='Cheese', price=5.0, option_category=self.meal_option_category)
self.meal.option_categories.add(self.meal_option_category)
self.meal.categories.add(self.meal_category)
# Setup old meal data to CONSTANTS
self.OLD_MEAL_TITLE = self.meal.title
self.OLD_MEAL_PRICE = self.meal.price
self.OLD_MEAL_OPTION_CATEGORIES = self.meal.option_categories.values_list('id', flat=True)
self.change_history = None
self.client = Client()
self.client.login(username='root', password='root')
# Checking for not equal new and old meal option categories for created meal
self.assertNotEqual(set(self.OLD_MEAL_OPTION_CATEGORIES), set(self.NEW_MEAL_OPTION_CATEGORIES))
self.url = reverse('admin:gloodny_meal_change', args=(self.meal.id,))
self.data = model_to_dict(self.meal)
# Action on admin panel
self.data['_save'] = 'save'
def test_change_meal_title(self):
"""
Checking if meal's title didn't change after change title on admin page
:return: None
"""
# Set new title
self.data['title'] = self.NEW_MEAL_TITLE
# Send request
self.client.post(self.url, self.data)
# Check if old title didn't change
self.meal.refresh_from_db()
self.assertEqual(self.OLD_MEAL_TITLE, self.meal.title)
# Check if ChangeHistory been crated with new meal title
change_history = ChangeHistory.objects.get(title=self.NEW_MEAL_TITLE)
# Check if change_history store not changed original fields
self.assertEqual(self.meal.price, change_history.price)
def test_change_meal_price(self):
"""
Checking if meal's price didn't change after change price on admin page
:return: None
"""
self.data['price'] = self.NEW_MEAL_PRICE
self.client.post(self.url, self.data)
# Check if old data not been changed
self.meal.refresh_from_db()
self.assertEqual(self.OLD_MEAL_PRICE, self.meal.price)
# Check if ChangeHistory been crated with new meal price
change_history = ChangeHistory.objects.get(price=self.NEW_MEAL_PRICE)
# Check if change_history store not changed original fields
self.assertEqual(self.meal.title, change_history.title)
def test_change_meal_option_categories(self):
"""
Checking if meal's option_categories didn't change after change option_categories on admin page
:return: None
"""
self.data['option_categories'] = self.NEW_MEAL_OPTION_CATEGORIES
self.client.post(self.url, self.data)
self.meal.refresh_from_db()
# Check if old data not been changed
self.assertEqual(set(self.OLD_MEAL_OPTION_CATEGORIES), set(self.meal.option_categories.values_list('id', flat=True)))
# Check if ChangeHistory been crated with new meal option_categories
change_history = ChangeHistory.objects.get(option_categories__id__in=self.NEW_MEAL_OPTION_CATEGORIES)
# Check if change_history store not changed original fields
self.assertEqual(self.meal.title, change_history.title)
def test_change_meal_full(self):
"""
Checking if meal's price, title and option_categories didn't change after change meal on admin page
:return: None
"""
self.data['title'] = self.NEW_MEAL_TITLE
self.data['price'] = self.NEW_MEAL_PRICE
self.data['option_categories'] = self.NEW_MEAL_OPTION_CATEGORIES
self.client.post(self.url, self.data)
self.meal.refresh_from_db()
# Check if old data not been changed
self.assertEqual(self.OLD_MEAL_TITLE, self.meal.title)
self.assertEqual(self.OLD_MEAL_PRICE, self.meal.price)
self.assertEqual(set(self.OLD_MEAL_OPTION_CATEGORIES), set(self.meal.option_categories.values_list('id', flat=True)))
# Check if ChangeHistory been crated with new meal data
change_history = ChangeHistory.objects.get(
title=self.NEW_MEAL_TITLE,
price=self.NEW_MEAL_PRICE,
option_categories__id__in=self.NEW_MEAL_OPTION_CATEGORIES,
)
def test_meal_history_instance(self):
"""
Checking if meal history created after changed original meal instance
:return: None
"""
self.data['short_description'] = "short_description"
self.client.post(self.url, self.data)
self.meal.refresh_from_db()
first_history_instance = self.meal.history.first()
last_history_instance = self.meal.history.last()
# Check if history instances not equal
self.assertNotEqual(first_history_instance.pk, last_history_instance.pk)
self.assertNotEqual(first_history_instance.short_description, last_history_instance.short_description)
# Check if the last history instance title equal current meal title
self.assertEqual(last_history_instance.title, self.meal.title)
# Check if the first history instance title equal old meal title
self.assertEqual(first_history_instance.title, self.OLD_MEAL_TITLE)
def test_apply_command(self):
"""
Checking if management command "apply_change_history" change original meal data
:return: None
"""
self.data['title'] = self.NEW_MEAL_TITLE
self.data['price'] = self.NEW_MEAL_PRICE
self.data['option_categories'] = self.NEW_MEAL_OPTION_CATEGORIES
self.client.post(self.url, self.data)
self.meal.refresh_from_db()
# Check if change_history instance been created
change_history = ChangeHistory.objects.get(
title=self.NEW_MEAL_TITLE,
price=self.NEW_MEAL_PRICE,
option_categories__id__in=self.NEW_MEAL_OPTION_CATEGORIES,
)
call_command('apply_change_history')
# Check if change_history not applied
change_history = ChangeHistory.objects.get(
title=self.NEW_MEAL_TITLE,
price=self.NEW_MEAL_PRICE,
option_categories__id__in=self.NEW_MEAL_OPTION_CATEGORIES,
)
# Set yesterday date
change_history.update_time = change_history.update_time - timedelta(days=1)
change_history.save()
# Rerun management command
call_command('apply_change_history')
self.meal.refresh_from_db()
# Check if change_history instance was deleted
change_history = ChangeHistory.objects.filter(
title=self.NEW_MEAL_TITLE,
price=self.NEW_MEAL_PRICE,
option_categories__id__in=self.NEW_MEAL_OPTION_CATEGORIES,
).exists()
self.assertEqual(False, change_history)
# Check if management command updated original meal data
self.assertEqual(self.meal.title, self.NEW_MEAL_TITLE)
self.assertEqual(self.meal.price, self.NEW_MEAL_PRICE)
self.assertEqual(set(self.meal.option_categories.values_list('id', flat=True)), set(self.NEW_MEAL_OPTION_CATEGORIES))
class ChangeMealOptionAdminTests(TestCase):
"""
ChangeMealOptionAdminTests TestCase - class for checking the behavior of MealOption after admin actions on admin-panel
"""
OLD_MEAL_NAME = ""
OLD_MEAL_PRICE = ""
NEW_MEAL_NAME = 'NEW TITLE'
NEW_MEAL_PRICE = Decimal('99.99')
def setUp(self):
"""
Initialization testing data via factories. Create superuser for login to admin.
Initialization post url to admin page, action method and meal instance data.
Checking for not equal new and old meal option categories for created meal
:return: None
"""
self.site = AdminSite()
# Initialization of test data
self.user = UserFactory(username='root', email="root@root.com", password='root', is_superuser=True, is_staff=True)
self.restaurant = RestaurantFactory(on_gloodny=True)
self.meal_option_category = MealOptionCategoryFactory(restaurant=self.restaurant)
self.meal_option_category_two = MealOptionCategoryFactory(restaurant=self.restaurant)
self.meal_category = MealCategoryFactory(restaurant=self.restaurant)
self.meal_option = MealOptionFactory(name='Cheese', price=5.0, option_category=self.meal_option_category)
# Setup old meal_option data to CONSTANTS
self.OLD_MEAL_NAME = self.meal_option.name
self.OLD_MEAL_PRICE = self.meal_option.price
self.change_history = None
self.client = Client()
self.client.login(username='root', password='root')
self.url = reverse('admin:gloodny_mealoption_change', args=(self.meal_option.id,))
self.data = model_to_dict(self.meal_option)
# Action on admin panel
self.data['_save'] = 'save'
def test_change_meal_name(self):
"""
Checking if meal_option name didn't change after change name on admin page
:return: None
"""
# Set new name
self.data['name'] = self.NEW_MEAL_NAME
# Send request
self.client.post(self.url, self.data)
# Check if old name didn't change
self.meal_option.refresh_from_db()
self.assertEqual(self.OLD_MEAL_NAME, self.meal_option.name)
# Check if ChangeHistory been crated with new meal_option name
change_history = ChangeHistory.objects.get(title=self.NEW_MEAL_NAME)
# Check if change_history store not changed original fields
self.assertEqual(self.meal_option.price, change_history.price)
def test_change_meal_price(self):
"""
Checking if meal_option price didn't change after change price on admin page
:return: None
"""
self.data['price'] = self.NEW_MEAL_PRICE
self.client.post(self.url, self.data)
# Check if old data not been changed
self.meal_option.refresh_from_db()
self.assertEqual(self.OLD_MEAL_PRICE, self.meal_option.price)
# Check if ChangeHistory been crated with new meal price
change_history = ChangeHistory.objects.get(price=self.NEW_MEAL_PRICE)
# Check if change_history store not changed original fields
self.assertEqual(self.meal_option.name, change_history.name)
def test_meal_history_instance(self):
"""
Checking if meal_option history created after changed original meal_option instance
:return: None
"""
self.data['order'] = 10
self.client.post(self.url, self.data)
self.meal_option.refresh_from_db()
first_history_instance = self.meal_option.history.first()
last_history_instance = self.meal_option.history.last()
# Check if history instances not equal
self.assertNotEqual(first_history_instance.pk, last_history_instance.pk)
self.assertNotEqual(first_history_instance.order, last_history_instance.order)
# Check if the last history instance name equal current meal name
self.assertEqual(last_history_instance.name, self.meal_option.name)
# Check if the first history instance name equal old meal name
self.assertEqual(first_history_instance.name, self.OLD_MEAL_NAME)
def test_apply_command(self):
"""
Checking if management command "apply_change_history" change original meal_option data
:return: None
"""
self.data['name'] = self.NEW_MEAL_NAME
self.data['price'] = self.NEW_MEAL_PRICE
self.client.post(self.url, self.data)
self.meal_option.refresh_from_db()
# Check if change_history instance been created
change_history = ChangeHistory.objects.get(
title=self.NEW_MEAL_NAME,
price=self.NEW_MEAL_PRICE,
)
call_command('apply_change_history')
# Check if change_history not applied
change_history = ChangeHistory.objects.get(
title=self.NEW_MEAL_NAME,
price=self.NEW_MEAL_PRICE,
)
# Set yesterday date
change_history.update_time = change_history.update_time - timedelta(days=1)
change_history.save()
# Rerun management command
call_command('apply_change_history')
self.meal_option.refresh_from_db()
# Check if change_history instance was deleted
change_history = ChangeHistory.objects.filter(
title=self.NEW_MEAL_NAME,
price=self.NEW_MEAL_PRICE,
).exists()
self.assertEqual(False, change_history)
# Check if management command updated original meal data
self.assertEqual(self.meal_option.name, self.NEW_MEAL_NAME)
self.assertEqual(self.meal_option.price, self.NEW_MEAL_PRICE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment