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(
type(field), field_class,
msg='Expecting type of %s.%s to be %s' % (
self.model.__name__,
field_name,
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 %s.%s.%s to be \'%s\'' % (
self.model.__name__,
field_name,
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.%s to have \'%s\'' % (
self.model.__name__,
field_name,
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 %s.%s.%s to not be \'%s\'' % (
self.model.__name__,
field_name,
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.%s to not have \'%s\'' % (
self.model.__name__,
field_name,
self.assertFalse(
hasattr(field, value),
msg='Expeting %s to not have \'%s\'' % (
str(field),
key
)
)
@ -89,9 +88,8 @@ class ModelTestCase(TestCase):
field_name, models.CharField, must_have, must_hot_have)
self.assertTrue(
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' % (
self.model.__name__,
field_name,
msg='Expeting %s max_length to be in the range of %d and %d' % (
str(field),
min_length,
max_length
)
@ -111,17 +109,15 @@ class ModelTestCase(TestCase):
if min_value:
self.assertTrue(
len(field.validators) >= 1,
msg='Expecting the first valiator of %s.%s to check the minimum' % (
self.model.__name__,
field_name
msg='Expecting the first valiator of %s to check the minimum' % (
str(field)
)
)
self.assertEqual(
field.validators[0].limit_value,
min_value,
msg='Expecting the min value of %s.%s min to be at least %d' % (
self.model.__name__,
field_name,
msg='Expecting the min value of %s min to be at least %d' % (
str(field),
min_value
)
)
@ -131,40 +127,15 @@ class ModelTestCase(TestCase):
index += 1
self.assertTrue(
len(field.validators) >= index+1,
msg='Expecting the second valiator of %s.%s to check the maximum' % (
self.model.__name__,
field_name
msg='Expecting the second valiator of %s to check the maximum' % (
str(field)
)
)
self.assertEqual(
field.validators[1].limit_value,
max_value,
msg='Expecting the max value of %s.%s min to be at most %d' % (
self.model.__name__,
field_name,
msg='Expecting the max value of %s min to be at most %d' % (
str(field),
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.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 taggit.managers import TaggableManager
@ -42,3 +46,35 @@ class MapablePointTestCase(ModelTestCase):
min_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 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
class TestPlaceImage(SubmittableTestCase, TestCase):
class PlaceImageTestCase(ModelTestCase):
model = PlaceImage
@classmethod
@ -59,6 +59,9 @@ class TestPlaceImage(SubmittableTestCase, TestCase):
submitted_by=user.explorer
)
def setUp(self):
self.place_image = PlaceImage.objects.get(id=1)
def test_description(self):
self.assertField('description', models.TextField)
@ -68,39 +71,37 @@ class TestPlaceImage(SubmittableTestCase, TestCase):
def test_place(self):
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,
'place',
self.model_name
msg='Expecting the deletion of %s to be cascading' % (
str(field)
)
)
self.assertEqual(field.remote_field.related_name, 'images',
msg='%s.%s related name should be images' % (
self.model_name,
'place'
expected_related_name = 'placeimages'
self.assertEqual(field.remote_field.related_name, expected_related_name,
msg='Expecting the related name of %s to be %s' % (
str(field),
expected_related_name
)
)
def test_str(self):
place_image = self.object
self.assertTrue(place_image.place.name.lower() in str(place_image).lower(),
self.assertTrue(self.place_image.place.name.lower() in str(self.place_image).lower(),
msg='Expecting %s.__str__ to contain the name of the place' % (
self.model_name
self.model.__name__
)
)
def test_change_filename(self):
path = self.object.filename.path
self.object.filename = os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg')
self.object.save()
path = self.place_image.filename.path
self.place_image.filename = os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg')
self.place_image.save()
self.assertFalse(
os.path.isfile(path),
msg='Expecting the old file of an place_image to be deleteed when an place_image file is changed'
)
def test_deletion(self):
path = self.object.filename.path
self.object.delete()
path = self.place_image.filename.path
self.place_image.delete()
self.assertFalse(
os.path.isfile(path),
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 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
related_name = 'places'
nullable = True
@ -33,6 +33,9 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
place.save()
def setUp(self):
self.place = Place.objects.get(id=1)
def test_location(self):
self.assertCharField(
field_name='location',
@ -68,12 +71,12 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
self.assertEqual(avg_latlon['latitude'], 5.5,
msg='%s: average latitude missmatch' % (
self.model_name
self.model.__name__
)
)
self.assertEqual(avg_latlon['longitude'], 14.5,
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])
self.assertEqual(avg_latlon['latitude'], place.latitude,
msg='%s:(one place) average latitude missmatch' % (
self.model_name
self.model.__name__
)
)
self.assertEqual(avg_latlon['longitude'], place.longitude,
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([])
self.assertEqual(avg_latlon['latitude'], 0,
msg='%s: (no places) average latitude missmatch' % (
self.model_name
self.model.__name__
)
)
self.assertEqual(avg_latlon['longitude'], 0,
msg='%s: a(no places) verage longitude missmatch' % (
self.model_name
self.model.__name__
)
)
def test_str(self):
place = self.object
place = self.place
self.assertTrue(place.name.lower() in str(place).lower(),
msg='Expecting %s.__str__ to contain the name' % (
self.model_name
self.model.__name__
)
)

View File

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