test_models.py 6.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from datetime import date, datetime, timedelta

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

from members.models import (Profile, Member, Membership)
from members.views import filter_users


class MemberBirthdayTest(TestCase):
    fixtures = ['members.json']

    def _make_date(self, date):
        return timezone.make_aware(datetime.strptime(date, '%Y-%m-%d'))

    def _get_members(self, start, end):
        start_date = self._make_date(start)
        end_date = self._make_date(end)
19
        return Member.current_members.with_birthdays_in_range(
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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
            start_date, end_date
        )

    def _assert_none(self, start, end):
        members = self._get_members(start, end)
        self.assertEqual(len(members), 0)

    def _assert_thom(self, start, end):
        members = self._get_members(start, end)
        self.assertEqual(len(members), 1)
        self.assertEqual(members[0].get_full_name(), 'Thom Wiggers')

    def test_one_year_contains_birthday(self):
        self._assert_thom('2016-03-02', '2016-08-08')

    def test_one_year_not_contains_birthday(self):
        self._assert_none('2016-01-01', '2016-02-01')

    def test_span_year_contains_birthday(self):
        self._assert_thom('2015-08-09', '2016-08-08')

    def test_span_year_not_contains_birthday(self):
        self._assert_none('2015-12-25', '2016-03-01')

    def test_span_multiple_years_contains_birthday(self):
        self._assert_thom('2012-12-31', '2016-01-01')

    def test_range_before_person_born(self):
        self._assert_none('1985-12-12', '1985-12-13')

    def test_person_born_in_range_in_one_year(self):
        self._assert_thom('1993-01-01', '1993-04-01')

    def test_person_born_in_range_spanning_one_year(self):
        self._assert_thom('1992-12-31', '1993-04-01')

    def test_person_born_in_range_spanning_multiple_years(self):
        self._assert_thom('1992-12-31', '1995-01-01')


class MemberTest(TestCase):
    fixtures = ['members.json']

    def test_has_been_member(self):
        member = Member.objects.get(pk=1)

        self.assertTrue(member.has_been_member())

        m1 = member.membership_set.all()[0]
        m1.type = 'honorary'
        m1.save()
        self.assertFalse(member.has_been_member())

    def test_has_been_honorary_member(self):
        member = Member.objects.get(pk=1)

        self.assertFalse(member.has_been_honorary_member())

        m1 = member.membership_set.all()[0]
        m1.type = 'honorary'
        m1.save()
        self.assertTrue(member.has_been_honorary_member())


class MemberDisplayNameTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        cls.member = Member.objects.create(
            username='johnnytest',
            first_name='',
            last_name=''
        )
        cls.profile = Profile.objects.create(
            user_id=cls.member.pk,
            initials=None,
            nickname=None,
            display_name_preference='full',
        )

    def setUp(self):
        self.profile.display_name_preference = 'full'
        # Assuming we always have a first and last name
        self.profile.user.first_name = 'Johnny'
        self.profile.user.last_name = 'Test'
        self.profile.nickname = None
        self.profile.initials = None

    def test_check_display_name_full(self):
        self.assertEqual('Johnny Test', self.profile.display_name())
        self.assertEqual('Johnny', self.profile.short_display_name())

    def test_check_display_name_nickname(self):
        self.profile.display_name_preference = 'nickname'
        self.assertEqual('Johnny Test', self.profile.display_name())
        self.assertEqual('Johnny', self.profile.short_display_name())
        self.profile.nickname = 'John'
        self.assertEqual('John', self.profile.display_name())
        self.assertEqual('John', self.profile.short_display_name())

    def test_check_display_name_firstname(self):
        self.profile.display_name_preference = 'firstname'
        self.assertEqual('Johnny', self.profile.display_name())
        self.assertEqual('Johnny', self.profile.short_display_name())

    def test_check_display_name_initials(self):
        self.profile.display_name_preference = 'initials'
        self.assertEqual('Test', self.profile.display_name())
        self.assertEqual('Test', self.profile.short_display_name())
        self.profile.initials = 'J'
        self.assertEqual('J Test', self.profile.display_name())
        self.assertEqual('J Test', self.profile.short_display_name())

    def test_check_display_name_fullnick(self):
        self.profile.display_name_preference = 'fullnick'
        self.assertEqual('Johnny Test', self.profile.display_name())
        self.assertEqual('Johnny', self.profile.short_display_name())
        self.profile.nickname = 'John'
        self.assertEqual('Johnny \'John\' Test', self.profile.display_name())
        self.assertEqual('Johnny', self.profile.short_display_name())

    def test_check_display_name_nicklast(self):
        self.profile.display_name_preference = 'nicklast'
        self.assertEqual('Johnny Test', self.profile.display_name())
        self.assertEqual('Johnny', self.profile.short_display_name())
        self.profile.nickname = 'John'
        self.assertEqual('\'John\' Test', self.profile.display_name())
        self.assertEqual('John', self.profile.short_display_name())


class MembershipFilterTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        # Add 10 members with default membership
        members = [Member(id=i, username=i) for i in range(7)]
        Member.objects.bulk_create(members)
        profiles = [Profile(user_id=i) for i in range(7)]
        Profile.objects.bulk_create(profiles)

        Membership(user_id=0, type='honorary',
                   until=date.today() + timedelta(days=1)).save()

        Membership(user_id=1, type='supporter',
                   until=date.today() + timedelta(days=1)).save()

        Membership(user_id=2, type='member',
                   until=date.today() + timedelta(days=1)).save()

        Membership(user_id=3, type='member',
                   until=date.today() + timedelta(days=1)).save()
        Membership(user_id=3, type='member',
                   until=date.today() - timedelta(days=365*10)).save()

        Membership(user_id=4, type='supporter',
                   until=date.today() + timedelta(days=1)).save()
        Membership(user_id=4, type='member',
                   until=date.today() - timedelta(days=365*10)).save()

        Membership(user_id=5, type='member',
                   until=date.today() - timedelta(days=365*10)).save()

        # user_id=6 has no memberships at all

    def test_honorary(self):
        members = filter_users('honor', '', [date.today().year])
        self.assertEqual(len(members), 1)
        self.assertEqual(members[0].id, 0)

    def test_ex(self):
        members = filter_users('ex', '', [date.today().year])
        self.assertEqual(len(members), 3)
        for member in members:
            self.assertIn(member.id, {4, 5, 6})

    # TODO more tests for other cases