-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |