test_decay_function.py 1.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
from datetime import date, timedelta

from django import test
from django.db import connection


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)
        )