lostplaces-backend/lostplaces/lostplaces_app/tests/models/test_place_model.py

137 lines
3.7 KiB
Python
Raw Normal View History

2020-09-03 20:07:11 +02:00
import datetime
from django.test import TestCase
from django.db import models
from lostplaces_app.models import Place
2020-09-03 22:22:04 +02:00
from lostplaces_app.tests.models import TestSubmittable
from lostplaces_app.tests import mock_user
from lostplaces_app.tests.models import TestModel
2020-09-03 20:07:11 +02:00
from taggit.managers import TaggableManager
def mock_place():
place = Place.objects.create(
name='Im a place',
submitted_when=datetime.datetime.now(),
submitted_by=mock_user().explorer,
2020-09-03 20:07:11 +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')
return place
2020-09-03 22:22:04 +02:00
class PlaceTestCase(TestSubmittable, TestCase):
2020-09-03 20:07:11 +02:00
model_name = 'Place'
2020-09-03 22:22:04 +02:00
related_name = 'places'
nullable = True
2020-09-03 20:07:11 +02:00
def setUp(self):
self.place = mock_place()
self.object = self.place
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):
place = mock_place()
place.latitude = i+1
place.longitude = i+10
place_list.append(place)
avg_latlon = Place.average_latlon(place_list)
self.assertEqual(avg_latlon[0], 5.5,
msg='%s: average latitude missmatch' % (
self.model_name
)
)
self.assertEqual(avg_latlon[1], 14.5,
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-03 20:07:11 +02:00
place = mock_place()
avg_latlon = Place.average_latlon([place])
self.assertEqual(avg_latlon[0], place.latitude,
msg='%s:(one place) average latitude missmatch' % (
self.model_name
)
)
self.assertEqual(avg_latlon[1], place.longitude,
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([])
self.assertEqual(avg_latlon[0], 0,
msg='%s: (no places) average latitude missmatch' % (
self.model_name
)
)
self.assertEqual(avg_latlon[1], 0,
msg='%s: a(no places) verage longitude missmatch' % (
self.model_name
)
)
def test_str(self):
place = mock_place()
self.assertEqual(str(place), place.name,
msg='%s __str__ should return the name' % (
self.model_name
)
)