Refactoring

This commit is contained in:
reverend 2020-09-13 19:12:32 +02:00
parent 27520c7ca4
commit 1fb71a172e
5 changed files with 113 additions and 99 deletions

View File

@ -33,47 +33,46 @@ class ModelTestCase(TestCase):
) )
self.assertEqual( self.assertEqual(
type(field), field_class, type(field), field_class,
msg='Expecting type of %s.%s to be %s' % ( msg='Expecting type of %s to be %s' % (
self.model.__name__, str(field),
field_name,
field_class.__name__ field_class.__name__
) )
) )
for key, value in must_have.items(): for key, value in must_have.items():
if value: if value:
self.assertEqual(getattr(field, key), value, self.assertEqual(
msg='Expeting %s.%s.%s to be \'%s\'' % ( getattr(field, key), value,
self.model.__name__, msg='Expeting the value of %s %s to be \'%s\'' % (
field_name, str(field),
key, key,
value value
) )
) )
else: else:
self.assertTrue(hasattr(field, key), self.assertTrue(
msg='Expeting %s.%s to have \'%s\'' % ( hasattr(field, key),
self.model.__name__, msg='Expeting %s to have \'%s\'' % (
field_name, str(field),
key key
) )
) )
for key, value in must_not_have.items(): for key, value in must_not_have.items():
if value: if value:
self.assertTrue(getattr(field, key) != value, self.assertTrue(
msg='Expeting %s.%s.%s to not be \'%s\'' % ( getattr(field, key) != value,
self.model.__name__, msg='Expeting the value of %s %s to not be \'%s\'' % (
field_name, str(field),
key, key,
value value
) )
) )
else: else:
self.assertFalse(hasattr(field, value), self.assertFalse(
msg='Expeting %s.%s to not have \'%s\'' % ( hasattr(field, value),
self.model.__name__, msg='Expeting %s to not have \'%s\'' % (
field_name, str(field),
key key
) )
) )
@ -89,9 +88,8 @@ class ModelTestCase(TestCase):
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),
msg='Expeting %s.%s field max_length to be in the range of %d and %d' % ( msg='Expeting %s max_length to be in the range of %d and %d' % (
self.model.__name__, str(field),
field_name,
min_length, min_length,
max_length max_length
) )
@ -111,17 +109,15 @@ class ModelTestCase(TestCase):
if min_value: if min_value:
self.assertTrue( self.assertTrue(
len(field.validators) >= 1, len(field.validators) >= 1,
msg='Expecting the first valiator of %s.%s to check the minimum' % ( msg='Expecting the first valiator of %s to check the minimum' % (
self.model.__name__, str(field)
field_name
) )
) )
self.assertEqual( self.assertEqual(
field.validators[0].limit_value, field.validators[0].limit_value,
min_value, min_value,
msg='Expecting the min value of %s.%s min to be at least %d' % ( msg='Expecting the min value of %s min to be at least %d' % (
self.model.__name__, str(field),
field_name,
min_value min_value
) )
) )
@ -131,40 +127,15 @@ class ModelTestCase(TestCase):
index += 1 index += 1
self.assertTrue( self.assertTrue(
len(field.validators) >= index+1, len(field.validators) >= index+1,
msg='Expecting the second valiator of %s.%s to check the maximum' % ( msg='Expecting the second valiator of %s to check the maximum' % (
self.model.__name__, str(field)
field_name
) )
) )
self.assertEqual( self.assertEqual(
field.validators[1].limit_value, field.validators[1].limit_value,
max_value, max_value,
msg='Expecting the max value of %s.%s min to be at most %d' % ( msg='Expecting the max value of %s min to be at most %d' % (
self.model.__name__, str(field),
field_name,
max_value max_value
) )
) )
class SubmittableTestCase(ModelTestCase):
related_name = None
nullable = False
def test_submitted_when(self):
submitted_when = self.assertField(
'submitted_when',
models.DateTimeField,
must_have={'auto_now_add': True}
)
def test_submitted_by(self):
submitted_by = self.assertField('submitted_by', models.ForeignKey)
if self.related_name:
self.assertEqual(
submitted_by.remote_field.related_name, self.related_name)
if self.nullable:
self.assertTrue(submitted_by.null,)
self.assertTrue(submitted_by.blank)
self.assertEqual(
submitted_by.remote_field.on_delete, models.SET_NULL)

View File

@ -4,7 +4,11 @@ from django.test import TestCase
from django.db import models 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 (
Taggable,
MapablePoint,
Submittable
)
from lostplaces_app.tests.models import ModelTestCase from lostplaces_app.tests.models import ModelTestCase
from taggit.managers import TaggableManager from taggit.managers import TaggableManager
@ -42,3 +46,35 @@ class MapablePointTestCase(ModelTestCase):
min_value=-180, min_value=-180,
max_value=180 max_value=180
) )
class SubmittableTestCase(ModelTestCase):
model = Submittable
def test_submitted_when(self):
self.assertField(
field_name='submitted_when',
field_class=models.DateTimeField,
must_have={'auto_now_add': True}
)
def test_submitted_by(self):
submitted_by = self.assertField(
field_name='submitted_by',
field_class=models.ForeignKey
)
self.assertEqual(
submitted_by.remote_field.related_name,
'%(class)s',
msg='Expecting the related_name of %s to be \'%%(class)s\', got %s' % (
str(submitted_by),
submitted_by.remote_field.related_name
)
)
self.assertTrue(
submitted_by.null,
msg='Expecting the sub'
)
self.assertTrue(submitted_by.blank)
self.assertEqual(
submitted_by.remote_field.on_delete, models.SET_NULL)

View File

@ -10,11 +10,11 @@ from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from lostplaces_app.models import PlaceImage, Place from lostplaces_app.models import PlaceImage, Place
from lostplaces_app.tests.models import SubmittableTestCase from lostplaces_app.tests.models import ModelTestCase
from easy_thumbnails.fields import ThumbnailerImageField from easy_thumbnails.fields import ThumbnailerImageField
class TestPlaceImage(SubmittableTestCase, TestCase): class PlaceImageTestCase(ModelTestCase):
model = PlaceImage model = PlaceImage
@classmethod @classmethod
@ -59,6 +59,9 @@ class TestPlaceImage(SubmittableTestCase, TestCase):
submitted_by=user.explorer submitted_by=user.explorer
) )
def setUp(self):
self.place_image = PlaceImage.objects.get(id=1)
def test_description(self): def test_description(self):
self.assertField('description', models.TextField) self.assertField('description', models.TextField)
@ -68,39 +71,37 @@ class TestPlaceImage(SubmittableTestCase, TestCase):
def test_place(self): def test_place(self):
field = self.assertField('place', models.ForeignKey) field = self.assertField('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='Expecting the deletion of %s to be cascading' % (
self.model_name, str(field)
'place',
self.model_name
) )
) )
self.assertEqual(field.remote_field.related_name, 'images', expected_related_name = 'placeimages'
msg='%s.%s related name should be images' % ( self.assertEqual(field.remote_field.related_name, expected_related_name,
self.model_name, msg='Expecting the related name of %s to be %s' % (
'place' str(field),
expected_related_name
) )
) )
def test_str(self): def test_str(self):
place_image = self.object self.assertTrue(self.place_image.place.name.lower() in str(self.place_image).lower(),
self.assertTrue(place_image.place.name.lower() in str(place_image).lower(),
msg='Expecting %s.__str__ to contain the name of the place' % ( msg='Expecting %s.__str__ to contain the name of the place' % (
self.model_name self.model.__name__
) )
) )
def test_change_filename(self): def test_change_filename(self):
path = self.object.filename.path path = self.place_image.filename.path
self.object.filename = os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg') self.place_image.filename = os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg')
self.object.save() self.place_image.save()
self.assertFalse( self.assertFalse(
os.path.isfile(path), os.path.isfile(path),
msg='Expecting the old file of an place_image to be deleteed when an place_image file is changed' msg='Expecting the old file of an place_image to be deleteed when an place_image file is changed'
) )
def test_deletion(self): def test_deletion(self):
path = self.object.filename.path path = self.place_image.filename.path
self.object.delete() self.place_image.delete()
self.assertFalse( self.assertFalse(
os.path.isfile(path), os.path.isfile(path),
msg='Expecting the file of an place_image to be deleteed when an place_image is deleted' msg='Expecting the file of an place_image to be deleteed when an place_image is deleted'

View File

@ -6,9 +6,9 @@ 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 from lostplaces_app.models import Place
from lostplaces_app.tests.models import SubmittableTestCase from lostplaces_app.tests.models import ModelTestCase
class PlaceTestCase(SubmittableTestCase, TestCase): class PlaceTestCase(ModelTestCase):
model = Place model = Place
related_name = 'places' related_name = 'places'
nullable = True nullable = True
@ -33,6 +33,9 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
place.save() place.save()
def setUp(self):
self.place = Place.objects.get(id=1)
def test_location(self): def test_location(self):
self.assertCharField( self.assertCharField(
field_name='location', field_name='location',
@ -68,12 +71,12 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
self.assertEqual(avg_latlon['latitude'], 5.5, self.assertEqual(avg_latlon['latitude'], 5.5,
msg='%s: average latitude missmatch' % ( msg='%s: average latitude missmatch' % (
self.model_name self.model.__name__
) )
) )
self.assertEqual(avg_latlon['longitude'], 14.5, self.assertEqual(avg_latlon['longitude'], 14.5,
msg='%s: average longitude missmatch' % ( msg='%s: average longitude missmatch' % (
self.model_name self.model.__name__
) )
) )
@ -86,12 +89,12 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
avg_latlon = Place.average_latlon([place]) avg_latlon = Place.average_latlon([place])
self.assertEqual(avg_latlon['latitude'], place.latitude, self.assertEqual(avg_latlon['latitude'], place.latitude,
msg='%s:(one place) average latitude missmatch' % ( msg='%s:(one place) average latitude missmatch' % (
self.model_name self.model.__name__
) )
) )
self.assertEqual(avg_latlon['longitude'], place.longitude, self.assertEqual(avg_latlon['longitude'], place.longitude,
msg='%s: (one place) average longitude missmatch' % ( msg='%s: (one place) average longitude missmatch' % (
self.model_name self.model.__name__
) )
) )
@ -103,19 +106,19 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
avg_latlon = Place.average_latlon([]) avg_latlon = Place.average_latlon([])
self.assertEqual(avg_latlon['latitude'], 0, self.assertEqual(avg_latlon['latitude'], 0,
msg='%s: (no places) average latitude missmatch' % ( msg='%s: (no places) average latitude missmatch' % (
self.model_name self.model.__name__
) )
) )
self.assertEqual(avg_latlon['longitude'], 0, self.assertEqual(avg_latlon['longitude'], 0,
msg='%s: a(no places) verage longitude missmatch' % ( msg='%s: a(no places) verage longitude missmatch' % (
self.model_name self.model.__name__
) )
) )
def test_str(self): def test_str(self):
place = self.object place = self.place
self.assertTrue(place.name.lower() in str(place).lower(), self.assertTrue(place.name.lower() in str(place).lower(),
msg='Expecting %s.__str__ to contain the name' % ( msg='Expecting %s.__str__ to contain the name' % (
self.model_name self.model.__name__
) )
) )

View File

@ -18,32 +18,35 @@ class VoucheTestCase(ModelTestCase):
expires_when=timezone.now() + datetime.timedelta(days=1) expires_when=timezone.now() + datetime.timedelta(days=1)
) )
def setUp(self):
self.voucher = Voucher.objects.get(id=1)
def test_voucher_code(self): def test_voucher_code(self):
self.assertCharField( self.assertCharField(
'code', field_name='code',
10, min_length=10,
100, max_length=100,
must_have={'unique': True} must_have={'unique': True}
) )
def test_voucher_created(self): def test_voucher_created(self):
self.assertField( self.assertField(
'created_when', field_name='created_when',
models.DateTimeField, field_class=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.assertField(
'expires_when', field_name='expires_when',
models.DateTimeField, field_class=models.DateTimeField,
must_not_have={'auto_now_add': True} must_not_have={'auto_now_add': True}
) )
def test_str(self): def test_str(self):
voucher = self.object self.assertTrue(
self.assertTrue(voucher.code.lower() in str(voucher).lower(), self.voucher.code.lower() in str(self.voucher).lower(),
msg='Expecting %s.__str__ to contain the voucher code' % ( msg='Expecting %s.__str__ to contain the voucher code' % (
self.model_name self.model.__name__
) )
) )