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