Skip to content

Commit 0f50f88

Browse files
committed
Add tests on persistence between different repositories
1 parent d7f14d9 commit 0f50f88

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

tests/repository/async_/test_operation_isolation.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,35 @@ async def test_update_model_doesnt_update_other_models_from_same_repo(
5858
assert updated_model2.name == "SomeoneElse"
5959

6060

61+
async def test_update_model_updates_models_retrieved_by_other_repos(
62+
repository_class, model_class, sa_manager
63+
):
64+
repo = repository_class(sa_manager.get_bind())
65+
repo2 = repository_class(sa_manager.get_bind())
66+
67+
# Populate a database entry to be used for tests
68+
model1 = model_class(
69+
name="Someone",
70+
)
71+
await repo.save(model1)
72+
assert model1.model_id is not None
73+
74+
# Retrieve the models
75+
new_model1 = await repo.get(model1.model_id)
76+
77+
# Update the model with another repository instance
78+
new_model1.name = "StillSomeoneElse"
79+
await repo2.save(new_model1)
80+
81+
# Check model1 has been updated
82+
updated_model1 = await repo2.get(model1.model_id)
83+
assert updated_model1.name == "StillSomeoneElse"
84+
85+
# Check model1 has been updated
86+
updated_model1b = await repo.get(model1.model_id)
87+
assert updated_model1b.name == "StillSomeoneElse"
88+
89+
6190
@patch.object(AsyncSessionHandler, "commit", return_value=None, new_callable=AsyncMock)
6291
async def test_commit_triggers_once_per_operation_using_internal_uow(
6392
mocked_uow_commit: AsyncMock, repository_class, model_class, sa_manager

tests/repository/sync/test_operation_isolation.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,35 @@ def test_update_model_doesnt_update_other_models_from_same_repo(
5858
assert updated_model2.name == "SomeoneElse"
5959

6060

61+
def test_update_model_updates_models_retrieved_by_other_repos(
62+
repository_class, model_class, sa_manager
63+
):
64+
repo = repository_class(sa_manager.get_bind())
65+
repo2 = repository_class(sa_manager.get_bind())
66+
67+
# Populate a database entry to be used for tests
68+
model1 = model_class(
69+
name="Someone",
70+
)
71+
repo.save(model1)
72+
assert model1.model_id is not None
73+
74+
# Retrieve the models
75+
new_model1 = repo.get(model1.model_id)
76+
77+
# Update the model with another repository instance
78+
new_model1.name = "StillSomeoneElse"
79+
repo2.save(new_model1)
80+
81+
# Check model1 has been updated
82+
updated_model1 = repo2.get(model1.model_id)
83+
assert updated_model1.name == "StillSomeoneElse"
84+
85+
# Check model1 has been updated
86+
updated_model1b = repo.get(model1.model_id)
87+
assert updated_model1b.name == "StillSomeoneElse"
88+
89+
6190
@patch.object(SessionHandler, "commit", return_value=None)
6291
def test_commit_triggers_once_per_operation(
6392
mocked_uow_commit: MagicMock, repository_class, model_class, sa_manager

0 commit comments

Comments
 (0)