This repository was archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_db.py
88 lines (75 loc) · 4.05 KB
/
test_db.py
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
81
82
83
84
85
86
87
88
import unittest
import unittest.mock
from datetime import date, datetime
from member import db, errors
class TestDbMethods(unittest.TestCase):
@unittest.mock.patch.object(db, 'current_membership_expires')
def test_new_member_expiration(self, current_membership_expires):
"""New memberships start on the date of payment in Boston."""
current_membership_expires.return_value = None
test_cases = [
# Normal case: It's the same day in both EST and UTC
(datetime(2018, 5, 2, 6, 13), date(2018, 5, 2)),
# UTC -5 in February
(datetime(2018, 2, 2, 5, 57), date(2018, 2, 2)),
(datetime(2018, 2, 2, 4, 57), date(2018, 2, 1)),
# UTC -4 when DST is observed in the summer
(datetime(2018, 6, 2, 4, 57), date(2018, 6, 2)),
(datetime(2018, 6, 2, 3, 57), date(2018, 6, 1)),
]
for datetime_paid, expected in test_cases:
self.assertEqual(expected, db.membership_start(37, datetime_paid))
@unittest.mock.patch.object(db, 'current_membership_expires')
def test_renewed_near_end(self, current_membership_expires):
"""Renewing at the end of a membership adds 12 full months."""
# It's early January, and our membership expires in two weeks
datetime_paid = datetime(2018, 1, 1, 12, 47)
membership_expires_on = date(2018, 1, 15)
current_membership_expires.return_value = membership_expires_on
# The resulting membership lasts one year, starting after the last one
self.assertEqual(membership_expires_on, db.membership_start(37, datetime_paid))
@unittest.mock.patch.object(db, 'current_membership_expires')
def test_renewed_very_early(self, current_membership_expires):
"""Renewing too early adds 12 months from the date renewed."""
# In January, we're renewing a membership that expires in June
datetime_paid = datetime(2018, 1, 1, 12, 47)
date_paid_EST = date(2018, 1, 1)
current_membership_expires.return_value = date(2018, 6, 6)
# Our resulting membership will only be valid until next January
self.assertEqual(date_paid_EST, db.membership_start(37, datetime_paid))
@unittest.mock.patch.object(db, 'get_db')
def test_unknown_affiliation_given(self, get_db):
"""Make sure that the affiliation we see is valid."""
cursor = unittest.mock.Mock()
get_db.cursor.return_value = cursor
with self.assertRaises(errors.InvalidAffiliation):
db.add_membership(42, '20.00', datetime.now(), 'MX')
cursor.execute.assert_not_called()
@unittest.mock.patch.object(db, 'get_db')
def test_insufficient_payment_given(self, get_db):
"""Ensure that the payment received was the amount expected."""
cursor = unittest.mock.Mock()
get_db.cursor.return_value = cursor
with self.assertRaises(errors.IncorrectPayment):
db.add_membership(42, '1.23', datetime.now(), 'NA')
cursor.execute.assert_not_called()
@unittest.mock.patch.object(db, 'get_db')
def test_old_payment_values(self, get_db):
"""Ensure that the old annual dues are not processed."""
cursor = unittest.mock.Mock()
get_db.cursor.return_value = cursor
old_now_invalid = {
'MA': 20, # MIT affiliates: formerly $20, now $30
'NA': 25, # Non-affiliates: formerly $25, now $40
'ML': 25, # MIT alumni: formerly $25, now $40
'NU': 15, # Non-MIT undergrads: formerly $15, now $40
'NG': 15, # Non-MIT grad stuents: formerly $15, now $40
}
for code, price in old_now_invalid.items():
with self.assertRaises(errors.IncorrectPayment):
db.add_membership(42, f'{price}.00', datetime.now(), code)
cursor.execute.assert_not_called()
def test_updating_with_invalid_affiliation(self):
"""We only attempt to update people with valid affiliations."""
with self.assertRaises(ValueError):
db.update_affiliation(42, "Cousin of MIT alumni's brother")