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,12 +7,13 @@ from django.test import TestCase
class ModelTestCase: class ModelTestCase:
''' '''
Base class for Lostplaces models Base class for ModelTests
''' '''
model = None model = None
model_name = None model_name = None
def setUp(self): def setUp(self):
if not self.model._meta.abstract:
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__
@ -26,7 +27,7 @@ class ModelTestCase:
something that fullfills value == False (i.e. '' or 0) something that fullfills value == False (i.e. '' or 0)
''' '''
try: try:
field = self.object._meta.get_field(field_name) field = self.model._meta.get_field(field_name)
except FieldDoesNotExist: except FieldDoesNotExist:
self.fail( self.fail(
'Expecting %s to have a field named \'%s\'' % ( '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.models import Place
from lostplaces_app.tests.models import SubmittableTestCase from lostplaces_app.tests.models import SubmittableTestCase
from taggit.managers import TaggableManager
class PlaceTestCase(SubmittableTestCase, TestCase): class PlaceTestCase(SubmittableTestCase, TestCase):
model = Place model = Place
related_name = 'places' related_name = 'places'
@ -35,12 +32,6 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
place.tags.add('I a tag', 'testlocation') place.tags.add('I a tag', 'testlocation')
place.save() place.save()
def test_name_field(self):
self._test_char_field(
field_name='name',
min_length=10,
max_length=100
)
def test_location(self): def test_location(self):
self._test_char_field( self._test_char_field(
@ -49,26 +40,9 @@ class PlaceTestCase(SubmittableTestCase, TestCase):
max_length=100 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): def test_decsription(self):
self._test_field('description', models.TextField) self._test_field('description', models.TextField)
def test_tags(self):
self._test_field('tags', TaggableManager)
def test_average_latlon(self): def test_average_latlon(self):
''' '''
Tests the average latitude/longitude calculation of a list Tests the average latitude/longitude calculation of a list