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