From b055c5f89114ae32da68bd4ff85d175ec1fc0f51 Mon Sep 17 00:00:00 2001 From: reverend Date: Thu, 3 Sep 2020 22:22:04 +0200 Subject: [PATCH] Tests for place_image --- lostplaces/lostplaces_app/models.py | 1 + lostplaces/lostplaces_app/tests/__init__.py | 20 +-- .../lostplaces_app/tests/models/__init__.py | 121 ++++++++++++++++++ .../tests/models/test_place_image_model.py | 52 ++++++++ .../tests/models/test_place_model.py | 108 +--------------- 5 files changed, 182 insertions(+), 120 deletions(-) create mode 100644 lostplaces/lostplaces_app/tests/models/test_place_image_model.py diff --git a/lostplaces/lostplaces_app/models.py b/lostplaces/lostplaces_app/models.py index e3d5563..81244ad 100644 --- a/lostplaces/lostplaces_app/models.py +++ b/lostplaces/lostplaces_app/models.py @@ -108,6 +108,7 @@ class PlaceImage (models.Model): on_delete=models.CASCADE, related_name='images' ) + submitted_when = models.DateTimeField(auto_now_add=True, null=True) submitted_by = models.ForeignKey( Explorer, on_delete=models.SET_NULL, diff --git a/lostplaces/lostplaces_app/tests/__init__.py b/lostplaces/lostplaces_app/tests/__init__.py index 498b38b..d23299f 100644 --- a/lostplaces/lostplaces_app/tests/__init__.py +++ b/lostplaces/lostplaces_app/tests/__init__.py @@ -10,22 +10,4 @@ def mock_user(): password='Develop123' ) else: - 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) \ No newline at end of file + return explorer_list[0] \ No newline at end of file diff --git a/lostplaces/lostplaces_app/tests/models/__init__.py b/lostplaces/lostplaces_app/tests/models/__init__.py index e69de29..7538d94 100644 --- a/lostplaces/lostplaces_app/tests/models/__init__.py +++ b/lostplaces/lostplaces_app/tests/models/__init__.py @@ -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='' + 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) diff --git a/lostplaces/lostplaces_app/tests/models/test_place_image_model.py b/lostplaces/lostplaces_app/tests/models/test_place_image_model.py new file mode 100644 index 0000000..9093982 --- /dev/null +++ b/lostplaces/lostplaces_app/tests/models/test_place_image_model.py @@ -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' + ) + ) + diff --git a/lostplaces/lostplaces_app/tests/models/test_place_model.py b/lostplaces/lostplaces_app/tests/models/test_place_model.py index 4f719cf..7213266 100644 --- a/lostplaces/lostplaces_app/tests/models/test_place_model.py +++ b/lostplaces/lostplaces_app/tests/models/test_place_model.py @@ -4,7 +4,9 @@ from django.test import TestCase from django.db import models 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 @@ -22,106 +24,10 @@ def mock_place(): return place -class ModelTest: - ''' - 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): +class PlaceTestCase(TestSubmittable, TestCase): model_name = 'Place' + related_name = 'places' + nullable = True def setUp(self): self.place = mock_place() @@ -186,7 +92,7 @@ class PlaceTestCase(ModelTest, TestSubmittable, TestCase): ) def test_average_latlon_one_place(self): - ''' + ''' Tests the average latitude/longitude calculation of a list of one place '''