Commit ba71abf6 authored by lizonr1's avatar lizonr1

Add tests

parent 1c081ea8
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from App.loyaltyMe.models import Product from App.loyaltyMe.models import Product
from App.loyaltyMe.test_data.products import PRODUCTS_SAMPLE_DATA
class Command(BaseCommand): class Command(BaseCommand):
help = 'Closes the specified poll for voting' help = 'Closes the specified poll for voting'
def handle(self, *args, **options): def handle(self, *args, **options):
products = [ products = PRODUCTS_SAMPLE_DATA
# ['name', 'description', 'price', 'img' ],
['Kawa Latte', 'Z nutą czekolady', '7 zł', '/static/img/photo_1.jpg'],
['Herbata', 'Z nutą czekolady', '4 zł', '/static/img/photo_1.jpg'],
['Ciasto', 'Z nutą czekolady', '5 zł', '/static/img/photo_1.jpg'],
['Espresso', 'Mała czarna', '5 zł', '/static/img/photo_1.jpg'],
]
print("Creating Products") print("Creating Products")
for product in products: for product in products:
print('Create {0}'.format(product[0])) print('Create {0}'.format(product[0]))
......
import datetime from datetime import datetime, timedelta
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
...@@ -9,18 +9,22 @@ class Command(BaseCommand): ...@@ -9,18 +9,22 @@ class Command(BaseCommand):
help = 'Closes the specified poll for voting' help = 'Closes the specified poll for voting'
def handle(self, *args, **options): def handle(self, *args, **options):
tasks = [ tasks = [
# 'name',products # 'name',products
['Bądź u nas 10 godzin', datetime.datetime.now(), 'description', 'TM', datetime.timedelta(seconds=15), ['Bądź u nas 10 godzin', timedelta(seconds=10), 'description', 'TM', 4,
15, 15, datetime.timedelta(seconds=15)], 15, 15, timedelta(seconds=15)],
['Przyjdz 10 razy', datetime.datetime.now(), 'description', 'PR', datetime.timedelta(seconds=15), ['Przyjdz 10 razy', timedelta(seconds=10), 'description', 'TM', 4,
15, 15, datetime.timedelta(seconds=15)], 15, 15, timedelta(seconds=15)],
] ]
print("Creating Tasks") print("Creating Tasks")
for task in tasks: for task in tasks:
print('Create {0}'.format(task[0])) print('Create {0}'.format(task[0]))
new_task = Task.objects.create(name=task[0], update_delay=task[1], description=task[2], type=task[3], new_task = Task.objects.create(name=task[0], update_delay=task[1], description=task[2], type=task[3],
counter_target=task[4], beacon_counter_target=task[5], counter_target=task[4], beacon_counter_target=task[5],
counter_incrementation=task[6], beacon_signal_delay=task[7]) counter_incrementation=task[6], beacon_signal_delay=task[7])
new_task.save() new_task.save()
PRODUCTS_SAMPLE_DATA = [
# ['name', 'description', 'price', 'img' ],
{'name': 'Kawa Latte', 'description':'Z nutą czekolady', 'price':'7 zł', 'img':'/static/img/photo_1.jpg'},
{'name': 'Herbata', 'description':'Z nutą czekolady', 'price':'4 zł', 'img':'/static/img/photo_1.jpg'},
{'name': 'Ciasto', 'description':'Z nutą czekolady', 'price':'5 zł', 'img':'/static/img/photo_1.jpg'},
{'name': 'Espresso', 'description':'Mała czarna', 'price':'5 zł', 'img':'/static/img/photo_1.jpg'},
]
PRODUCTS_SAMPLE_SERIALIZED_DATA = [
# ['name', 'description', 'price', 'img' ],
{'description': 'Z nutą czekolady', 'url': 'http://testserver/api/products/1/', 'img': 'http://testserver/static/img/photo_1.jpg', 'price': '7 zł', 'id': 1, 'name': 'Kawa Latte'},
]
...@@ -10,10 +10,10 @@ from App.user.models import UserTask ...@@ -10,10 +10,10 @@ from App.user.models import UserTask
class TaskTestCase(TestCase): class TaskTestCase(TestCase):
def setUp(self): def setUp(self):
Task.objects.get_or_create(type="TM", update_delay=timedelta(seconds=10), self.task = Task.objects.get_or_create(type="TM", update_delay=timedelta(seconds=10),
description="Test_description", name="test_name", counter_target=5, description="Test_description", name="test_name", counter_target=1,
beacon_counter_target=5, counter_incrementation=1, beacon_counter_target=2, counter_incrementation=1,
beacon_signal_delay=timedelta(seconds=10)) beacon_signal_delay=timedelta(seconds=15))[0]
class UserTaskTestCase(TestCase): class UserTaskTestCase(TestCase):
...@@ -118,3 +118,22 @@ class UserTaskTestCase(TestCase): ...@@ -118,3 +118,22 @@ class UserTaskTestCase(TestCase):
result = self.user_task.update() result = self.user_task.update()
self.assertTrue(self.user_task.done) self.assertTrue(self.user_task.done)
self.assertTrue(result) self.assertTrue(result)
def test_update_test_not_ready_to_update(self):
self.user_task.signal_last_update = timedelta(seconds=15)
self.user_task.signal_delay = datetime.now() - timedelta(seconds=14)
result = self.user_task.update()
self.assertFalse(result)
def test_update_test_task_is_done(self):
self.set_data_task_signal_passed()
self.set_data_beacon_singal_passed()
self.user_task.beacon_counter = 0
self.user_task.task.beacon_counter_target = 1
self.user_task.counter = 0
self.user_task.task.counter_target = 1
self.user_task.done = True
result = self.user_task.update()
self.assertFalse(result)
\ No newline at end of file
from django.test import TestCase, RequestFactory
from rest_framework.test import APIRequestFactory
# Using the standard RequestFactory API to create a form POST request
from App.loyaltyMe.models import Product
from App.loyaltyMe.serializers import ProductSerializer
from App.loyaltyMe.test_data.products import PRODUCTS_SAMPLE_DATA, PRODUCTS_SAMPLE_SERIALIZED_DATA
class ProductSerializerTestCase(TestCase):
def setUp(self):
self.product_data = PRODUCTS_SAMPLE_DATA[0]
self.product_data_serialized = PRODUCTS_SAMPLE_SERIALIZED_DATA[0]
self.product = Product.objects.create(**self.product_data)
def test_serializer(self):
context = dict(request=RequestFactory().get('/'))
serializer = ProductSerializer(self.product, context=context)
self.assertEqual(serializer.data, self.product_data_serialized)
\ No newline at end of file
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from App.user.test_data.users import USERS_SAMPLE_DATA
class Command(BaseCommand): class Command(BaseCommand):
help = 'Closes the specified poll for voting' help = 'Closes the specified poll for voting'
def handle(self, *args, **options): def handle(self, *args, **options):
users = [ users = USERS_SAMPLE_DATA
['admin', 'admin', 'admin@gmail.com', ],
['Antek', 'Antek', 'antek@gmail.com'],
['Rafal', 'Rafal', 'rafal@gmail.com'],
['Klaudia', 'Klaudia', 'klaudia@gmail.com']
]
print("Creating Users") print("Creating Users")
for user in users: for user in users:
print('Create {0}'.format(user[0])) print('Create {0}'.format(user[0]))
......
...@@ -10,10 +10,10 @@ from App.user.models import UserTask ...@@ -10,10 +10,10 @@ from App.user.models import UserTask
class Command(BaseCommand): class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
data = [ data = [
['Bądź u nas 10 godzin', 'admin', True,datetime.datetime.now(),15,15,datetime.datetime.now()], ['Bądź u nas 10 godzin', 'admin', True, datetime.datetime.now(), 15, 15, datetime.datetime.now()],
['Bądź u nas 10 godzin', 'Klaudia', False,datetime.datetime.now(),15,15,datetime.datetime.now()], ['Bądź u nas 10 godzin', 'Klaudia', False, datetime.datetime.now(), 15, 15, datetime.datetime.now()],
['Przyjdz 10 razy', 'admin', False,datetime.datetime.now(),15,15,datetime.datetime.now()], ['Przyjdz 10 razy', 'admin', False, datetime.datetime.now(), 15, 15, datetime.datetime.now()],
['Przyjdz 10 razy', 'Antek', True, datetime.datetime.now(),15,15,datetime.datetime.now()], ['Przyjdz 10 razy', 'Antek', True, datetime.datetime.now(), 15, 15, datetime.datetime.now()],
] ]
print("Creating UsersTasks") print("Creating UsersTasks")
...@@ -21,8 +21,9 @@ class Command(BaseCommand): ...@@ -21,8 +21,9 @@ class Command(BaseCommand):
print("{0} take {1} Task".format(user_task[0], user_task[1])) print("{0} take {1} Task".format(user_task[0], user_task[1]))
task = Task.objects.filter(name=user_task[0])[0] task = Task.objects.filter(name=user_task[0])[0]
user = User.objects.filter(username=user_task[1])[0] user = User.objects.filter(username=user_task[1])[0]
counter = datetime.timedelta(seconds=user_task[4]) counter = user_task[4]
beacon_counter= datetime.timedelta(seconds=user_task[5]) beacon_counter = user_task[5]
new_usertask = UserTask.objects.create(task=task, user=user, done=user_task[2], last_update=user_task[3],
new_usertask = UserTask.objects.create(task=task, user=user, done=user_task[2],last_update=user_task[3],counter=counter, beacon_counter=beacon_counter,beacon_signal_last_update=user_task[6]) counter=counter, beacon_counter=beacon_counter,
beacon_signal_last_update=user_task[6])
new_usertask.save() new_usertask.save()
USERS_SAMPLE_DATA = [
['admin', 'admin', 'admin@gmail.com', ],
['Antek', 'Antek', 'antek@gmail.com'],
['Rafal', 'Rafal', 'rafal@gmail.com'],
['Klaudia', 'Klaudia', 'klaudia@gmail.com']
]
\ No newline at end of file
import rest_framework
from django.test import TestCase
from App.user.serializers import AuthTokenSerializerByEmail
from django.contrib.auth.models import User
from App.user.test_data.users import USERS_SAMPLE_DATA
class AuthTokenSerializerByEmailTestCase(TestCase):
def setUp(self):
self.auth_by_email = AuthTokenSerializerByEmail()
user = USERS_SAMPLE_DATA[0]
self.test_user = User.objects.create_user(username=user[0], password=user[1], email=user[2], is_active=True,
is_superuser=True, is_staff=True, )
self.test_user.save()
def test_success_validate(self):
test = {'email':'admin@gmail.com', 'password':'admin'}
validation = self.auth_by_email.validate(test)
self.assertIn('user', validation)
def test_fail_validate_wrong_password(self):
test = {'email':'admin@gmail.com', 'password':'admin12'}
self.assertRaises(rest_framework.exceptions.ValidationError,self.auth_by_email.validate, test )
def test_fail_validate_user_not_exist(self):
test = {'email':'admined2@gmail.com', 'password':'admin12'}
self.assertRaises(rest_framework.exceptions.ValidationError,self.auth_by_email.validate, test )
def test_fail_validate_lack_of_data(self):
test = {'email':'admin@gmail.com', }
self.assertRaises(rest_framework.exceptions.ValidationError,self.auth_by_email.validate, test)
def test_fail_validate_user_blocked(self):
self.test_user.is_active = False
self.test_user.save()
test = {'email':'admin@gmail.com', 'password':'admin'}
self.assertRaises(rest_framework.exceptions.ValidationError,self.auth_by_email.validate, test )
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment