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_add_user_ok(self, root): |
---|
42 | assert len(root.users) == 1 |
---|
43 | user = User(firstname='New', lastname='For Testing', |
---|
44 | email='new.for.testing@example.net') |
---|
45 | root.add_user(user) |
---|
46 | assert len(root.users) == 2 |
---|
47 | assert user in root.users |
---|
48 | |
---|
49 | def test_add_user_invalid(self, root): |
---|
50 | assert len(root.users) == 1 |
---|
51 | with pytest.raises(AttributeError): |
---|
52 | root.add_user('faked-user-object') |
---|
53 | |
---|
54 | def test_del_user_ok(self, root, john): |
---|
55 | assert len(root.users) == 1 |
---|
56 | root.del_user(john) |
---|
57 | assert len(root.users) == 0 |
---|
58 | |
---|
59 | def test_del_user_failure(self, root): |
---|
60 | assert len(root.users) == 1 |
---|
61 | with pytest.raises(AttributeError): |
---|
62 | root.add_user('faked-user-object') |
---|
63 | |
---|
64 | def test_get_user_by_uid(self, root, john): |
---|
65 | # first, get an user that does exist |
---|
66 | user = root.get_user_by_uid(str(john.uid)) |
---|
67 | assert user == john |
---|
68 | # now, without converting first to str, works too |
---|
69 | user = root.get_user_by_uid(john.uid) |
---|
70 | assert user == john |
---|
71 | # now, something that is not there |
---|
72 | new_user = User(firstname='someone', lastname='else', |
---|
73 | email='someone.else@example.net') |
---|
74 | user = root.get_user_by_uid(new_user.uid) |
---|
75 | assert user is None |
---|
76 | # now, something that is not an uid |
---|
77 | user = root.get_user_by_uid('faked-user-uid') |
---|
78 | assert user is None |
---|
79 | |
---|
80 | def test_get_user_by_email(self, root, john): |
---|
81 | # first, get an user that does exist |
---|
82 | user = root.get_user_by_email(str(john.email)) |
---|
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_email(new_user.email) |
---|
88 | assert user is None |
---|
89 | # now, something that is not an email |
---|
90 | user = root.get_user_by_email('faked-user-email') |
---|
91 | assert user is None |
---|
92 | # passing in None |
---|
93 | user = root.get_user_by_email(None) |
---|
94 | assert user is None |
---|
95 | # passing in something that is not None or a string will break |
---|
96 | # the query code |
---|
97 | with pytest.raises(TypeError): |
---|
98 | user = root.get_user_by_email(False) |
---|
99 | with pytest.raises(TypeError): |
---|
100 | user = root.get_user_by_email(Mock()) |
---|
101 | |
---|
102 | def test_users(self, root, john): |
---|
103 | assert root.users == [john] |
---|
104 | |
---|
105 | def test_all_nicknames(self, root, john): |
---|
106 | # the existing user has not a nickname, and empty nicknames are not |
---|
107 | # added to the list of nicknames |
---|
108 | assert root.all_nicknames == [] |
---|
109 | # now set one |
---|
110 | john.nickname = 'MrJohn' |
---|
111 | assert root.all_nicknames == ['MrJohn'] |
---|
112 | |
---|
113 | def test_lowercase_nicknames(self, root, john): |
---|
114 | # the existing user has not a nickname |
---|
115 | assert root.lowercase_nicknames == [] |
---|
116 | # now set one |
---|
117 | john.nickname = 'MrJohn' |
---|
118 | assert root.lowercase_nicknames == ['mrjohn'] |
---|
119 | |
---|
120 | def test_emails(self, root): |
---|
121 | assert root.emails == ['john.doe@example.net'] |
---|
122 | |
---|
123 | def test_lowercase_emails(self, root): |
---|
124 | user = User(firstname='Jack', lastname='Dumb', |
---|
125 | email='Jack.Dumb@example.net') |
---|
126 | root.add_user(user) |
---|
127 | assert root.lowercase_emails == ['john.doe@example.net', |
---|
128 | 'jack.dumb@example.net'] |
---|
129 | |
---|
130 | def test_sports(self, root, john): |
---|
131 | assert root.sports == ['cycling'] |
---|
132 | workout = Workout( |
---|
133 | start=datetime(2015, 6, 29, 12, 55, tzinfo=timezone.utc), |
---|
134 | duration=timedelta(minutes=60), |
---|
135 | distance=10, sport='running') |
---|
136 | john.add_workout(workout) |
---|
137 | assert root.sports == ['cycling', 'running'] |
---|
138 | |
---|
139 | def test_sports_json(self, root, john): |
---|
140 | assert root.sports_json == json.dumps(["cycling"]) |
---|
141 | workout = Workout( |
---|
142 | start=datetime(2015, 6, 29, 12, 55, tzinfo=timezone.utc), |
---|
143 | duration=timedelta(minutes=60), |
---|
144 | distance=10, sport='running') |
---|
145 | john.add_workout(workout) |
---|
146 | assert root.sports_json == json.dumps(["cycling", "running"]) |
---|