Skip to content

Commit 5f7d64d

Browse files
committed
Units testing patience
1 parent e14e21e commit 5f7d64d

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/pbench/test/unit/server/test_cache_manager.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pytest
1313

14+
from pbench.server import JSONOBJECT
1415
from pbench.server.cache_manager import (
1516
BadDirpath,
1617
BadFilename,
@@ -910,6 +911,32 @@ def test_get_inventory(
910911
assert file_info["type"] == exp_file_type
911912
assert file_info["stream"].stream == exp_stream
912913

914+
def test_cm_inventory(self, monkeypatch, server_config, make_logger):
915+
"""Verify the happy path of the high level get_inventory"""
916+
id = None
917+
918+
class MockTarball:
919+
def get_inventory(self, target: str) -> JSONOBJECT:
920+
return {
921+
"name": target,
922+
"type": CacheType.FILE,
923+
"stream": Inventory(io.BytesIO(b"success")),
924+
}
925+
926+
def mock_find_dataset(self, dataset: str) -> MockTarball:
927+
nonlocal id
928+
id = dataset
929+
930+
return MockTarball()
931+
932+
with monkeypatch.context() as m:
933+
m.setattr(CacheManager, "find_dataset", mock_find_dataset)
934+
cm = CacheManager(server_config, make_logger)
935+
inventory = cm.get_inventory("dataset", "target")
936+
assert id == "dataset"
937+
assert inventory["name"] == "target"
938+
assert inventory["stream"].read() == b"success"
939+
913940
def test_tarfile_extract(self, monkeypatch, tmp_path):
914941
"""Test to check Tarball.extract success"""
915942
tar = Path("/mock/result.tar.xz")

lib/pbench/test/unit/server/test_datasets_inventory.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,31 @@ def mock_send_file(path_or_file, *args, **kwargs):
128128
assert args["as_attachment"] is False
129129
assert args["download_name"] == "f1.json"
130130
assert mock_close
131+
132+
def test_send_fail(self, query_get_as, monkeypatch):
133+
mock_close = False
134+
135+
def mock_close(_s):
136+
nonlocal mock_close
137+
mock_close = True
138+
139+
exp_stream = io.BytesIO(b"file_as_a_byte_stream")
140+
141+
def mock_get_inventory(_s, _d: str, _t: str):
142+
return {
143+
"name": "f1.json",
144+
"type": CacheType.FILE,
145+
"stream": Inventory(exp_stream, None),
146+
}
147+
148+
def mock_send_file(path_or_file, *args, **kwargs):
149+
raise Exception("I'm failing to succeed")
150+
151+
monkeypatch.setattr(exp_stream, "close", mock_close)
152+
monkeypatch.setattr(CacheManager, "get_inventory", mock_get_inventory)
153+
monkeypatch.setattr(
154+
"pbench.server.api.resources.datasets_inventory.send_file", mock_send_file
155+
)
156+
157+
query_get_as("fio_2", "f1.json", HTTPStatus.INTERNAL_SERVER_ERROR)
158+
assert mock_close

0 commit comments

Comments
 (0)