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

294 lines
8.3 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
import datetime
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, PlaceVoting
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([])
self.assertEqual(avg_latlon['latitude'], 51.961922091398904,
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
)
)
self.assertEqual(avg_latlon['longitude'], 7.6295628132604385,
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):
explorer = self.place.submitted_by
PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote=5
)
PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote=2
)
PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote=4
)
self.place.calculate_place_level()
self.assertEqual(
4,
self.place.level,
msg='Expecting the place level to be 4'
)
def test_level_calculation_no_votes(self):
self.place.calculate_place_level()
self.assertEqual(
5,
self.place.level,
msg='Expecting the default place level to be 5'
)
def test_level_mid_accuracy(self):
explorer = self.place.submitted_by
six_month_ago = datetime.timedelta(days=180)
self.place.submitted_when = timezone.now() - six_month_ago
votings = [
{
'date': timezone.now() - datetime.timedelta(days=170),
'vote': 5
},
{
'date': timezone.now() - datetime.timedelta(days=23),
'vote': 2
},
{
'date': timezone.now() - datetime.timedelta(days=1),
'vote': 4
}
]
for vote in votings:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote= vote['vote']
)
voting.submitted_when = vote['date']
voting.save()
self.assertEqual(
65,
self.place.calculate_voting_accuracy()
)
def test_level_high_accuracy(self):
explorer = self.place.submitted_by
six_month_ago = datetime.timedelta(days=180)
self.place.submitted_when = timezone.now() - six_month_ago
votings = [
{
'date': timezone.now() - datetime.timedelta(days=9),
'vote': 5
},
{
'date': timezone.now() - datetime.timedelta(days=14),
'vote': 2
},
{
'date': timezone.now() - datetime.timedelta(days=0),
'vote': 4
}
]
for vote in votings:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote= vote['vote']
)
voting.submitted_when = vote['date']
voting.save()
self.assertEqual(
96,
self.place.calculate_voting_accuracy()
)
def test_level_low_accuracy(self):
explorer = self.place.submitted_by
six_month_ago = datetime.timedelta(days=180)
self.place.submitted_when = timezone.now() - six_month_ago
votings = [
{
'date': timezone.now() - datetime.timedelta(days=177),
'vote': 5
},
{
'date': timezone.now() - datetime.timedelta(days=150),
'vote': 2
},
{
'date': timezone.now() - datetime.timedelta(days=100),
'vote': 4
}
]
for vote in votings:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote= vote['vote']
)
voting.submitted_when = vote['date']
voting.save()
self.assertEqual(
21,
self.place.calculate_voting_accuracy()
)
def test_level_accuracy_zero_timedelta(self):
explorer = self.place.submitted_by
six_month_ago = datetime.timedelta(days=180)
self.place.submitted_when = timezone.now() - six_month_ago
votings = [
{
'date': timezone.now() - datetime.timedelta(days=0),
'vote': 4
}
]
for vote in votings:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=self.place,
vote= vote['vote']
)
voting.submitted_when = vote['date']
voting.save()
self.assertEqual(
100,
self.place.calculate_voting_accuracy(),
msg='Expecting the accurcy to be 100% when the vote is 0 time units old'
)