[5ec3a0b] | 1 | import json |
---|
| 2 | from datetime import datetime, timedelta, timezone |
---|
| 3 | |
---|
| 4 | import pytest |
---|
| 5 | |
---|
| 6 | from ow.models.workout import Workout |
---|
| 7 | from ow.models.user import User |
---|
| 8 | from ow.models.root import OpenWorkouts |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | class TestRootOpenWorkouts(object): |
---|
| 12 | |
---|
| 13 | @pytest.fixture |
---|
| 14 | def root(self): |
---|
| 15 | root = OpenWorkouts() |
---|
| 16 | root['john'] = User(firstname='John', lastname='Doe', |
---|
| 17 | email='john.doe@example.net') |
---|
| 18 | root['john'].password = 's3cr3t' |
---|
| 19 | workout = Workout( |
---|
| 20 | start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc), |
---|
| 21 | duration=timedelta(minutes=60), |
---|
| 22 | distance=30, sport='cycling' |
---|
| 23 | ) |
---|
| 24 | root['john'].add_workout(workout) |
---|
| 25 | return root |
---|
| 26 | |
---|
| 27 | def test_all_usernames(self, root): |
---|
| 28 | # the method returns an OOBtree object |
---|
| 29 | assert [u for u in root.all_usernames()] == ['john'] |
---|
| 30 | |
---|
| 31 | def test_lowercase_usernames(self, root): |
---|
| 32 | root.add_user(user_id='Jack', firstname='Jack', lastname='Dumb', |
---|
| 33 | email='jack.dumb@example.net') |
---|
| 34 | assert root.lowercase_usernames() == ['jack', 'john'] |
---|
| 35 | |
---|
| 36 | def test_emails(self, root): |
---|
| 37 | assert root.emails() == ['john.doe@example.net'] |
---|
| 38 | |
---|
| 39 | def test_lowercase_emails(self, root): |
---|
| 40 | root.add_user(user_id='Jack', firstname='Jack', lastname='Dumb', |
---|
| 41 | email='Jack.Dumb@example.net') |
---|
| 42 | assert root.lowercase_emails() == ['jack.dumb@example.net', |
---|
| 43 | 'john.doe@example.net'] |
---|
| 44 | |
---|
| 45 | def test_users(self, root): |
---|
| 46 | # the method returns an OOBtree object |
---|
| 47 | assert [u for u in root.users()] == [root['john']] |
---|
| 48 | |
---|
| 49 | def test_get_user(self, root): |
---|
| 50 | assert root.get_user('john') == root['john'] |
---|
| 51 | assert root.get_user('jack') is None |
---|
| 52 | with pytest.raises(TypeError): |
---|
| 53 | root.get_user() |
---|
| 54 | |
---|
| 55 | def test_add_user(self, root): |
---|
| 56 | assert len(root.users()) == 1 |
---|
| 57 | root.add_user(user_id='jack', firstname='Jack', lastname='Dumb', |
---|
| 58 | email='jack.dumb@example.net') |
---|
| 59 | assert len(root.users()) == 2 |
---|
| 60 | assert 'jack' in root.all_usernames() |
---|
| 61 | |
---|
| 62 | def test_sports(self, root): |
---|
| 63 | assert root.sports == ['cycling'] |
---|
| 64 | workout = Workout( |
---|
| 65 | start=datetime(2015, 6, 29, 12, 55, tzinfo=timezone.utc), |
---|
| 66 | duration=timedelta(minutes=60), |
---|
| 67 | distance=10, sport='running') |
---|
| 68 | root['john'].add_workout(workout) |
---|
| 69 | assert root.sports == ['cycling', 'running'] |
---|
| 70 | |
---|
| 71 | def test_sports_json(self, root): |
---|
| 72 | assert root.sports_json == json.dumps(["cycling"]) |
---|
| 73 | workout = Workout( |
---|
| 74 | start=datetime(2015, 6, 29, 12, 55, tzinfo=timezone.utc), |
---|
| 75 | duration=timedelta(minutes=60), |
---|
| 76 | distance=10, sport='running') |
---|
| 77 | root['john'].add_workout(workout) |
---|
| 78 | assert root.sports_json == json.dumps(["cycling", "running"]) |
---|