Refactoring test_field

This commit is contained in:
reverend 2020-09-13 13:29:27 +02:00
parent f1c51ab8a7
commit 19299598c3
5 changed files with 12 additions and 12 deletions

View File

@ -17,7 +17,7 @@ class ModelTestCase:
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 _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 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.
@ -89,7 +89,7 @@ class ModelTestCase:
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._test_field( field = self.assertField(
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),
@ -110,7 +110,7 @@ class ModelTestCase:
[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._test_field( field = self.assertField(
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(
@ -156,14 +156,14 @@ class SubmittableTestCase(ModelTestCase):
nullable = False nullable = False
def test_submitted_when(self): def test_submitted_when(self):
submitted_when = self._test_field( submitted_when = self.assertField(
'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._test_field('submitted_by', models.ForeignKey) submitted_by = self.assertField('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

@ -15,7 +15,7 @@ class TaggableTestCase(ModelTestCase, TestCase):
model = Taggable model = Taggable
def test_tags(self): def test_tags(self):
self._test_field('tags', TaggableManager) self.assertField('tags', TaggableManager)
class MapablePointTestCase(ModelTestCase, TestCase): class MapablePointTestCase(ModelTestCase, TestCase):

View File

@ -60,13 +60,13 @@ class TestPlaceImage(SubmittableTestCase, TestCase):
) )
def test_description(self): def test_description(self):
self._test_field('description', models.TextField) self.assertField('description', models.TextField)
def test_filename(self): def test_filename(self):
self._test_field('filename',ThumbnailerImageField) self.assertField('filename',ThumbnailerImageField)
def test_place(self): 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, 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

@ -41,7 +41,7 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
) )
def test_decsription(self): def test_decsription(self):
self._test_field('description', models.TextField) self.assertField('description', models.TextField)
def test_average_latlon(self): def test_average_latlon(self):
''' '''

View File

@ -27,14 +27,14 @@ class VoucheTestCase(ModelTestCase, TestCase):
) )
def test_voucher_created(self): def test_voucher_created(self):
self._test_field( self.assertField(
'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._test_field( self.assertField(
'expires_when', 'expires_when',
models.DateTimeField, models.DateTimeField,
must_not_have={'auto_now_add': True} must_not_have={'auto_now_add': True}