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

155 lines
4.4 KiB
Python
Raw Normal View History

2020-09-19 22:50:07 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2020-09-03 20:07:11 +02:00
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
from django.utils import timezone
2020-09-11 23:07:19 +02:00
from lostplaces.models import Place
from lostplaces.tests.models import ModelTestCase
2020-09-03 20:07:11 +02:00
2020-09-13 19:12:32 +02:00
class PlaceTestCase(ModelTestCase):
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=timezone.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
2020-09-13 19:12:32 +02:00
def setUp(self):
self.place = Place.objects.get(id=1)
2020-09-03 20:07:11 +02:00
def test_location(self):
2020-09-13 13:30:11 +02:00
self.assertCharField(
2020-09-03 20:07:11 +02:00
field_name='location',
min_length=10,
max_length=100
)
2020-09-21 21:37:28 +02:00
def test_description(self):
2020-09-13 13:29:27 +02:00
self.assertField('description', models.TextField)
2020-09-03 20:07:11 +02:00
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' % (
2020-09-13 19:12:32 +02:00
self.model.__name__
2020-09-03 20:07:11 +02:00
)
)
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' % (
2020-09-13 19:12:32 +02:00
self.model.__name__
2020-09-03 20:07:11 +02:00
)
)
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' % (
2020-09-13 19:12:32 +02:00
self.model.__name__
2020-09-03 20:07:11 +02:00
)
)
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' % (
2020-09-13 19:12:32 +02:00
self.model.__name__
2020-09-03 20:07:11 +02:00
)
)
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' % (
2020-09-13 19:12:32 +02:00
self.model.__name__
2020-09-03 20:07:11 +02:00
)
)
2020-09-12 11:34:49 +02:00
self.assertEqual(avg_latlon['longitude'], 0,
2020-09-21 21:37:28 +02:00
msg='%s: (no places) average longitude missmatch' % (
2020-09-13 19:12:32 +02:00
self.model.__name__
2020-09-03 20:07:11 +02:00
)
)
def test_str(self):
2020-09-13 19:12:32 +02:00
place = self.place
2020-09-11 12:09:34 +02:00
self.assertTrue(place.name.lower() in str(place).lower(),
msg='Expecting %s.__str__ to contain the name' % (
2020-09-13 19:12:32 +02:00
self.model.__name__
2020-09-03 20:07:11 +02:00
)
)
def test_level_calculation(self):
place = self.place
explorer = place.submitted_by
PlaceVoting.objects.create(
submitted_by=explorer,
place=place,
vote=5,
expires_when=timezone.now()+self.delta
)
PlaceVoting.objects.create(
submitted_by=explorer,
place=place,
vote=2,
expires_when=timezone.now()+self.delta
)
PlaceVoting.objects.create(
submitted_by=explorer,
place=place,
vote=4,
expires_when=timezone.now()+self.delta
)
self.assertEqual(
4,
place.calculate_place_level()
)