Skip to content

Commit

Permalink
Add smoke test utilities for the client (#634)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jmaruland authored May 1, 2024
1 parent 821e06b commit 552260d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tiled/_tests/test_client_smoke.py
Original file line number Diff line number Diff line change
@@ -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)
44 changes: 44 additions & 0 deletions tiled/client/smoke.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 552260d

Please sign in to comment.