Testing place level and level acccuracy
This commit is contained in:
parent
a2ee323fa4
commit
624878624f
@ -1,13 +1,14 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import datetime
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from lostplaces.models import Place
|
from lostplaces.models import Place, PlaceVoting
|
||||||
from lostplaces.tests.models import ModelTestCase
|
from lostplaces.tests.models import ModelTestCase
|
||||||
|
|
||||||
class PlaceTestCase(ModelTestCase):
|
class PlaceTestCase(ModelTestCase):
|
||||||
@ -106,12 +107,12 @@ class PlaceTestCase(ModelTestCase):
|
|||||||
an empty list
|
an empty list
|
||||||
'''
|
'''
|
||||||
avg_latlon = Place.average_latlon([])
|
avg_latlon = Place.average_latlon([])
|
||||||
self.assertEqual(avg_latlon['latitude'], 0,
|
self.assertEqual(avg_latlon['latitude'], 51.961922091398904,
|
||||||
msg='%s: (no places) average latitude missmatch' % (
|
msg='%s: (no places) average latitude missmatch' % (
|
||||||
self.model.__name__
|
self.model.__name__
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertEqual(avg_latlon['longitude'], 0,
|
self.assertEqual(avg_latlon['longitude'], 7.6295628132604385,
|
||||||
msg='%s: (no places) average longitude missmatch' % (
|
msg='%s: (no places) average longitude missmatch' % (
|
||||||
self.model.__name__
|
self.model.__name__
|
||||||
)
|
)
|
||||||
@ -126,29 +127,167 @@ class PlaceTestCase(ModelTestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def test_level_calculation(self):
|
def test_level_calculation(self):
|
||||||
place = self.place
|
explorer = self.place.submitted_by
|
||||||
explorer = place.submitted_by
|
|
||||||
|
|
||||||
PlaceVoting.objects.create(
|
PlaceVoting.objects.create(
|
||||||
submitted_by=explorer,
|
submitted_by=explorer,
|
||||||
place=place,
|
place=self.place,
|
||||||
vote=5,
|
vote=5
|
||||||
expires_when=timezone.now()+self.delta
|
|
||||||
)
|
)
|
||||||
PlaceVoting.objects.create(
|
PlaceVoting.objects.create(
|
||||||
submitted_by=explorer,
|
submitted_by=explorer,
|
||||||
place=place,
|
place=self.place,
|
||||||
vote=2,
|
vote=2
|
||||||
expires_when=timezone.now()+self.delta
|
|
||||||
)
|
)
|
||||||
PlaceVoting.objects.create(
|
PlaceVoting.objects.create(
|
||||||
submitted_by=explorer,
|
submitted_by=explorer,
|
||||||
place=place,
|
place=self.place,
|
||||||
vote=4,
|
vote=4
|
||||||
expires_when=timezone.now()+self.delta
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.place.calculate_place_level()
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
4,
|
4,
|
||||||
place.calculate_place_level()
|
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'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user