-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_repository.py
462 lines (360 loc) · 12.9 KB
/
test_repository.py
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
"""Tests the integration between the repositories and their storage layer.
The tests are all the same for all the repositories. If you want to add a new one
add them to the cases.
"""
import logging
import os
from typing import Any, List
import pytest
from _pytest.logging import LogCaptureFixture
from py._path.local import LocalPath
from repository_orm import EntityNotFoundError, Repository
from ..cases import Entity, RepositoryTester
def test_apply_repository_creates_schema( # noqa: AAA01
database: Any,
empty_repo: Repository,
caplog: LogCaptureFixture,
repo_tester: RepositoryTester,
) -> None:
"""
Given: an empty repository.
When: the apply_migrations method is called.
Then: The migrations are applied as expected.
"""
caplog.set_level(logging.DEBUG)
repo_tester.apply_migrations(empty_repo) # type: ignore
repo_tester.assert_schema_exists(database, caplog)
def test_repository_handles_unexistent_database_file(
repo: Repository, tmpdir: LocalPath
) -> None:
"""
Given: A database url pointing to an inexistent file
When: the repository is initialized
Then: The object is well initialized, and the database file is created.
The FakeRepository should not create any file, as all entities are stored in
memory.
"""
database_url = str(tmpdir.join("inexistent.db")) # type: ignore
result = repo.__class__(database_url)
assert isinstance(result, repo.__class__)
if result.__class__.__name__ != "FakeRepository":
assert os.path.isfile(database_url)
def test_repository_handles_connection_errors(repo: Repository) -> None:
"""
Given: A database url pointing to an inexistent file
When: the repository is initialized with an inexistent directory
Then: a ConnectionError is raised. This doesn't apply to FakeRepository as it
doesn't create a database
"""
with pytest.raises(ConnectionError):
repo.__class__("/inexistent_dir/database.db") # act
def test_repository_can_save_an_entity(
database: Any,
repo: Repository,
repo_tester: RepositoryTester,
entity: Entity,
) -> None:
"""Saved entities remain in the repository."""
repo.add(entity)
repo.commit() # act
assert entity == repo_tester.get_entity(database, entity)
def test_repository_can_save_an_entity_without_id_in_empty_repo(
repo: Repository,
entity: Entity,
) -> None:
"""
Given: An empty repository
When: adding an entity without id
Then: the id 0 is set
"""
entity = entity.__class__(name="Entity without id")
repo.add(entity)
repo.commit() # act
entities = repo.all(type(entity))
assert len(entities) == 1
assert entities[0].id_ == 0
def test_repository_can_save_an_entity_without_id(
repo: Repository,
inserted_entity: Entity,
) -> None:
"""
Given: A repository with an entity
When: adding an entity without id
Then: the id of the new entity is one unit greater than the last one.
"""
entity = inserted_entity.__class__(name="Entity without id")
repo.add(entity)
repo.commit() # act
saved_entity = repo.last(type(inserted_entity))
assert saved_entity.id_ == inserted_entity.id_ + 1
assert saved_entity.name == "Entity without id"
def test_repository_cant_save_an_entity_with_a_negative_id(
repo: Repository,
inserted_entity: Entity,
) -> None:
"""
Given: A repository with an entity
When: adding an entity with a negative id
Then: the id of the new entity is one unit greater than the last one.
"""
entity = inserted_entity.__class__(id=-3, name="Entity with negative id")
repo.add(entity)
repo.commit() # act
saved_entity = repo.last(type(inserted_entity))
assert saved_entity.id_ == inserted_entity.id_ + 1
assert saved_entity.name == "Entity with negative id"
def test_repo_add_entity_is_idempotent(
database: Any, repo: Repository, repo_tester: RepositoryTester, entity: Entity
) -> None:
"""
Given: An empty repository.
When: We insert the same entity twice and then commit.
Then: Only one item exists.
"""
repo.add(entity)
repo.add(entity)
repo.commit() # act
entities = repo_tester.get_all(database, type(entity))
assert len(entities) == 1
assert entity == entities[0]
def test_repo_add_entity_is_idempotent_if_entity_is_commited(
database: Any, repo: Repository, repo_tester: RepositoryTester, entity: Entity
) -> None:
"""
Given: A repository with the identity we want to add already commited.
When: We insert the same entity again and then commit.
Then: Only one item exists.
"""
repo_tester.insert_entity(database, entity)
repo.add(entity)
repo.commit() # act
entities = repo_tester.get_all(database, type(entity))
assert len(entities) == 1
assert entity == entities[0]
def test_repo_add_entity_updates_attribute(
database: Any, repo: Repository, repo_tester: RepositoryTester, entity: Entity
) -> None:
"""
Given: A repository with the identity we want to add already commited.
When: We insert the same entity with an attribute changed then commit.
Then: Only one item exists.
"""
repo_tester.insert_entity(database, entity)
# ignore: Although Entity doesn't have the rating attribute, all the entities
# defined in the tests models do.
entity.rating += 1 # type: ignore
repo.add(entity)
repo.commit() # act
entities = repo_tester.get_all(database, type(entity))
assert len(entities) == 1
assert entity.rating == entities[0].rating # type: ignore
def test_repository_doesnt_add_an_entity_if_we_dont_commit_changes(
database: Any,
repo: Repository,
repo_tester: RepositoryTester,
entity: Entity,
) -> None:
"""
Given: an empty repository.
When: an entity is added but we don't commit the changes.
Then: the entity is not found in the repository.
"""
repo.add(entity)
with pytest.raises(EntityNotFoundError):
repo_tester.get_entity(database, entity)
def test_repository_can_retrieve_an_entity(
repo: Repository,
inserted_entity: Entity,
) -> None:
"""Given an entity_id the repository returns the entity object."""
result = repo.get(type(inserted_entity), inserted_entity.id_)
assert result == inserted_entity
assert result.id_ == inserted_entity.id_
def test_repository_raises_error_if_no_entity_found_by_get(
repo: Repository,
entity: Entity,
) -> None:
"""As the entity is not inserted into the repository, it shouldn't be found."""
with pytest.raises(EntityNotFoundError) as error:
repo.get(type(entity), entity.id_)
assert (
f"There are no {entity._model_name}s with id "
f"{entity.id_} in the repository" in str(error.value)
)
def test_repository_can_retrieve_all_objects(
repo: Repository,
inserted_entities: List[Entity],
) -> None:
"""Given an entity type all the entity objects that match should be returned."""
entity_type = type(inserted_entities[0])
result = repo.all(entity_type)
assert result == inserted_entities
assert len(result) == 3
assert result[0].id_ == inserted_entities[0].id_
def test_repository_all_raises_error_if_empty_repository(
repo: Repository,
entity: Entity,
) -> None:
"""If there are no entities, an error should be raised."""
with pytest.raises(EntityNotFoundError) as error:
repo.all(type(entity))
assert f"There are no {entity._model_name} entities in the repository" in str(
error.value
)
def test_repository_can_search_by_property(
repo: Repository,
inserted_entities: List[Entity],
) -> None:
"""Search should return the objects that match the desired property."""
expected_entity = inserted_entities[1]
result = repo.search(type(expected_entity), {"id_": expected_entity.id_})
assert result == [expected_entity]
def test_repository_can_search_regular_expression(
repo: Repository, inserted_entities: List[Entity]
) -> None:
"""
Given: More than one entity is inserted in the repository.
When: We search using a regular expression
Then: The matching entity is found
"""
expected_entity = inserted_entities[1]
regular_expression = fr"^{expected_entity.name}.*"
result = repo.search(type(expected_entity), {"name": regular_expression})
assert result == [expected_entity]
def test_repository_search_raises_error_if_searching_by_unexistent_field(
repo: Repository,
inserted_entities: List[Entity],
) -> None:
"""If no object has the property of the search criteria, raise the desired
error.
"""
entity = inserted_entities[0]
with pytest.raises(EntityNotFoundError) as error:
repo.search(type(entity), {"unexistent_field": "unexistent_value"})
assert (
f"There are no {entity._model_name}s that match "
"the search filter {'unexistent_field': 'unexistent_value'}" in str(error.value)
)
def test_repository_search_raises_error_if_searching_by_unexistent_value(
repo: Repository,
inserted_entities: List[Entity],
) -> None:
"""If no object has a value like the search criteria raise the desired error."""
entity = inserted_entities[0]
with pytest.raises(EntityNotFoundError) as error:
repo.search(type(entity), {"id_": "unexistent_value"})
assert (
f"There are no {entity._model_name}s that match "
"the search filter {'id_': 'unexistent_value'}" in str(error.value)
)
def test_repository_can_search_by_multiple_properties(
repo: Repository,
inserted_entities: List[Entity],
) -> None:
"""
Given: a full repository.
When: a search is performed by multiple properties.
Then: the matching objects are returned.
"""
entity = inserted_entities[1]
search_criteria = entity.dict()
result = repo.search(type(entity), search_criteria)
assert result == [entity]
@pytest.mark.secondary()
def test_repository_can_delete_an_entity(
repo: Repository,
inserted_entities: List[Entity],
) -> None:
"""
Given: a full repository.
When: an entity is deleted.
Then: the entity is not longer in the repository.
"""
entity_to_delete = inserted_entities[1]
repo.delete(entity_to_delete)
repo.commit() # act
remaining_entities = repo.all(type(entity_to_delete))
assert entity_to_delete not in remaining_entities
@pytest.mark.secondary()
def test_repository_doesnt_delete_the_entity_if_we_dont_commit(
database: Any,
repo: Repository,
repo_tester: RepositoryTester,
inserted_entities: List[Entity],
) -> None:
"""
Given: a full repository.
When: an entity is deleted but we don't commit the changes.
Then: the entity is still in the repository.
"""
entity_to_delete = inserted_entities[1]
repo.delete(entity_to_delete) # act
remaining_entities = repo_tester.get_all(database, type(entity_to_delete))
assert entity_to_delete in remaining_entities
def test_repository_delete_raise_error_if_entity_not_found(
repo: Repository,
entity: Entity,
) -> None:
"""
Given: an empty repository.
When: trying to delete an unexistent entity.
Then: An EntityNotFoundError error is raised.
"""
with pytest.raises(EntityNotFoundError) as error:
repo.delete(entity)
assert (
f"Unable to delete entity {entity} because it's not in the repository"
in str(error.value)
)
def test_repository_last_returns_last_entity(
repo: Repository,
inserted_entities: List[Entity],
) -> None:
"""
Given: A repository with many entities.
When: using the last method.
Then: The greater entity is returned
"""
greater_entity = max(inserted_entities)
result = repo.last(type(greater_entity))
assert result == greater_entity
def test_repository_last_raise_error_if_entity_not_found(
repo: Repository,
entity: Entity,
) -> None:
"""
Given: an empty repository.
When: trying to get the last entity.
Then: An EntityNotFoundError error is raised.
"""
with pytest.raises(
EntityNotFoundError,
match=f"There are no {entity._model_name} entities in the repository",
):
repo.last(type(entity))
def test_repository_first_returns_first_entity(
repo: Repository,
inserted_entities: List[Entity],
) -> None:
"""
Given: A repository with many entities.
When: using the first method.
Then: The smallest entity is returned
"""
smaller_entity = min(inserted_entities)
result = repo.first(type(smaller_entity))
assert result == smaller_entity
def test_repository_first_raise_error_if_entity_not_found(
repo: Repository,
entity: Entity,
) -> None:
"""
Given: an empty repository.
When: trying to get the first entity.
Then: An EntityNotFoundError error is raised.
"""
with pytest.raises(
EntityNotFoundError,
match=f"There are no {entity._model_name} entities in the repository",
):
repo.first(type(entity))