148 lines
4.1 KiB
Python
148 lines
4.1 KiB
Python
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
|
|
from lostplaces_app.tests.models import SubmittableTestCase
|
|
|
|
|
|
from taggit.managers import TaggableManager
|
|
|
|
class PlaceTestCase(SubmittableTestCase, TestCase):
|
|
model = Place
|
|
related_name = 'places'
|
|
nullable = True
|
|
|
|
@classmethod
|
|
def setUpTestData(cls):
|
|
user = User.objects.create_user(
|
|
username='testpeter',
|
|
password='Develop123'
|
|
)
|
|
|
|
place = Place.objects.create(
|
|
name='Im a place',
|
|
submitted_when=datetime.datetime.now(),
|
|
submitted_by=user.explorer,
|
|
location='Testtown',
|
|
latitude=50.5,
|
|
longitude=7.0,
|
|
description='This is just a test, do not worry'
|
|
)
|
|
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(
|
|
field_name='location',
|
|
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
|
|
)
|
|
|
|
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
|
|
of 10 places
|
|
'''
|
|
place_list = []
|
|
for i in range(10):
|
|
place = Place.objects.get(id=1)
|
|
place.id = None
|
|
place.latitude = i+1
|
|
place.longitude = i+10
|
|
place.save()
|
|
place_list.append(place)
|
|
|
|
avg_latlon = Place.average_latlon(place_list)
|
|
|
|
self.assertTrue('latitude' in avg_latlon,
|
|
msg='Expecting avg_latlon dict to have an \'latitude\' key'
|
|
)
|
|
self.assertTrue('longitude' in avg_latlon,
|
|
msg='Expecting avg_latlon dict to have an \'longitude\' key'
|
|
)
|
|
|
|
self.assertEqual(avg_latlon['latitude'], 5.5,
|
|
msg='%s: average latitude missmatch' % (
|
|
self.model_name
|
|
)
|
|
)
|
|
self.assertEqual(avg_latlon['longitude'], 14.5,
|
|
msg='%s: average longitude missmatch' % (
|
|
self.model_name
|
|
)
|
|
)
|
|
|
|
def test_average_latlon_one_place(self):
|
|
'''
|
|
Tests the average latitude/longitude calculation of a list
|
|
of one place
|
|
'''
|
|
place = Place.objects.get(id=1)
|
|
avg_latlon = Place.average_latlon([place])
|
|
self.assertEqual(avg_latlon['latitude'], place.latitude,
|
|
msg='%s:(one place) average latitude missmatch' % (
|
|
self.model_name
|
|
)
|
|
)
|
|
self.assertEqual(avg_latlon['longitude'], place.longitude,
|
|
msg='%s: (one place) average longitude missmatch' % (
|
|
self.model_name
|
|
)
|
|
)
|
|
|
|
def test_average_latlon_no_places(self):
|
|
'''
|
|
Tests the average latitude/longitude calculation of
|
|
an empty list
|
|
'''
|
|
avg_latlon = Place.average_latlon([])
|
|
self.assertEqual(avg_latlon['latitude'], 0,
|
|
msg='%s: (no places) average latitude missmatch' % (
|
|
self.model_name
|
|
)
|
|
)
|
|
self.assertEqual(avg_latlon['longitude'], 0,
|
|
msg='%s: a(no places) verage longitude missmatch' % (
|
|
self.model_name
|
|
)
|
|
)
|
|
|
|
def test_str(self):
|
|
place = self.object
|
|
self.assertTrue(place.name.lower() in str(place).lower(),
|
|
msg='Expecting %s.__str__ to contain the name' % (
|
|
self.model_name
|
|
)
|
|
)
|