From ab3ecae54b9f8a5156c637604d96fb9f74dcb466 Mon Sep 17 00:00:00 2001 From: reverend Date: Sun, 4 Oct 2020 22:12:44 +0200 Subject: [PATCH] Voucher is_valid property --- django_lostplaces/lostplaces/models/models.py | 4 + .../lostplaces/tests/forms/__init__.py | 76 +++++++++++++++++++ .../tests/forms/test_explorer_forms.py | 59 ++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 django_lostplaces/lostplaces/tests/forms/__init__.py create mode 100644 django_lostplaces/lostplaces/tests/forms/test_explorer_forms.py diff --git a/django_lostplaces/lostplaces/models/models.py b/django_lostplaces/lostplaces/models/models.py index a545fe0..16b53da 100644 --- a/django_lostplaces/lostplaces/models/models.py +++ b/django_lostplaces/lostplaces/models/models.py @@ -49,6 +49,10 @@ class Voucher(Expireable): """ code = models.CharField(unique=True, max_length=30) + + @property + def valid(self): + return not self.is_expired def __str__(self): return "Voucher " + str(self.code) diff --git a/django_lostplaces/lostplaces/tests/forms/__init__.py b/django_lostplaces/lostplaces/tests/forms/__init__.py new file mode 100644 index 0000000..0cf6568 --- /dev/null +++ b/django_lostplaces/lostplaces/tests/forms/__init__.py @@ -0,0 +1,76 @@ +from django.test import TestCase +from django.core.exceptions import FieldDoesNotExist + +class FormTestCase(TestCase): + ''' + Base class for FormTests. + Parameters: + - form : Form to test + ''' + form = None + + def assertField(self, field_name, field_class, must_have={}, must_not_have={}): + ''' + Tests if a field exists under the given name and + if the field is of the right type. + Also checks if the field has the given must_have attributes + and does not have any of the must_not_have attributes. If you + dont care about the value of the attribute you can just set it to + something that fullfills value == False (i.e. '' or 0) + ''' + try: + field = self.form.base_fields[field_name] + except FieldDoesNotExist: + self.fail( + 'Expecting %s to have a field named \'%s\'' % ( + self.form.__name__, + field_name + ) + ) + self.assertEqual( + type(field), field_class, + msg='Expecting type of %s to be %s' % ( + str(field), + field_class.__name__ + ) + ) + + for key, value in must_have.items(): + if value: + self.assertEqual( + getattr(field, key), value, + msg='Expeting the value of %s %s to be \'%s\'' % ( + str(field), + key, + value + ) + ) + else: + self.assertTrue( + hasattr(field, key), + msg='Expeting %s to have \'%s\'' % ( + str(field), + key + ) + ) + + for key, value in must_not_have.items(): + if value: + self.assertTrue( + getattr(field, key) != value, + msg='Expeting the value of %s %s to not be \'%s\'' % ( + str(field), + key, + value + ) + ) + else: + self.assertFalse( + hasattr(field, value), + msg='Expeting %s to not have \'%s\'' % ( + str(field), + key + ) + ) + + return field \ No newline at end of file diff --git a/django_lostplaces/lostplaces/tests/forms/test_explorer_forms.py b/django_lostplaces/lostplaces/tests/forms/test_explorer_forms.py new file mode 100644 index 0000000..89b6986 --- /dev/null +++ b/django_lostplaces/lostplaces/tests/forms/test_explorer_forms.py @@ -0,0 +1,59 @@ +import datetime + +from django import forms +from django.utils import timezone + +from lostplaces.tests.forms import FormTestCase +from lostplaces.forms import ExplorerCreationForm +from lostplaces.models.models import Voucher + +class ExplorerCreationFormTestCase(FormTestCase): + """ + This test case only tests for the voucher since all other aspects don't realy matter + to this project and are already tested by django + """ + form = ExplorerCreationForm + + @classmethod + def setUpTestData(cls): + Voucher.objects.create( + code='Imacode123', + expires_when=timezone.now() + datetime.timedelta(minutes=1) + ) + + def setUp(self): + self.post_data = { + 'voucher': 'Imacode123', + 'username': 'testpeter', + 'email': 'testpeter@example.org', + 'password1': 'Develop123', + 'password2': 'Develop123' + } + + def test_voucher_field(self): + self.assertField( + field_name='voucher', + field_class=forms.CharField + ) + + def test_validation_valid(self): + form = ExplorerCreationForm(self.post_data) + self.assertTrue( + form.is_valid(), + msg='Expecting the %s to validate' % ( + self.form.__name__ + ) + ) + + def test_validation_invalid(self): + self.post_data = { + 'voucher': 'Imanotacode123' + } + form = ExplorerCreationForm(self.post_data) + self.assertFalse( + form.is_valid(), + msg='Expecting the %s to not validate' % ( + self.form.__name__ + ) + ) + \ No newline at end of file