Compare commits

..

No commits in common. "c78ff60231c877643264570929cb785483dd6e8f" and "b77c5d1d7f16f6dc7754555029f7320782fc24f9" have entirely different histories.

6 changed files with 33 additions and 36 deletions

View File

@ -5,7 +5,7 @@ from django.test import TestCase
# Creating a test user # Creating a test user
class ModelTestCaseMixin: class ModelTestCase:
''' '''
Base class for ModelTests Base class for ModelTests
''' '''
@ -17,7 +17,7 @@ class ModelTestCaseMixin:
self.object = self.model.objects.get(id=1) self.object = self.model.objects.get(id=1)
self.model_name = self.model.__name__ self.model_name = self.model.__name__
def assertField(self, field_name, field_class, must_have={}, must_not_have={}): def _test_field(self, field_name, field_class, must_have={}, must_not_have={}):
''' '''
Tests if a field exists under the given name and Tests if a field exists under the given name and
if the field is of the right type. if the field is of the right type.
@ -84,12 +84,12 @@ class ModelTestCaseMixin:
return field return field
def assertCharField(self, field_name, min_length, max_length, must_have={}, must_hot_have={}): def _test_char_field(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 Tests if the given field is a char field and if its max_length
is in min_length and max_legth is in min_length and max_legth
''' '''
field = self.assertField( field = self._test_field(
field_name, models.CharField, must_have, must_hot_have) field_name, models.CharField, must_have, must_hot_have)
self.assertTrue( self.assertTrue(
field.max_length in range(min_length, max_length), field.max_length in range(min_length, max_length),
@ -101,7 +101,7 @@ class ModelTestCaseMixin:
) )
) )
def assertFloatField(self, field_name, min_value=None, max_value=None, must_have={}, must_hot_have={}): def _test_float_field(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, 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 the validators of the field are also checked. The validator list of the field should
@ -110,7 +110,7 @@ class ModelTestCaseMixin:
[MinValueValidator] if only min_value is passed, [MinValueValidator] if only min_value is passed,
[MaxValueValidator] if only max_value is passed [MaxValueValidator] if only max_value is passed
''' '''
field = self.assertField( field = self._test_field(
field_name, models.FloatField, must_have, must_hot_have) field_name, models.FloatField, must_have, must_hot_have)
if min_value: if min_value:
self.assertTrue( self.assertTrue(
@ -151,19 +151,19 @@ class ModelTestCaseMixin:
) )
class SubmittableTestCase(ModelTestCaseMixin): class SubmittableTestCase(ModelTestCase):
related_name = None related_name = None
nullable = False nullable = False
def test_submitted_when(self): def test_submitted_when(self):
submitted_when = self.assertField( submitted_when = self._test_field(
'submitted_when', 'submitted_when',
models.DateTimeField, models.DateTimeField,
must_have={'auto_now_add': True} must_have={'auto_now_add': True}
) )
def test_submitted_by(self): def test_submitted_by(self):
submitted_by = self.assertField('submitted_by', models.ForeignKey) submitted_by = self._test_field('submitted_by', models.ForeignKey)
if self.related_name: if self.related_name:
self.assertEqual( self.assertEqual(
submitted_by.remote_field.related_name, self.related_name) 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 django.contrib.auth.models import User
from lostplaces_app.models import Place, Taggable, MapablePoint from lostplaces_app.models import Place, Taggable, MapablePoint
from lostplaces_app.tests.models import ModelTestCaseMixin from lostplaces_app.tests.models import ModelTestCase
from taggit.managers import TaggableManager from taggit.managers import TaggableManager
class TaggableTestCase(ModelTestCaseMixin, TestCase): class TaggableTestCase(ModelTestCase, TestCase):
model = Taggable model = Taggable
def test_tags(self): def test_tags(self):
self.assertField('tags', TaggableManager) self._test_field('tags', TaggableManager)
class MapablePointTestCase(ModelTestCaseMixin, TestCase): class MapablePointTestCase(ModelTestCase, TestCase):
model = MapablePoint model = MapablePoint
def test_name(self): def test_name(self):
self.assertCharField( self._test_char_field(
field_name='name', field_name='name',
min_length=10, min_length=10,
max_length=100 max_length=100
) )
def test_latitude(self): def test_latitude(self):
self.assertFloatField( self._test_float_field(
field_name='latitude', field_name='latitude',
min_value=-90, min_value=-90,
max_value=90 max_value=90
) )
def test_longitude(self): def test_longitude(self):
self.assertFloatField( self._test_float_field(
field_name='longitude', field_name='longitude',
min_value=-180, min_value=-180,
max_value=180 max_value=180

View File

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

View File

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

View File

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

View File

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