From 38b3736951cde1095d59338acd5035cc97ed5281 Mon Sep 17 00:00:00 2001 From: reverend Date: Fri, 11 Sep 2020 12:08:27 +0200 Subject: [PATCH] New Model tests --- .../tests/models/test_explorer_model.py | 55 +++++++++++++++++++ .../tests/models/test_voucher_model.py | 51 +++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 lostplaces/lostplaces_app/tests/models/test_explorer_model.py create mode 100644 lostplaces/lostplaces_app/tests/models/test_voucher_model.py diff --git a/lostplaces/lostplaces_app/tests/models/test_explorer_model.py b/lostplaces/lostplaces_app/tests/models/test_explorer_model.py new file mode 100644 index 0000000..825251d --- /dev/null +++ b/lostplaces/lostplaces_app/tests/models/test_explorer_model.py @@ -0,0 +1,55 @@ +from django.test import TestCase +from django.db import models +from django.contrib.auth.models import User + +from lostplaces_app.models import Explorer +from lostplaces_app.tests import mock_user + +class ExplorerTestCase(TestCase): + + def setUp(self): + mock_user() + + def test_epxlorer_creation(self): + ''' + Tests if the explorer profile will be automticly + created when a user is created + ''' + + user = User.objects.get(id=1) + explorer_list = Explorer.objects.all() + self.assertTrue(len(explorer_list) > 0, + msg='Expecting at least one Exlorer object, none found' + ) + self.assertTrue(hasattr(user, 'explorer'), + msg='''Expecting the User instance to have an \'explorer\' attribute. + Check the Explorer model and the related name.''' + ) + + explorer = Explorer.objects.get(id=1) + self.assertEqual(explorer, user.explorer, + msg='''The Explorer object of the User did not match. + Expecting User with id 1 to have Explorer with id 1''' + ) + + explorer = Explorer.objects.get(id=1) + self.assertEqual(explorer.user, user, + msg='''The User object of the Explorer did not match. + Expecting Explorer with id 1 to have User with id 1''' + ) + + def test_explorer_deletion(self): + ''' + Tests if the Explorer objects get's deleted when the User instance is deleted + ''' + + user = User.objects.get(id=1) + explorer_id = user.explorer.id + user.delete() + with self.assertRaises(models.ObjectDoesNotExist, + msg='Expecting explorer objec to be deleted when the corresponding User object is deleted' + ): + Explorer.objects.get(id=explorer_id) + + + \ No newline at end of file diff --git a/lostplaces/lostplaces_app/tests/models/test_voucher_model.py b/lostplaces/lostplaces_app/tests/models/test_voucher_model.py new file mode 100644 index 0000000..89c733e --- /dev/null +++ b/lostplaces/lostplaces_app/tests/models/test_voucher_model.py @@ -0,0 +1,51 @@ +import datetime + +from django.test import TestCase +from django.db import models + +from lostplaces_app.models import Voucher +from lostplaces_app.tests.models import ModelTestCase + + +def mock_voucher(): + return Voucher.objects.create( + code='ayDraJCCwfhcFiYmSR5GrcjcchDfcahv', + expires=datetime.datetime.now() + datetime.timedelta(days=1) + ) + + +class VoucheTestCase(ModelTestCase, TestCase): + model_name = 'Voucher' + + def setUp(self): + self.object = mock_voucher() + + def test_voucher_code(self): + self._test_char_field( + 'code', + 10, + 100, + must_have={'unique': True} + ) + + def test_voucher_created(self): + self._test_field( + 'created_when', + models.DateTimeField, + must_have={'auto_now_add': True} + ) + + def test_voucher_expires(self): + self._test_field( + 'expires_when', + models.DateTimeField, + must_not_have={'auto_now_add': True} + ) + + def test_str(self): + voucher = self.object + self.assertTrue(voucher.code.lower() in str(voucher).lower(), + msg='Expecting %s.__str__ to contain the voucher code' % ( + self.model_name + ) + ) \ No newline at end of file