From 552260dc73ff770e1bb9bfba1b705c967a3b9f54 Mon Sep 17 00:00:00 2001 From: Juan Marulanda <86245530+jmaruland@users.noreply.github.com> Date: Wed, 1 May 2024 10:19:34 -0400 Subject: [PATCH] Add smoke test utilities for the client (#634) * Added read method as a smoke test utility * Optimized method, enable verbose parameter and added docstring * Optimize smoke read method * Added first draft for test case on smoke read * Added test cases for smoke.read --- tiled/_tests/test_client_smoke.py | 46 +++++++++++++++++++++++++++++++ tiled/client/smoke.py | 44 +++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 tiled/_tests/test_client_smoke.py create mode 100644 tiled/client/smoke.py diff --git a/tiled/_tests/test_client_smoke.py b/tiled/_tests/test_client_smoke.py new file mode 100644 index 000000000..713710ed2 --- /dev/null +++ b/tiled/_tests/test_client_smoke.py @@ -0,0 +1,46 @@ +import numpy as np +import pytest + +from ..adapters.array import ArrayAdapter +from ..adapters.mapping import MapAdapter +from ..client import Context, from_context +from ..client.smoke import read +from ..server.app import build_app + + +class Broken(Exception): + pass + + +class BrokenArrayAdapter(ArrayAdapter): + def read(self, *args, **kwargs): + raise Broken + + def read_block(self, *args, **kwargs): + raise Broken + + +@pytest.fixture(scope="module") +def context(): + mapping = { + "A": ArrayAdapter.from_array(np.array([1, 2, 3]), metadata={"A": "a"}), + "B": BrokenArrayAdapter.from_array(np.array([4, 5, 6]), metadata={"B": "b"}), + } + + tree = MapAdapter(mapping) + with Context.from_app(build_app(tree)) as context: + yield context + + +def test_smoke_read_list(context): + client = from_context(context) + + faulty_list = read(client) + assert len(faulty_list) == 1 + + +def test_smoke_read_raise(context): + client = from_context(context) + + with pytest.raises(Broken): + read(client, strict=True) diff --git a/tiled/client/smoke.py b/tiled/client/smoke.py new file mode 100644 index 000000000..8454fd1d5 --- /dev/null +++ b/tiled/client/smoke.py @@ -0,0 +1,44 @@ +import sys + +from ..structures.core import StructureFamily + + +def read(node, verbose=False, strict=False): + """ + + Parameters + ---------- + node : tiled node + A Client node with access to the tiled server. + It can take form of anything compatible with the values in StructureFamily. + verbose : bool, optional + Enables status messages while process runs. The default is False. + strict : bool, optional + Enables debugging mode if a faulty node is found. + + Returns + ------- + faulty_docs : List + A list with URIs of all the faulty nodes that were found. + + """ + + faulty_entries = [] + if node.structure_family == StructureFamily.container: + for key, child_node in node.items(): + fault_result = read(child_node, verbose=verbose, strict=strict) + faulty_entries.extend(fault_result) + else: + try: + tmp = node.read() # noqa: F841 + except Exception as err: + faulty_entries.append(node.uri) + if verbose: + print(f"ERROR: {node.item['id']} - {err!r}", file=sys.stderr) + if strict: + raise + else: + if verbose: + print(f"SUCCESS: {node.item['id']} ", file=sys.stderr) + + return faulty_entries