test_compute.py 3.43 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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(
            datetime=when,
            kind='other',
            title='TEST',
            text='TEST',
            link='http://www.example.com',
            score=score,
            published=True
        )
        pos.save()
81 82
        pos.representatives.add(self.representative)
        pos.save()
Nicolas Joyard's avatar
Nicolas Joyard committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        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