From e77edf18ac861c03bbf3cb59d68237997575796a Mon Sep 17 00:00:00 2001 From: reverend Date: Sun, 13 Sep 2020 10:27:01 +0200 Subject: [PATCH] Testing Abstract classes --- .../lostplaces_app/tests/models/__init__.py | 7 +-- .../tests/models/test_abstract_models.py | 44 +++++++++++++++++++ .../tests/models/test_place_model.py | 26 ----------- 3 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 lostplaces/lostplaces_app/tests/models/test_abstract_models.py diff --git a/lostplaces/lostplaces_app/tests/models/__init__.py b/lostplaces/lostplaces_app/tests/models/__init__.py index ca1b38b..ab59400 100644 --- a/lostplaces/lostplaces_app/tests/models/__init__.py +++ b/lostplaces/lostplaces_app/tests/models/__init__.py @@ -7,13 +7,14 @@ from django.test import TestCase class ModelTestCase: ''' - Base class for Lostplaces models + Base class for ModelTests ''' model = None model_name = None def setUp(self): - self.object = self.model.objects.get(id=1) + if not self.model._meta.abstract: + self.object = self.model.objects.get(id=1) self.model_name = self.model.__name__ def _test_field(self, field_name, field_class, must_have={}, must_not_have={}): @@ -26,7 +27,7 @@ class ModelTestCase: something that fullfills value == False (i.e. '' or 0) ''' try: - field = self.object._meta.get_field(field_name) + field = self.model._meta.get_field(field_name) except FieldDoesNotExist: self.fail( 'Expecting %s to have a field named \'%s\'' % ( diff --git a/lostplaces/lostplaces_app/tests/models/test_abstract_models.py b/lostplaces/lostplaces_app/tests/models/test_abstract_models.py new file mode 100644 index 0000000..802541f --- /dev/null +++ b/lostplaces/lostplaces_app/tests/models/test_abstract_models.py @@ -0,0 +1,44 @@ +import datetime + +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.tests.models import ModelTestCase + +from taggit.managers import TaggableManager + + +class TaggableTestCase(ModelTestCase, TestCase): + + model = Taggable + + def test_tags(self): + self._test_field('tags', TaggableManager) + + +class MapablePointTestCase(ModelTestCase, TestCase): + + model = MapablePoint + + def test_name(self): + self._test_char_field( + field_name='name', + min_length=10, + max_length=100 + ) + + def test_latitude(self): + self._test_float_field( + field_name='latitude', + min_value=-90, + max_value=90 + ) + + def test_longitude(self): + self._test_float_field( + field_name='longitude', + min_value=-180, + max_value=180 + ) diff --git a/lostplaces/lostplaces_app/tests/models/test_place_model.py b/lostplaces/lostplaces_app/tests/models/test_place_model.py index e5284da..e685c43 100644 --- a/lostplaces/lostplaces_app/tests/models/test_place_model.py +++ b/lostplaces/lostplaces_app/tests/models/test_place_model.py @@ -8,9 +8,6 @@ from django.contrib.auth.models import User from lostplaces_app.models import Place from lostplaces_app.tests.models import SubmittableTestCase - -from taggit.managers import TaggableManager - class PlaceTestCase(SubmittableTestCase, TestCase): model = Place related_name = 'places' @@ -35,12 +32,6 @@ class PlaceTestCase(SubmittableTestCase, TestCase): place.tags.add('I a tag', 'testlocation') place.save() - def test_name_field(self): - self._test_char_field( - field_name='name', - min_length=10, - max_length=100 - ) def test_location(self): self._test_char_field( @@ -49,26 +40,9 @@ class PlaceTestCase(SubmittableTestCase, TestCase): max_length=100 ) - def test_latitude(self): - self._test_float_field( - field_name='latitude', - min_value=-90, - max_value=90 - ) - - def test_longitude(self): - self._test_float_field( - field_name='longitude', - min_value=-180, - max_value=180 - ) - def test_decsription(self): self._test_field('description', models.TextField) - def test_tags(self): - self._test_field('tags', TaggableManager) - def test_average_latlon(self): ''' Tests the average latitude/longitude calculation of a list