1 | import json |
---|
2 | from unittest.mock import Mock |
---|
3 | from datetime import datetime, timedelta, timezone |
---|
4 | from decimal import Decimal |
---|
5 | |
---|
6 | import pytest |
---|
7 | from repoze.catalog.catalog import Catalog |
---|
8 | |
---|
9 | from ow.models.workout import Workout |
---|
10 | from ow.models.user import User |
---|
11 | from ow.models.root import OpenWorkouts |
---|
12 | |
---|
13 | |
---|
14 | class TestRootOpenWorkouts(object): |
---|
15 | |
---|
16 | @pytest.fixture |
---|
17 | def john(self): |
---|
18 | user = User(firstname='John', lastname='Doe', |
---|
19 | email='john.doe@example.net') |
---|
20 | user.password = 's3cr3t' |
---|
21 | return user |
---|
22 | |
---|
23 | @pytest.fixture |
---|
24 | def root(self, john): |
---|
25 | root = OpenWorkouts() |
---|
26 | root.add_user(john) |
---|
27 | workout = Workout( |
---|
28 | start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc), |
---|
29 | duration=timedelta(minutes=60), |
---|
30 | distance=30, sport='cycling' |
---|
31 | ) |
---|
32 | john.add_workout(workout) |
---|
33 | return root |
---|
34 | |
---|
35 | def test__init__(self, root): |
---|
36 | # a new OpenWorkouts instance has a catalog created automatically |
---|
37 | assert isinstance(root.catalog, Catalog) |
---|
38 | assert len(root.catalog) == 4 |
---|
39 | for key in ['email', 'nickname', 'sport', 'hashed']: |
---|
40 | assert key in root.catalog |
---|
41 | |
---|
42 | def test_update_indexes(self, root): |
---|
43 | indexes = sorted([i for i in root.catalog]) |
---|
44 | assert indexes == ['email', 'hashed', 'nickname', 'sport'] |
---|
45 | # remove one index |
---|
46 | del root.catalog['email'] |
---|
47 | indexes = sorted([i for i in root.catalog]) |
---|
48 | assert indexes == ['hashed', 'nickname', 'sport'] |
---|
49 | # now update indexes, the index will be back there |
---|
50 | root._update_indexes() |
---|
51 | indexes = sorted([i for i in root.catalog]) |
---|
52 | assert indexes == ['email', 'hashed', 'nickname', 'sport'] |
---|
53 | |
---|
54 | def test_add_user_ok(self, root): |
---|
55 | assert len(root.users) == 1 |
---|
56 | user = User(firstname='New', lastname='For Testing', |
---|
57 | email='new.for.testing@example.net') |
---|
58 | root.add_user(user) |
---|
59 | assert len(root.users) == 2 |
---|
60 | assert user in root.users |
---|
61 | |
---|
62 | def test_add_user_invalid(self, root): |
---|
63 | assert len(root.users) == 1 |
---|
64 | with pytest.raises(AttributeError): |
---|
65 | root.add_user('faked-user-object') |
---|
66 | |
---|
67 | def test_del_user_ok(self, root, john): |
---|
68 | assert len(root.users) == 1 |
---|
69 | root.del_user(john) |
---|
70 | assert len(root.users) == 0 |
---|
71 | |
---|
72 | def test_del_user_failure(self, root): |
---|
73 | assert len(root.users) == 1 |
---|
74 | with pytest.raises(AttributeError): |
---|
75 | root.add_user('faked-user-object') |
---|
76 | |
---|
77 | def test_get_user_by_uid(self, root, john): |
---|
78 | # first, get an user that does exist |
---|
79 | user = root.get_user_by_uid(str(john.uid)) |
---|
80 | assert user == john |
---|
81 | # now, without converting first to str, works too |
---|
82 | user = root.get_user_by_uid(john.uid) |
---|
83 | assert user == john |
---|
84 | # now, something that is not there |
---|
85 | new_user = User(firstname='someone', lastname='else', |
---|
86 | email='someone.else@example.net') |
---|
87 | user = root.get_user_by_uid(new_user.uid) |
---|
88 | assert user is None |
---|
89 | # now, something that is not an uid |
---|
90 | user = root.get_user_by_uid('faked-user-uid') |
---|
91 | assert user is None |
---|
92 | |
---|
93 | def test_get_user_by_email(self, root, john): |
---|
94 | # first, get an user that does exist |
---|
95 | user = root.get_user_by_email(str(john.email)) |
---|
96 | assert user == john |
---|
97 | # now, something that is not there |
---|
98 | new_user = User(firstname='someone', lastname='else', |
---|
99 | email='someone.else@example.net') |
---|
100 | user = root.get_user_by_email(new_user.email) |
---|
101 | assert user is None |
---|
102 | # now, something that is not an email |
---|
103 | user = root.get_user_by_email('faked-user-email') |
---|
104 | assert user is None |
---|
105 | # passing in None |
---|
106 | user = root.get_user_by_email(None) |
---|
107 | assert user is None |
---|
108 | # passing in something that is not None or a string will break |
---|
109 | # the query code |
---|
110 | with pytest.raises(TypeError): |
---|
111 | user = root.get_user_by_email(False) |
---|
112 | with pytest.raises(TypeError): |
---|
113 | user = root.get_user_by_email(Mock()) |
---|
114 | |
---|
115 | def test_get_user_by_nickname(self, root, john): |
---|
116 | # set a nickname for john |
---|
117 | john.nickname = 'JohnDoe' |
---|
118 | root.reindex(john) |
---|
119 | user = root.get_user_by_nickname('JohnDoe') |
---|
120 | assert user == john |
---|
121 | user = root.get_user_by_nickname('NonExistant') |
---|
122 | assert user is None |
---|
123 | # passing in None |
---|
124 | user = root.get_user_by_nickname(None) |
---|
125 | assert user is None |
---|
126 | # passing in something that is not None or a string will break |
---|
127 | # the query code |
---|
128 | with pytest.raises(TypeError): |
---|
129 | user = root.get_user_by_nickname(False) |
---|
130 | with pytest.raises(TypeError): |
---|
131 | user = root.get_user_by_nickname(Mock()) |
---|
132 | |
---|
133 | def test_users(self, root, john): |
---|
134 | assert root.users == [john] |
---|
135 | |
---|
136 | def test_all_nicknames(self, root, john): |
---|
137 | # the existing user has not a nickname, and empty nicknames are not |
---|
138 | # added to the list of nicknames |
---|
139 | assert root.all_nicknames == [] |
---|
140 | # now set one |
---|
141 | john.nickname = 'MrJohn' |
---|
142 | assert root.all_nicknames == ['MrJohn'] |
---|
143 | |
---|
144 | def test_lowercase_nicknames(self, root, john): |
---|
145 | # the existing user has not a nickname |
---|
146 | assert root.lowercase_nicknames == [] |
---|
147 | # now set one |
---|
148 | john.nickname = 'MrJohn' |
---|
149 | assert root.lowercase_nicknames == ['mrjohn'] |
---|
150 | |
---|
151 | def test_emails(self, root): |
---|
152 | assert root.emails == ['john.doe@example.net'] |
---|
153 | |
---|
154 | def test_lowercase_emails(self, root): |
---|
155 | user = User(firstname='Jack', lastname='Dumb', |
---|
156 | email='Jack.Dumb@example.net') |
---|
157 | root.add_user(user) |
---|
158 | assert root.lowercase_emails == ['john.doe@example.net', |
---|
159 | 'jack.dumb@example.net'] |
---|
160 | |
---|
161 | def test_sports(self, root, john): |
---|
162 | assert root.sports == ['cycling'] |
---|
163 | workout = Workout( |
---|
164 | start=datetime(2015, 6, 29, 12, 55, tzinfo=timezone.utc), |
---|
165 | duration=timedelta(minutes=60), |
---|
166 | distance=10, sport='running') |
---|
167 | john.add_workout(workout) |
---|
168 | assert root.sports == ['cycling', 'running'] |
---|
169 | |
---|
170 | def test_sports_json(self, root, john): |
---|
171 | assert root.sports_json == json.dumps(["cycling"]) |
---|
172 | workout = Workout( |
---|
173 | start=datetime(2015, 6, 29, 12, 55, tzinfo=timezone.utc), |
---|
174 | duration=timedelta(minutes=60), |
---|
175 | distance=10, sport='running') |
---|
176 | john.add_workout(workout) |
---|
177 | assert root.sports_json == json.dumps(["cycling", "running"]) |
---|
178 | |
---|
179 | def test_get_workout_by_hash(self, root, john): |
---|
180 | # non existant hash |
---|
181 | found = root.get_workout_by_hash('non-existant-hash') |
---|
182 | assert found is None |
---|
183 | # existing workout |
---|
184 | workout = root[str(john.uid)]['1'] |
---|
185 | found = root.get_workout_by_hash(workout.hashed) |
---|
186 | assert found == workout |
---|
187 | # a workout that was not added to the system |
---|
188 | workout = Workout( |
---|
189 | start_time=datetime.now(timezone.utc), |
---|
190 | duration=timedelta(seconds=3600), |
---|
191 | distance=Decimal(30) |
---|
192 | ) |
---|
193 | found = root.get_workout_by_hash(workout.hashed) |
---|
194 | assert found is None |
---|