Tests for place_image
This commit is contained in:
parent
1dee72cd15
commit
b055c5f891
@ -108,6 +108,7 @@ class PlaceImage (models.Model):
|
|||||||
on_delete=models.CASCADE,
|
on_delete=models.CASCADE,
|
||||||
related_name='images'
|
related_name='images'
|
||||||
)
|
)
|
||||||
|
submitted_when = models.DateTimeField(auto_now_add=True, null=True)
|
||||||
submitted_by = models.ForeignKey(
|
submitted_by = models.ForeignKey(
|
||||||
Explorer,
|
Explorer,
|
||||||
on_delete=models.SET_NULL,
|
on_delete=models.SET_NULL,
|
||||||
|
@ -11,21 +11,3 @@ def mock_user():
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return explorer_list[0]
|
return explorer_list[0]
|
||||||
|
|
||||||
|
|
||||||
class TestSubmittable:
|
|
||||||
|
|
||||||
def test_submitted_when(self):
|
|
||||||
submitted_when = self.object._meta.get_field('submitted_when')
|
|
||||||
self.assertTrue(submitted_when)
|
|
||||||
self.assertEqual(type(submitted_when), django_models.DateTimeField)
|
|
||||||
self.assertTrue(submitted_when.auto_now_add)
|
|
||||||
|
|
||||||
def test_submitted_by(self):
|
|
||||||
submitted_by = self.object._meta.get_field('submitted_by')
|
|
||||||
self.assertTrue(submitted_by)
|
|
||||||
self.assertEqual(type(submitted_by), django_models.ForeignKey)
|
|
||||||
self.assertEqual(submitted_by.remote_field.related_name, 'places')
|
|
||||||
self.assertTrue(submitted_by.null)
|
|
||||||
self.assertTrue(submitted_by.blank)
|
|
||||||
self.assertEqual(submitted_by.remote_field.on_delete, django_models.SET_NULL)
|
|
@ -0,0 +1,121 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
class TestModel:
|
||||||
|
'''
|
||||||
|
Base class for Lostplaces models
|
||||||
|
'''
|
||||||
|
model_name = None
|
||||||
|
|
||||||
|
def _test_field(self, field_name, field_class):
|
||||||
|
'''
|
||||||
|
Tests if a field exists under the given name and
|
||||||
|
if the field is of the right type
|
||||||
|
'''
|
||||||
|
field = self.object._meta.get_field(field_name)
|
||||||
|
self.assertTrue(
|
||||||
|
field,
|
||||||
|
msg="%s has no field named '%s'" % (
|
||||||
|
self.model_name,
|
||||||
|
field_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
type(field), field_class,
|
||||||
|
msg='%s.%s name field is no CharField' % (
|
||||||
|
self.model_name,
|
||||||
|
field_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return field
|
||||||
|
|
||||||
|
|
||||||
|
def _test_char_field(self, field_name, min_length, max_length):
|
||||||
|
'''
|
||||||
|
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_name, models.CharField)
|
||||||
|
self.assertEqual(
|
||||||
|
type(field), models.CharField,
|
||||||
|
msg='%s.%s name field is no CharField' % (
|
||||||
|
self.model_name,
|
||||||
|
field_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
field.max_length in range(min_length, max_length),
|
||||||
|
msg='%s.%s field max_length not in range of %d and %d' % (
|
||||||
|
self.model_name,
|
||||||
|
field_name,
|
||||||
|
min_length,
|
||||||
|
max_length
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _test_float_field(self, field_name, min_value=None, max_value=None):
|
||||||
|
'''
|
||||||
|
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
|
||||||
|
look like
|
||||||
|
[MinValueValidator, MayValueValidator], if both values are passed,
|
||||||
|
[MinValueValidator] if only min_value is passed,
|
||||||
|
[MaxValueValidator] if only max_value is passed
|
||||||
|
'''
|
||||||
|
field = self._test_field(field_name, models.FloatField)
|
||||||
|
if min_value:
|
||||||
|
self.assertTrue(
|
||||||
|
len(field.validators) >= 1,
|
||||||
|
msg='%s.%s first validator should check minimum' % (
|
||||||
|
self.model_name,
|
||||||
|
field_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
field.validators[0].limit_value,
|
||||||
|
min_value,
|
||||||
|
msg='%s.%s min value missmatch' % (
|
||||||
|
self.model_name,
|
||||||
|
field_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if max_value:
|
||||||
|
index = 0
|
||||||
|
if min_value:
|
||||||
|
index += 1
|
||||||
|
self.assertTrue(
|
||||||
|
len(field.validators) >= index+1,
|
||||||
|
msg='%s.%s second validator should check maximum' % (
|
||||||
|
self.model_name,
|
||||||
|
field_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
field.validators[1].limit_value,
|
||||||
|
max_value,
|
||||||
|
msg='%s.%s max value missmatch' % (
|
||||||
|
self.model_name,
|
||||||
|
field_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
class TestSubmittable(TestModel):
|
||||||
|
model_name='<Class>'
|
||||||
|
related_name = None
|
||||||
|
nullable = False
|
||||||
|
|
||||||
|
def test_submitted_when(self):
|
||||||
|
submitted_when = self._test_field('submitted_when', models.DateTimeField)
|
||||||
|
self.assertTrue(submitted_when.auto_now_add,
|
||||||
|
msg='%s.submitted_when should be auto_now_add' % (
|
||||||
|
self.model_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_submitted_by(self):
|
||||||
|
submitted_by = self._test_field('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)
|
@ -0,0 +1,52 @@
|
|||||||
|
import datetime
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.db import models
|
||||||
|
from django.core.files import File
|
||||||
|
|
||||||
|
from lostplaces_app.models import PlaceImage
|
||||||
|
from lostplaces_app.tests.models import TestSubmittable
|
||||||
|
from lostplaces_app.tests import mock_user
|
||||||
|
from lostplaces_app.tests.models import TestModel
|
||||||
|
from lostplaces_app.tests.models.test_place_model import mock_place
|
||||||
|
|
||||||
|
from easy_thumbnails.fields import ThumbnailerImageField
|
||||||
|
|
||||||
|
def mock_place_image():
|
||||||
|
return PlaceImage(
|
||||||
|
description='Im a description',
|
||||||
|
filename=mock.MagicMock(spec=File, name='FileMock'),
|
||||||
|
place=mock_place(),
|
||||||
|
submitted_when=datetime.datetime.now(),
|
||||||
|
submitted_by=mock_user()
|
||||||
|
)
|
||||||
|
|
||||||
|
class TestPlaceImage(TestSubmittable, TestCase):
|
||||||
|
model_name = 'PlaceImage'
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.object = mock_place_image()
|
||||||
|
|
||||||
|
def test_description(self):
|
||||||
|
self._test_field('description', models.TextField)
|
||||||
|
|
||||||
|
def test_filename(self):
|
||||||
|
self._test_field('filename',ThumbnailerImageField)
|
||||||
|
|
||||||
|
def test_place(self):
|
||||||
|
field = self._test_field('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
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertEqual(field.remote_field.related_name, 'images',
|
||||||
|
msg='%s.%s related name should be images' % (
|
||||||
|
self.model_name,
|
||||||
|
'place'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
@ -4,7 +4,9 @@ from django.test import TestCase
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from lostplaces_app.models import Place
|
from lostplaces_app.models import Place
|
||||||
from lostplaces_app.tests import mock_user, TestSubmittable
|
from lostplaces_app.tests.models import TestSubmittable
|
||||||
|
from lostplaces_app.tests import mock_user
|
||||||
|
from lostplaces_app.tests.models import TestModel
|
||||||
|
|
||||||
from taggit.managers import TaggableManager
|
from taggit.managers import TaggableManager
|
||||||
|
|
||||||
@ -22,106 +24,10 @@ def mock_place():
|
|||||||
|
|
||||||
return place
|
return place
|
||||||
|
|
||||||
class ModelTest:
|
class PlaceTestCase(TestSubmittable, TestCase):
|
||||||
'''
|
|
||||||
Base class for Lostplaces models
|
|
||||||
'''
|
|
||||||
model_name = None
|
|
||||||
|
|
||||||
def _test_field(self, field_name, field_class):
|
|
||||||
'''
|
|
||||||
Tests if a field exists under the given name and
|
|
||||||
if the field is of the right type
|
|
||||||
'''
|
|
||||||
field = self.place._meta.get_field(field_name)
|
|
||||||
self.assertTrue(
|
|
||||||
field,
|
|
||||||
msg="%s has no field named '%s'" % (
|
|
||||||
self.model_name,
|
|
||||||
field_name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
type(field), field_class,
|
|
||||||
msg='%s.%s name field is no CharField' % (
|
|
||||||
self.model_name,
|
|
||||||
field_name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return field
|
|
||||||
|
|
||||||
|
|
||||||
def _test_char_field(self, field_name, min_length, max_length):
|
|
||||||
'''
|
|
||||||
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_name, models.CharField)
|
|
||||||
self.assertEqual(
|
|
||||||
type(field), models.CharField,
|
|
||||||
msg='%s.%s name field is no CharField' % (
|
|
||||||
self.model_name,
|
|
||||||
field_name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
self.assertTrue(
|
|
||||||
field.max_length in range(min_length, max_length),
|
|
||||||
msg='%s.%s field max_length not in range of %d and %d' % (
|
|
||||||
self.model_name,
|
|
||||||
field_name,
|
|
||||||
min_length,
|
|
||||||
max_length
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
def _test_float_field(self, field_name, min_value=None, max_value=None):
|
|
||||||
'''
|
|
||||||
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
|
|
||||||
look like
|
|
||||||
[MinValueValidator, MayValueValidator], if both values are passed,
|
|
||||||
[MinValueValidator] if only min_value is passed,
|
|
||||||
[MaxValueValidator] if only max_value is passed
|
|
||||||
'''
|
|
||||||
field = self._test_field(field_name, models.FloatField)
|
|
||||||
if min_value:
|
|
||||||
self.assertTrue(
|
|
||||||
len(field.validators) >= 1,
|
|
||||||
msg='%s.%s first validator should check minimum' % (
|
|
||||||
self.model_name,
|
|
||||||
field_name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
field.validators[0].limit_value,
|
|
||||||
min_value,
|
|
||||||
msg='%s.%s min value missmatch' % (
|
|
||||||
self.model_name,
|
|
||||||
field_name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if max_value:
|
|
||||||
index = 0
|
|
||||||
if min_value:
|
|
||||||
index += 1
|
|
||||||
self.assertTrue(
|
|
||||||
len(field.validators) >= index+1,
|
|
||||||
msg='%s.%s second validator should check maximum' % (
|
|
||||||
self.model_name,
|
|
||||||
field_name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
field.validators[1].limit_value,
|
|
||||||
max_value,
|
|
||||||
msg='%s.%s max value missmatch' % (
|
|
||||||
self.model_name,
|
|
||||||
field_name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
class PlaceTestCase(ModelTest, TestSubmittable, TestCase):
|
|
||||||
model_name = 'Place'
|
model_name = 'Place'
|
||||||
|
related_name = 'places'
|
||||||
|
nullable = True
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.place = mock_place()
|
self.place = mock_place()
|
||||||
|
Loading…
Reference in New Issue
Block a user