2020-09-03 20:07:11 +02:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.db import models
|
|
|
|
|
2020-09-11 23:07:19 +02:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
2020-09-03 20:07:11 +02:00
|
|
|
from lostplaces_app.models import Place
|
2020-09-11 12:09:34 +02:00
|
|
|
from lostplaces_app.tests.models import SubmittableTestCase
|
2020-09-03 20:07:11 +02:00
|
|
|
|
|
|
|
|
2020-09-11 23:07:19 +02:00
|
|
|
from taggit.managers import TaggableManager
|
2020-09-03 20:07:11 +02:00
|
|
|
|
2020-09-11 12:09:34 +02:00
|
|
|
class PlaceTestCase(SubmittableTestCase, TestCase):
|
2020-09-11 23:07:19 +02:00
|
|
|
model = Place
|
2020-09-03 22:22:04 +02:00
|
|
|
related_name = 'places'
|
|
|
|
nullable = True
|
2020-09-03 20:07:11 +02:00
|
|
|
|
2020-09-11 23:07:19 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2020-09-12 08:39:06 +02:00
|
|
|
user = User.objects.create_user(
|
|
|
|
username='testpeter',
|
|
|
|
password='Develop123'
|
|
|
|
)
|
|
|
|
|
2020-09-11 23:07:19 +02:00
|
|
|
place = Place.objects.create(
|
|
|
|
name='Im a place',
|
|
|
|
submitted_when=datetime.datetime.now(),
|
2020-09-12 08:39:06 +02:00
|
|
|
submitted_by=user.explorer,
|
2020-09-11 23:07:19 +02:00
|
|
|
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()
|
2020-09-03 20:07:11 +02:00
|
|
|
|
|
|
|
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):
|
2020-09-03 20:24:20 +02:00
|
|
|
'''
|
|
|
|
Tests the average latitude/longitude calculation of a list
|
|
|
|
of 10 places
|
|
|
|
'''
|
2020-09-03 20:07:11 +02:00
|
|
|
place_list = []
|
|
|
|
for i in range(10):
|
2020-09-12 08:39:06 +02:00
|
|
|
place = Place.objects.get(id=1)
|
|
|
|
place.id = None
|
2020-09-03 20:07:11 +02:00
|
|
|
place.latitude = i+1
|
|
|
|
place.longitude = i+10
|
2020-09-11 23:07:19 +02:00
|
|
|
place.save()
|
2020-09-03 20:07:11 +02:00
|
|
|
place_list.append(place)
|
|
|
|
|
|
|
|
avg_latlon = Place.average_latlon(place_list)
|
2020-09-12 11:34:49 +02:00
|
|
|
|
|
|
|
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,
|
2020-09-03 20:07:11 +02:00
|
|
|
msg='%s: average latitude missmatch' % (
|
|
|
|
self.model_name
|
|
|
|
)
|
|
|
|
)
|
2020-09-12 11:34:49 +02:00
|
|
|
self.assertEqual(avg_latlon['longitude'], 14.5,
|
2020-09-03 20:07:11 +02:00
|
|
|
msg='%s: average longitude missmatch' % (
|
|
|
|
self.model_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_average_latlon_one_place(self):
|
2020-09-03 22:22:04 +02:00
|
|
|
'''
|
2020-09-03 20:24:20 +02:00
|
|
|
Tests the average latitude/longitude calculation of a list
|
|
|
|
of one place
|
|
|
|
'''
|
2020-09-11 23:07:19 +02:00
|
|
|
place = Place.objects.get(id=1)
|
2020-09-03 20:07:11 +02:00
|
|
|
avg_latlon = Place.average_latlon([place])
|
2020-09-12 11:34:49 +02:00
|
|
|
self.assertEqual(avg_latlon['latitude'], place.latitude,
|
2020-09-03 20:07:11 +02:00
|
|
|
msg='%s:(one place) average latitude missmatch' % (
|
|
|
|
self.model_name
|
|
|
|
)
|
|
|
|
)
|
2020-09-12 11:34:49 +02:00
|
|
|
self.assertEqual(avg_latlon['longitude'], place.longitude,
|
2020-09-03 20:07:11 +02:00
|
|
|
msg='%s: (one place) average longitude missmatch' % (
|
|
|
|
self.model_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_average_latlon_no_places(self):
|
2020-09-03 20:24:20 +02:00
|
|
|
'''
|
|
|
|
Tests the average latitude/longitude calculation of
|
|
|
|
an empty list
|
|
|
|
'''
|
2020-09-03 20:07:11 +02:00
|
|
|
avg_latlon = Place.average_latlon([])
|
2020-09-12 11:34:49 +02:00
|
|
|
self.assertEqual(avg_latlon['latitude'], 0,
|
2020-09-03 20:07:11 +02:00
|
|
|
msg='%s: (no places) average latitude missmatch' % (
|
|
|
|
self.model_name
|
|
|
|
)
|
|
|
|
)
|
2020-09-12 11:34:49 +02:00
|
|
|
self.assertEqual(avg_latlon['longitude'], 0,
|
2020-09-03 20:07:11 +02:00
|
|
|
msg='%s: a(no places) verage longitude missmatch' % (
|
|
|
|
self.model_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_str(self):
|
2020-09-11 12:09:34 +02:00
|
|
|
place = self.object
|
|
|
|
self.assertTrue(place.name.lower() in str(place).lower(),
|
|
|
|
msg='Expecting %s.__str__ to contain the name' % (
|
2020-09-03 20:07:11 +02:00
|
|
|
self.model_name
|
|
|
|
)
|
|
|
|
)
|