Testing Abstract classes
This commit is contained in:
parent
3780aa6cf1
commit
e77edf18ac
@ -7,13 +7,14 @@ 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):
|
||||||
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__
|
self.model_name = self.model.__name__
|
||||||
|
|
||||||
def _test_field(self, field_name, field_class, must_have={}, must_not_have={}):
|
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)
|
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\'' % (
|
||||||
|
@ -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
|
||||||
|
)
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user