test_user_repository.py
8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
# Cache global variables for timeouts
from default import Test, db
from nose.tools import assert_raises
from factories import UserFactory
from pybossa.repositories import UserRepository
from pybossa.exc import WrongObjectError, DBIntegrityError
class TestUserRepository(Test):
def setUp(self):
super(TestUserRepository, self).setUp()
self.user_repo = UserRepository(db)
def test_get_return_none_if_no_user(self):
"""Test get method returns None if there is no user with the
specified id"""
user = self.user_repo.get(200)
assert user is None, user
def test_get_returns_user(self):
"""Test get method returns a user if exists"""
user = UserFactory.create()
retrieved_user = self.user_repo.get(user.id)
assert user == retrieved_user, retrieved_user
def test_get_by_name_return_none_if_no_user(self):
"""Test get_by_name returns None when a user with the specified
name does not exist"""
user = self.user_repo.get_by_name('thisuserdoesnotexist')
assert user is None, user
def test_get_by_name_returns_the_user(self):
"""Test get_by_name returns a user if exists"""
user = UserFactory.create()
retrieved_user = self.user_repo.get_by_name(user.name)
assert user == retrieved_user, retrieved_user
def test_get_by(self):
"""Test get_by returns a user with the specified attribute"""
user = UserFactory.create(name='Jon Snow')
retrieved_user = self.user_repo.get_by(name=user.name)
assert user == retrieved_user, retrieved_user
def test_get_by_returns_none_if_no_user(self):
"""Test get_by returns None if no user matches the query"""
UserFactory.create(name='Tyrion Lannister')
user = self.user_repo.get_by(name='no_name')
assert user is None, user
def get_all_returns_list_of_all_users(self):
"""Test get_all returns a list of all the existing users"""
users = UserFactory.create_batch(3)
retrieved_users = self.user_repo.get_all()
assert isinstance(retrieved_users, list)
assert len(retrieved_users) == len(users), retrieved_users
for user in retrieved_users:
assert user in users, user
def test_filter_by_no_matches(self):
"""Test filter_by returns an empty list if no users match the query"""
UserFactory.create(name='reek', fullname='Theon Greyjoy')
retrieved_users = self.user_repo.filter_by(name='asha')
assert isinstance(retrieved_users, list)
assert len(retrieved_users) == 0, retrieved_users
def test_filter_by_one_condition(self):
"""Test filter_by returns a list of users that meet the filtering
condition"""
UserFactory.create_batch(3, locale='es')
should_be_missing = UserFactory.create(locale='fr')
retrieved_users = self.user_repo.filter_by(locale='es')
assert len(retrieved_users) == 3, retrieved_users
assert should_be_missing not in retrieved_users, retrieved_users
def test_filter_by_multiple_conditions(self):
"""Test filter_by supports multiple-condition queries"""
UserFactory.create_batch(2, locale='es', privacy_mode=True)
user = UserFactory.create(locale='es', privacy_mode=False)
retrieved_users = self.user_repo.filter_by(locale='es',
privacy_mode=False)
assert len(retrieved_users) == 1, retrieved_users
assert user in retrieved_users, retrieved_users
def test_filter_by_limit_offset(self):
"""Test that filter_by supports limit and offset options"""
UserFactory.create_batch(4)
all_users = self.user_repo.filter_by()
first_two = self.user_repo.filter_by(limit=2)
last_two = self.user_repo.filter_by(limit=2, offset=2)
assert len(first_two) == 2, first_two
assert len(last_two) == 2, last_two
assert first_two == all_users[:2]
assert last_two == all_users[2:]
def test_search_by_name_returns_list(self):
"""Test search_by_name returns a list with search results"""
search = self.user_repo.search_by_name('')
assert isinstance(search, list), search.__class__
def test_search_by_name(self):
"""Test search_by_name returns a list with the user if searching by
either its name or fullname"""
user = UserFactory.create(name='greenseer', fullname='Jojen Reed')
search_by_name = self.user_repo.search_by_name('greenseer')
search_by_fullname = self.user_repo.search_by_name('Jojen Reed')
assert user in search_by_name, search_by_name
assert user in search_by_fullname, search_by_fullname
def test_search_by_name_capital_lower_letters(self):
"""Test search_by_name works the same with capital or lower letters"""
user_capitals = UserFactory.create(name='JOJEN')
user_lowers = UserFactory.create(name='meera')
search_lower = self.user_repo.search_by_name('jojen')
search_capital = self.user_repo.search_by_name('MEERA')
assert user_capitals in search_lower, search_lower
assert user_lowers in search_capital, search_capital
def test_search_by_name_substrings(self):
"""Test search_by_name works when searching by a substring"""
user = UserFactory.create(name='Hodor')
search = self.user_repo.search_by_name('odo')
assert user in search, search
def test_search_by_name_empty_string(self):
"""Test search_by_name returns an empty list when searching by '' """
user = UserFactory.create(name='Brandon')
search = self.user_repo.search_by_name('')
assert len(search) == 0, search
def test_total_users_no_users(self):
"""Test total_users return 0 if there are no users"""
count = self.user_repo.total_users()
assert count == 0, count
def test_total_users_count(self):
"""Test total_users return 1 if there is one user"""
UserFactory.create()
count = self.user_repo.total_users()
assert count == 1, count
def test_save(self):
"""Test save persist the user"""
user = UserFactory.build()
assert self.user_repo.get(user.id) is None
self.user_repo.save(user)
assert self.user_repo.get(user.id) == user, "User not saved"
def test_save_fails_if_integrity_error(self):
"""Test save raises a DBIntegrityError if the instance to be saved lacks
a required value"""
user = UserFactory.build(name=None)
assert_raises(DBIntegrityError, self.user_repo.save, user)
def test_save_only_saves_users(self):
"""Test save raises a WrongObjectError when an object which is not
a User instance is saved"""
bad_object = dict()
assert_raises(WrongObjectError, self.user_repo.save, bad_object)
def test_update(self):
"""Test update persists the changes made to the user"""
user = UserFactory.create(locale='en')
user.locale = 'it'
self.user_repo.update(user)
updated_user = self.user_repo.get(user.id)
assert updated_user.locale == 'it', updated_user
def test_update_fails_if_integrity_error(self):
"""Test update raises a DBIntegrityError if the instance to be updated
lacks a required value"""
user = UserFactory.create()
user.name = None
assert_raises(DBIntegrityError, self.user_repo.update, user)
def test_update_only_updates_users(self):
"""Test update raises a WrongObjectError when an object which is not
a User instance is updated"""
bad_object = dict()
assert_raises(WrongObjectError, self.user_repo.update, bad_object)