Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update agent_persist_mock to patch _exists method #868

Merged
merged 5 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/ostorlab/testing/agent.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""mock agent implements the required methods to test the agent's behavior without using external components."""

from typing import List, Union

Check warning on line 3 in src/ostorlab/testing/agent.py

View check run for this annotation

Codecov / codecov/patch

src/ostorlab/testing/agent.py#L3

Added line #L3 was not covered by tests
import dataclasses

import pytest

from typing import List

from ostorlab.agent.message import message as msg


Expand Down Expand Up @@ -95,9 +94,15 @@
if key in storage:
return storage[key]

def _add(key, value):
def _add(key: Union[str, bytes], value: bytes):
"""Check values are present in the storage dict."""
storage[key] = str(value).encode()
if isinstance(value, bytes) is False:
value = str(value).encode()

Check warning on line 100 in src/ostorlab/testing/agent.py

View check run for this annotation

Codecov / codecov/patch

src/ostorlab/testing/agent.py#L100

Added line #L100 was not covered by tests
storage[key] = value

def _exists(key: Union[str, bytes]) -> bool:
"""Check if thr key is present in the storage dict."""
return key in storage

def _hash_add(hash_name, mapping):
for k, v in mapping.items():
Expand Down Expand Up @@ -156,6 +161,10 @@
"ostorlab.agent.mixins.agent_persist_mixin.AgentPersistMixin.add",
side_effect=_add,
)
mocker.patch(
"ostorlab.agent.mixins.agent_persist_mixin.AgentPersistMixin.exists",
side_effect=_exists,
)
mocker.patch(
"ostorlab.agent.mixins.agent_persist_mixin.AgentPersistMixin.hash_add",
side_effect=_hash_add,
Expand Down
3 changes: 3 additions & 0 deletions tests/testing/agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ def testMockPersistAgent_whensetMethodsAreCalled_stateIsPersistedByMock(
assert test_agent.set_add("test", "1") is True
assert test_agent.set_is_member("test", "1") is True
assert agent_persist_mock == {"test": {"1"}}
assert test_agent.exists("akey") is False
test_agent.add("akey", b"aval")
assert test_agent.exists("akey") is True