From 376e4bd52956ded4849c7ae832fc561edc1a88b8 Mon Sep 17 00:00:00 2001 From: Mohamed Nasser Date: Sun, 23 Feb 2025 14:54:54 +0000 Subject: [PATCH 1/5] Update agent_persist_mock to patch _exists method --- src/ostorlab/testing/agent.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ostorlab/testing/agent.py b/src/ostorlab/testing/agent.py index c54c56086..e14699b8e 100644 --- a/src/ostorlab/testing/agent.py +++ b/src/ostorlab/testing/agent.py @@ -95,9 +95,15 @@ def _get(key): if key in storage: return storage[key] - def _add(key, value): + def _add(key: str | bytes, value: bytes): """Check values are present in the storage dict.""" - storage[key] = str(value).encode() + if isinstance(value, bytes) is False: + storage[key] = str(value).encode() + storage[key] = value + + def _exists(key: str) -> 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(): @@ -156,6 +162,10 @@ def _value_type(key): "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, From 776043e1fc3f665ebb3fed22379fe5a94ae6ae2d Mon Sep 17 00:00:00 2001 From: Mohamed Nasser Date: Sun, 23 Feb 2025 15:14:05 +0000 Subject: [PATCH 2/5] Update agent_persist_mock to patch _exists method --- src/ostorlab/testing/agent.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ostorlab/testing/agent.py b/src/ostorlab/testing/agent.py index e14699b8e..b15b4eae1 100644 --- a/src/ostorlab/testing/agent.py +++ b/src/ostorlab/testing/agent.py @@ -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 import dataclasses import pytest -from typing import List - from ostorlab.agent.message import message as msg @@ -95,7 +94,7 @@ def _get(key): if key in storage: return storage[key] - def _add(key: str | bytes, value: bytes): + def _add(key: Union[str, bytes], value: bytes): """Check values are present in the storage dict.""" if isinstance(value, bytes) is False: storage[key] = str(value).encode() From c710b37345546ceaa6231d85c82b9844ba7b62e8 Mon Sep 17 00:00:00 2001 From: Mohamed Nasser Date: Sun, 23 Feb 2025 15:22:16 +0000 Subject: [PATCH 3/5] Update agent_persist_mock to patch _exists method --- src/ostorlab/testing/agent.py | 2 +- tests/testing/agent_test.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ostorlab/testing/agent.py b/src/ostorlab/testing/agent.py index b15b4eae1..2c70f1a05 100644 --- a/src/ostorlab/testing/agent.py +++ b/src/ostorlab/testing/agent.py @@ -97,7 +97,7 @@ def _get(key): def _add(key: Union[str, bytes], value: bytes): """Check values are present in the storage dict.""" if isinstance(value, bytes) is False: - storage[key] = str(value).encode() + value = str(value).encode() storage[key] = value def _exists(key: str) -> bool: diff --git a/tests/testing/agent_test.py b/tests/testing/agent_test.py index 1025ec7a9..bba8a0dfb 100644 --- a/tests/testing/agent_test.py +++ b/tests/testing/agent_test.py @@ -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 From 6f7af64b33decf7d4e76659cf9caf325bc8787b1 Mon Sep 17 00:00:00 2001 From: Mohamed Nasser Date: Sun, 23 Feb 2025 15:29:01 +0000 Subject: [PATCH 4/5] Update agent_persist_mock to patch _exists method --- src/ostorlab/testing/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ostorlab/testing/agent.py b/src/ostorlab/testing/agent.py index 2c70f1a05..176114564 100644 --- a/src/ostorlab/testing/agent.py +++ b/src/ostorlab/testing/agent.py @@ -100,7 +100,7 @@ def _add(key: Union[str, bytes], value: bytes): value = str(value).encode() storage[key] = value - def _exists(key: str) -> bool: + def _exists(key: Union[str, bytes]) -> bool: """Check if thr key is present in the storage dict.""" return key in storage From 6819446f88dfeec3d85c818a1f98fe6feeb689dd Mon Sep 17 00:00:00 2001 From: Mohamed Nasser Date: Mon, 24 Feb 2025 10:01:18 +0000 Subject: [PATCH 5/5] Update agent_persist_mock to patch _exists method --- src/ostorlab/testing/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ostorlab/testing/agent.py b/src/ostorlab/testing/agent.py index 176114564..8ff2a96da 100644 --- a/src/ostorlab/testing/agent.py +++ b/src/ostorlab/testing/agent.py @@ -94,7 +94,7 @@ def _get(key): if key in storage: return storage[key] - def _add(key: Union[str, bytes], value: bytes): + def _add(key: Union[str, bytes], value: bytes) -> None: """Check values are present in the storage dict.""" if isinstance(value, bytes) is False: value = str(value).encode()