Testing Abstract classes

This commit is contained in:
reverend 2020-09-13 10:27:01 +02:00
parent 3780aa6cf1
commit e77edf18ac
3 changed files with 48 additions and 29 deletions

View File

@ -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\'' % (

View File

@ -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
)

View File

@ -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