Changeset 1d92bf2 in OpenWorkouts-current for ow/tests/models/test_root.py
- Timestamp:
- Dec 16, 2018, 1:07:04 AM (5 years ago)
- Branches:
- current, feature/docs, master
- Children:
- 6560b8f
- Parents:
- 929097a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ow/tests/models/test_root.py
r929097a r1d92bf2 1 1 import json 2 from unittest.mock import Mock 2 3 from datetime import datetime, timedelta, timezone 3 4 4 5 import pytest 6 from repoze.catalog.catalog import Catalog 5 7 6 8 from ow.models.workout import Workout … … 12 14 13 15 @pytest.fixture 14 def root(self): 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): 15 24 root = OpenWorkouts() 16 root['john'] = User(firstname='John', lastname='Doe', 17 email='john.doe@example.net') 18 root['john'].password = 's3cr3t' 25 root.add_user(john) 19 26 workout = Workout( 20 27 start=datetime(2015, 6, 28, 12, 55, tzinfo=timezone.utc), … … 22 29 distance=30, sport='cycling' 23 30 ) 24 root['john'].add_workout(workout)31 john.add_workout(workout) 25 32 return root 26 33 27 def test_all_usernames(self, root): 28 # the method returns an OOBtree object 29 assert [u for u in root.all_usernames()] == ['john'] 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) == 2 38 assert 'email' in root.catalog 39 assert 'sport' in root.catalog 30 40 31 def test_lowercase_usernames(self, root): 32 root.add_user(uid='Jack', firstname='Jack', lastname='Dumb', 33 email='jack.dumb@example.net') 34 assert root.lowercase_usernames() == ['jack', 'john'] 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'] 35 119 36 120 def test_emails(self, root): 37 assert root.emails ()== ['john.doe@example.net']121 assert root.emails == ['john.doe@example.net'] 38 122 39 123 def test_lowercase_emails(self, root): 40 root.add_user(uid='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'] 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'] 44 129 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(uid='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): 130 def test_sports(self, root, john): 63 131 assert root.sports == ['cycling'] 64 132 workout = Workout( … … 66 134 duration=timedelta(minutes=60), 67 135 distance=10, sport='running') 68 root['john'].add_workout(workout)136 john.add_workout(workout) 69 137 assert root.sports == ['cycling', 'running'] 70 138 71 def test_sports_json(self, root ):139 def test_sports_json(self, root, john): 72 140 assert root.sports_json == json.dumps(["cycling"]) 73 141 workout = Workout( … … 75 143 duration=timedelta(minutes=60), 76 144 distance=10, sport='running') 77 root['john'].add_workout(workout)145 john.add_workout(workout) 78 146 assert root.sports_json == json.dumps(["cycling", "running"])
Note: See TracChangeset
for help on using the changeset viewer.