Skip to content

Commit 2b97f5c

Browse files
committed
Increases test coverage for state backend
1 parent bbfbc11 commit 2b97f5c

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

tests/unit/test_state_backend.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ async def test_state_backend_init(
8484
assert state_backend.metadata == (metadata or dict())
8585

8686

87+
@pytest.mark.asyncio
88+
async def test_state_backend_init_with_existing_job(
89+
datetime_now: str,
90+
state_backend_cls: _t.Type[StateBackend],
91+
valid_job_id: str,
92+
) -> None:
93+
"""Tests `StateBackend` initialisation with an existing job_id."""
94+
# Initialise once to create the job
95+
state_backend1 = state_backend_cls(job_id=valid_job_id, metadata={"original": "data"})
96+
with time_machine.travel(datetime_now, tick=False):
97+
await state_backend1.init()
98+
assert state_backend1.metadata == {"original": "data"}
99+
100+
# Initialise again with the same job_id and new metadata
101+
state_backend2 = state_backend_cls(
102+
job_id=valid_job_id, metadata={"new": "data", "original": "updated"}
103+
)
104+
with time_machine.travel(datetime_now, tick=False):
105+
await state_backend2.init()
106+
107+
# The metadata should be merged
108+
assert state_backend2.metadata == {"original": "updated", "new": "data"}
109+
110+
87111
@pytest.mark.asyncio
88112
async def test_state_backend_get(state_backend_cls: _t.Type[DictStateBackend]) -> None:
89113
"""Tests `StateBackend` get method."""
@@ -140,3 +164,26 @@ async def test_state_backend_set(state_backend_cls: _t.Type[DictStateBackend]) -
140164
"nested": {"key": "new_value"},
141165
"nonexistent": {"key": "value"},
142166
}
167+
168+
169+
@pytest.mark.asyncio
170+
async def test_state_backend_id_methods(
171+
state_backend_cls: _t.Type[StateBackend], valid_job_id: str
172+
) -> None:
173+
"""Tests `_get_db_id` and `_strip_job_id` methods."""
174+
state_backend = state_backend_cls(job_id=valid_job_id)
175+
await state_backend.init()
176+
177+
# _get_db_id
178+
with pytest.raises(ValueError, match="Invalid entity id:"):
179+
state_backend._get_db_id("a:b:c")
180+
with pytest.raises(ValueError, match="does not belong to job"):
181+
state_backend._get_db_id("wrong_job:entity")
182+
183+
# _strip_job_id
184+
with pytest.raises(ValueError, match="Invalid database id:"):
185+
state_backend._strip_job_id("a")
186+
with pytest.raises(ValueError, match="Invalid database id:"):
187+
state_backend._strip_job_id("a:b:c")
188+
with pytest.raises(ValueError, match="does not belong to job"):
189+
state_backend._strip_job_id(f"wrong_job:{valid_job_id}")

0 commit comments

Comments
 (0)