source: OpenWorkouts-current/ow/tests/models/test_root.py @ 5ec3a0b

currentfeature/docs
Last change on this file since 5ec3a0b was 5ec3a0b, checked in by borja <borja@…>, 5 years ago

Imported sources from the old python2-only repository:

  • Modified the code so it is python 3.6 compatible
  • Fixed deprecation warnings, pyramid 1.10.x supported now
  • Fixed deprecation warnings about some libraries, like pyramid-simpleform
  • Added pytest-pycodestyle and pytest-flakes for automatic checks on the source code files when running tests.
  • Added default pytest.ini setup to enforce some default parameters when running tests.
  • Cleaned up the code a bit, catched up with tests coverage.
  • Property mode set to 100644
File size: 2.8 KB
Line 
1import json
2from datetime import datetime, timedelta, timezone
3
4import pytest
5
6from ow.models.workout import Workout
7from ow.models.user import User
8from ow.models.root import OpenWorkouts
9
10
11class 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"])
Note: See TracBrowser for help on using the repository browser.