test_services.py 5.51 KB
Newer Older
1
from datetime import timedelta, date
2
3
4
5

from django.test import TestCase
from django.utils import timezone

6
7
from members.models import Member, Membership, Profile
from members.services import gen_stats_year, gen_stats_member_type
8
from utils.snippets import datetime_to_lectureyear
9
10


11
12
class StatisticsTest(TestCase):

Thom Wiggers's avatar
Thom Wiggers committed
13
14
    @classmethod
    def setUpTestData(cls):
15
        # Add 10 members with default membership
16
17
        members = [Member(id=i, username=i) for i in range(10)]
        Member.objects.bulk_create(members)
Thom Wiggers's avatar
Thom Wiggers committed
18
19
20
        memberships = [Membership(user_id=i, type="member")
                       for i in range(10)]
        Membership.objects.bulk_create(memberships)
21
22
        profiles = [Profile(user_id=i) for i in range(10)]
        Profile.objects.bulk_create(profiles)
23
24

    def sum_members(self, members, type=None):
Thom Wiggers's avatar
Thom Wiggers committed
25
26
27
28
        if type is None:
            return sum(sum(i.values()) for i in members)
        else:
            return sum(map(lambda x: x[type], members))
29
30

    def sum_member_types(self, members):
Thom Wiggers's avatar
Thom Wiggers committed
31
        return sum(members.values())
32
33
34
35
36
37
38
39
40
41
42

    def test_gen_stats_year_no_members(self):
        member_types = ["member", "supporter", "honorary"]
        result = gen_stats_year(member_types)
        self.assertEqual(0, self.sum_members(result))

    def test_gen_stats_active(self):
        """
        Testing if active and non-active objects are counted correctly
        """
        member_types = ["member", "supporter", "honorary"]
43
        current_year = datetime_to_lectureyear(date.today())
44
45
46

        # Set start date to current year - 1:
        for m in Member.objects.all():
47
48
            m.profile.starting_year = current_year - 1
            m.profile.save()
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        result = gen_stats_year(member_types)
        self.assertEqual(10, self.sum_members(result))
        self.assertEqual(10, self.sum_members(result, "member"))

        result = gen_stats_member_type(member_types)
        self.assertEqual(10, self.sum_member_types(result))

        # Change one membership to supporter should decrease amount of members
        m = Membership.objects.all()[0]
        m.type = "supporter"
        m.save()

        result = gen_stats_year(member_types)
        self.assertEqual(10, self.sum_members(result))
        self.assertEqual(9, self.sum_members(result, "member"))
        self.assertEqual(1, self.sum_members(result, "supporter"))

        result = gen_stats_member_type(member_types)
        self.assertEqual(10, self.sum_member_types(result))
        self.assertEqual(9, result["member"])
        self.assertEqual(1, result["supporter"])

        # Same for honorary members
        m = Membership.objects.all()[1]
        m.type = "honorary"
        m.save()

        result = gen_stats_year(member_types)
        self.assertEqual(10, self.sum_members(result))
        self.assertEqual(8, self.sum_members(result, "member"))
        self.assertEqual(1, self.sum_members(result, "supporter"))
        self.assertEqual(1, self.sum_members(result, "honorary"))

        result = gen_stats_member_type(member_types)
        self.assertEqual(10, self.sum_member_types(result))
        self.assertEqual(8, result["member"])
        self.assertEqual(1, result["supporter"])
        self.assertEqual(1, result["honorary"])

        # Terminate one membership by setting end date to current_year -1,
        # should decrease total amount and total members
        m = Membership.objects.all()[2]
        m.until = timezone.now() - timedelta(days=365)
        m.save()
        result = gen_stats_year(member_types)
        self.assertEqual(9, self.sum_members(result))
        self.assertEqual(7, self.sum_members(result, "member"))
        self.assertEqual(1, self.sum_members(result, "supporter"))
        self.assertEqual(1, self.sum_members(result, "honorary"))

        result = gen_stats_member_type(member_types)
        self.assertEqual(9, self.sum_member_types(result))
        self.assertEqual(7, result["member"])
        self.assertEqual(1, result["supporter"])
        self.assertEqual(1, result["honorary"])

    def test_gen_stats_different_years(self):
        member_types = ["member", "supporter", "honorary"]
107
        current_year = datetime_to_lectureyear(date.today())
108

109
110
111
        # postgres does not define random access directly on unsorted querysets
        members = [member for member in Member.objects.all()]

112
        # one first year student
113
        m = members[0]
114
115
        m.profile.starting_year = current_year
        m.profile.save()
116
117

        # one second year student
118
        m = members[1]
119
120
        m.profile.starting_year = current_year - 1
        m.profile.save()
121
122
123
124

        # no third year students

        # one fourth year student
125
        m = members[2]
126
127
        m.profile.starting_year = current_year - 3
        m.profile.save()
128
129
130
131

        # no fifth year students

        # one >5 year student
132
        m = members[3]
133
134
        m.profile.starting_year = current_year - 5
        m.profile.save()
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

        # 4 active members
        result = gen_stats_year(member_types)
        self.assertEqual(4, self.sum_members(result))
        self.assertEqual(4, self.sum_members(result, "member"))

        # one first year student
        self.assertEqual(1, result[0]['member'])

        # one second year student
        self.assertEqual(1, result[1]['member'])

        # no third year students
        self.assertEqual(0, result[2]['member'])

        # one fourth year student
        self.assertEqual(1, result[3]['member'])

        # no fifth year students
        self.assertEqual(0, result[4]['member'])

        # one >5 year student
        self.assertEqual(1, result[5]['member'])