Compare commits

..

5 Commits

Author SHA1 Message Date
c78ff60231 Refactoring ModelTestCase 2020-09-13 13:31:41 +02:00
09eb8794b8 Refactoring test_float_field 2020-09-13 13:30:47 +02:00
470e54da8d Refactoring test_char_field 2020-09-13 13:30:11 +02:00
19299598c3 Refactoring test_field 2020-09-13 13:29:27 +02:00
f1c51ab8a7 Testing messages 2020-09-13 13:28:22 +02:00
6 changed files with 36 additions and 33 deletions

View File

@ -5,7 +5,7 @@ from django.test import TestCase
# Creating a test user
class ModelTestCase:
class ModelTestCaseMixin:
'''
Base class for ModelTests
'''
@ -17,7 +17,7 @@ class ModelTestCase:
self.object = self.model.objects.get(id=1)
self.model_name = self.model.__name__
def _test_field(self, field_name, field_class, must_have={}, must_not_have={}):
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.
@ -84,12 +84,12 @@ class ModelTestCase:
return field
def _test_char_field(self, field_name, min_length, max_length, must_have={}, must_hot_have={}):
def assertCharField(self, field_name, min_length, max_length, must_have={}, must_hot_have={}):
'''
Tests if the given field is a char field and if its max_length
is in min_length and max_legth
'''
field = self._test_field(
field = self.assertField(
field_name, models.CharField, must_have, must_hot_have)
self.assertTrue(
field.max_length in range(min_length, max_length),
@ -101,7 +101,7 @@ class ModelTestCase:
)
)
def _test_float_field(self, field_name, min_value=None, max_value=None, must_have={}, must_hot_have={}):
def assertFloatField(self, field_name, min_value=None, max_value=None, must_have={}, must_hot_have={}):
'''
Tests if the field is a floatfield. If min_value and/or max_value are passed,
the validators of the field are also checked. The validator list of the field should
@ -110,7 +110,7 @@ class ModelTestCase:
[MinValueValidator] if only min_value is passed,
[MaxValueValidator] if only max_value is passed
'''
field = self._test_field(
field = self.assertField(
field_name, models.FloatField, must_have, must_hot_have)
if min_value:
self.assertTrue(
@ -151,19 +151,19 @@ class ModelTestCase:
)
class SubmittableTestCase(ModelTestCase):
class SubmittableTestCase(ModelTestCaseMixin):
related_name = None
nullable = False
def test_submitted_when(self):
submitted_when = self._test_field(
submitted_when = self.assertField(
'submitted_when',
models.DateTimeField,
must_have={'auto_now_add': True}
)
def test_submitted_by(self):
submitted_by = self._test_field('submitted_by', models.ForeignKey)
submitted_by = self.assertField('submitted_by', models.ForeignKey)
if self.related_name:
self.assertEqual(
submitted_by.remote_field.related_name, self.related_name)

View File

@ -5,39 +5,39 @@ from django.db import models
from django.contrib.auth.models import User
from lostplaces_app.models import Place, Taggable, MapablePoint
from lostplaces_app.tests.models import ModelTestCase
from lostplaces_app.tests.models import ModelTestCaseMixin
from taggit.managers import TaggableManager
class TaggableTestCase(ModelTestCase, TestCase):
class TaggableTestCase(ModelTestCaseMixin, TestCase):
model = Taggable
def test_tags(self):
self._test_field('tags', TaggableManager)
self.assertField('tags', TaggableManager)
class MapablePointTestCase(ModelTestCase, TestCase):
class MapablePointTestCase(ModelTestCaseMixin, TestCase):
model = MapablePoint
def test_name(self):
self._test_char_field(
self.assertCharField(
field_name='name',
min_length=10,
max_length=100
)
def test_latitude(self):
self._test_float_field(
self.assertFloatField(
field_name='latitude',
min_value=-90,
max_value=90
)
def test_longitude(self):
self._test_float_field(
self.assertFloatField(
field_name='longitude',
min_value=-180,
max_value=180

View File

@ -60,13 +60,13 @@ class TestPlaceImage(SubmittableTestCase, TestCase):
)
def test_description(self):
self._test_field('description', models.TextField)
self.assertField('description', models.TextField)
def test_filename(self):
self._test_field('filename',ThumbnailerImageField)
self.assertField('filename',ThumbnailerImageField)
def test_place(self):
field = self._test_field('place', models.ForeignKey)
field = self.assertField('place', models.ForeignKey)
self.assertEqual(field.remote_field.on_delete, models.CASCADE,
msg='%s.%s deleting of %s should be cascadinf' % (
self.model_name,

View File

@ -34,14 +34,14 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
def test_location(self):
self._test_char_field(
self.assertCharField(
field_name='location',
min_length=10,
max_length=100
)
def test_decsription(self):
self._test_field('description', models.TextField)
self.assertField('description', models.TextField)
def test_average_latlon(self):
'''

View File

@ -5,10 +5,10 @@ from django.db import models
from django.utils import timezone
from lostplaces_app.models import Voucher
from lostplaces_app.tests.models import ModelTestCase
from lostplaces_app.tests.models import ModelTestCaseMixin
class VoucheTestCase(ModelTestCase, TestCase):
class VoucheTestCase(ModelTestCaseMixin, TestCase):
model = Voucher
@classmethod
@ -19,7 +19,7 @@ class VoucheTestCase(ModelTestCase, TestCase):
)
def test_voucher_code(self):
self._test_char_field(
self.assertCharField(
'code',
10,
100,
@ -27,14 +27,14 @@ class VoucheTestCase(ModelTestCase, TestCase):
)
def test_voucher_created(self):
self._test_field(
self.assertField(
'created_when',
models.DateTimeField,
must_have={'auto_now_add': True}
)
def test_voucher_expires(self):
self._test_field(
self.assertField(
'expires_when',
models.DateTimeField,
must_not_have={'auto_now_add': True}

View File

@ -7,8 +7,10 @@ from django.contrib.messages.storage.fallback import FallbackStorage
from lostplaces_app.models import Place
from lostplaces_app.views import IsAuthenticatedMixin
from lostplaces_app.tests.views import ViewTestCaseMixin
class TestIsAuthenticatedMixin(RedirectTestCase):
class TestIsAuthenticated(ViewTestCaseMixin, TestCase):
view = IsAuthenticatedMixin
@classmethod
def setUpTestData(cls):
@ -22,7 +24,8 @@ class TestIsAuthenticatedMixin(RedirectTestCase):
request.user = User.objects.get(id=1)
response = IsAuthenticatedMixin.as_view()(request)
self.assertEqual(response.status_code, 200)
# Expecting a 405 because IsAuthenticatedMixin has no 'get' method
self.assertHttpMethodNotAllowed(response)
def test_not_logged_in(self):
request = RequestFactory().get('/someurl1234')
@ -32,10 +35,10 @@ class TestIsAuthenticatedMixin(RedirectTestCase):
request._messages = messages
response = IsAuthenticatedMixin.as_view()(request)
self.assertRedirectsTo(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
self.assertHttpRedirect(response, '?'.join([str(reverse_lazy('login')), 'next=/someurl1234']))
self.assertTrue(response.context['messages'])
self.assertTrue(len(response.context['messages']) > 0)
response = self.client.get(response['Location'])
self.assertTrue(len(messages) > 0)
class TestIsPlaceSubmitterMixin(TestCase):