Commit 7b1382a0 authored by Nicolas Joyard's avatar Nicolas Joyard

Add score tests

parent ae647ea6
[
{
"fields": {
"updated": "1970-01-01T00:00:00.000Z",
"last_name": "TEST",
"created": "1970-01-01T00:00:00.000Z",
"gender": 1,
"first_name": "Test",
"cv": "",
"active": true,
"birth_place": "Test",
"full_name": "Test TEST",
"photo": "http://www.europarl.europa.eu/mepphoto/test.jpg",
"birth_date": "1970-01-01",
"slug": "test-test-1970-01-01"
},
"model": "representatives.representative",
"pk": 1
},
{
"fields": {
"text": "",
"updated": "1970-01-01T00:00:00.000Z",
"title": "Test Dossier",
"reference": "TEST",
"created": "1970-01-01T00:00:00.000Z"
},
"model": "representatives_votes.dossier",
"pk": 1
},
{
"fields": {
"updated": "1970-01-01T00:00:00.000Z",
"total_for": 1,
"description": "",
"reference": "GOOD",
"title": "Good proposal",
"dossier": 1,
"created": "1970-01-01T00:00:00.000Z",
"kind": "",
"datetime": "1970-01-01T00:00:00.000Z",
"total_against": 0,
"total_abstain": 0
},
"model": "representatives_votes.proposal",
"pk": 1
},
{
"fields": {
"updated": "1970-01-01T00:00:00.000Z",
"total_for": 1,
"description": "",
"reference": "BAD",
"title": "Bad proposal",
"dossier": 1,
"created": "1970-01-01T00:00:00.000Z",
"kind": "",
"datetime": "1970-01-01T00:00:00.000Z",
"total_against": 0,
"total_abstain": 0
},
"model": "representatives_votes.proposal",
"pk": 2
},
{
"fields": {
"representative_name": "",
"position": "for",
"proposal": 1,
"representative": 1
},
"model": "representatives_votes.vote",
"pk": 1
},
{
"fields": {
"representative_name": "",
"position": "for",
"proposal": 2,
"representative": 1
},
"model": "representatives_votes.vote",
"pk": 2
}
]
\ No newline at end of file
from datetime import date, timedelta
from django import test
from memopol_scores.models import RepresentativeScore
from memopol_settings.models import Setting
from representatives.models import Representative
from representatives_positions.models import Position
from representatives_recommendations.models import Recommendation
from representatives_votes.models import Dossier, Proposal
class ComputeTest(test.TestCase):
fixtures = ['compute_score.json']
@property
def representative(self):
return Representative.objects.get(last_name='TEST')
@property
def dossier(self):
return Dossier.objects.get(reference='TEST')
@property
def good_proposal(self):
return Proposal.objects.get(dossier=self.dossier, reference='GOOD')
@property
def bad_proposal(self):
return Proposal.objects.get(dossier=self.dossier, reference='BAD')
def refresh(self):
RepresentativeScore.refresh()
def set_decay(self, days, exponent, decimals=0):
params = {
"SCORE_DECAY_NUM": 1,
"SCORE_DECAY_DENOM": days,
"SCORE_EXPONENT": exponent,
"SCORE_DECIMALS": decimals
}
for k, v in params.iteritems():
setting = Setting.objects.get(key=k)
setting.value = '%s' % v
setting.save()
def set_good_recommendation(self, weight):
rec = Recommendation(
proposal=self.good_proposal,
recommendation='for',
title='Good',
description='Good',
weight=weight
)
rec.save()
return rec
def set_bad_recommendation(self, weight):
rec = Recommendation(
proposal=self.bad_proposal,
recommendation='against',
title='Bad',
description='Bad',
weight=weight
)
rec.save()
return rec
def create_position(self, when, score):
pos = Position(
representative=self.representative,
datetime=when,
kind='other',
title='TEST',
text='TEST',
link='http://www.example.com',
score=score,
published=True
)
pos.save()
return pos
def test_no_score(self):
self.refresh()
assert self.representative.representative_score.score == 0
def test_good_vote_score(self):
self.set_good_recommendation(100)
self.refresh()
assert self.representative.representative_score.score == 100
def test_bad_vote_score(self):
self.set_bad_recommendation(100)
self.refresh()
assert self.representative.representative_score.score == -100
def test_decay(self):
proposal = self.good_proposal
proposal.datetime = date.today() - timedelta(365)
proposal.save()
self.set_good_recommendation(100)
self.set_decay(365, 1)
self.refresh()
assert self.representative.representative_score.score == 37
def test_position(self):
self.create_position(date.today(), 100)
self.refresh()
assert self.representative.representative_score.score == 100
def test_total(self):
self.set_good_recommendation(100)
self.set_bad_recommendation(10)
self.create_position(date.today(), 1)
self.refresh()
assert self.representative.representative_score.score == 100 - 10 + 1
from datetime import date, timedelta
from django import test
from django.db import connection
from memopol_settings.models import Setting
class DecayFunctionTest(test.TestCase):
def decay_score(self, score, date, num, denom, exponent, decimals):
with connection.cursor() as cursor:
cursor.execute('SELECT decay_score(%s, %s, %s, %s, %s, %s);',
[score, date, num, denom, exponent, decimals])
return float([row[0] for row in cursor.fetchall()][0])
def do_decay_test(self, num, denom, exp, decs, days, scores):
def decayed(score, date):
return self.decay_score(score, date, num, denom, exp, decs)
for d, s in zip(days, scores):
assert decayed(100, date.today() - timedelta(d)) == s
def test_no_decay(self):
self.do_decay_test(
0, 1, 1, 0,
(0, 31, 355, 375, 730, 3650),
(100, 100, 100, 100, 100, 100)
)
def test_normal_decay(self):
self.do_decay_test(
1, 365, 1, 0,
(0, 31, 355, 375, 730, 3650),
(100, 99, 39, 35, 2, 0)
)
def test_decimals(self):
self.do_decay_test(
1, 365, 1, 2,
(0, 31, 355, 375, 730, 3650),
(100, 99.28, 38.83, 34.8, 1.83, 0)
)
def test_extreme_decay(self):
self.do_decay_test(
1, 365, 100, 0,
(0, 31, 355, 375, 730, 3650),
(100, 100, 100, 0, 0, 0)
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment