Skip to content

Commit

Permalink
Error item links (#89)
Browse files Browse the repository at this point in the history
* Add failing test case

* Handle raw keys
  • Loading branch information
manycoding authored May 21, 2019
1 parent c2108d4 commit a0b72b1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Note that the top-most release is changes in the unreleased master branch on Git
### Added
### Changed
### Fixed
- `Arche.glance()`, #88
- Item links in Schema validation errors, #89
- Empty NAN bars on category graphs, #93
### Removed

Expand Down
14 changes: 11 additions & 3 deletions src/arche/report.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict

from arche import SH_URL
from arche.rules.result import Level, Outcome, Result
from colorama import Fore, Style
from IPython.display import display, HTML
Expand Down Expand Up @@ -100,8 +101,15 @@ def sample_keys(keys: pd.Series, limit: int) -> str:
sample = keys.sample(limit)
else:
sample = keys

def url(x: str) -> str:
if SH_URL in x:
return f"<a href='{x}'>{x.split('/')[-1]}</a>"
key, number = x.rsplit("/", 1)
return f"<a href='{SH_URL}/{key}/item/{number}'>{number}</a>"

# make links only for Cloud data
if keys.dtype == np.dtype("object") and "/" in keys.iloc[0]:
sample = [f"<a href='{k}'>{k.split('/')[-1]}</a>" for k in sample]
sample = ", ".join(sample)
return sample
sample = sample.apply(url)

return ", ".join(sample)
1 change: 0 additions & 1 deletion src/arche/rules/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def validate(schema: Schema, raw_items: RawItems, fast: bool = False) -> Result:
schema_result_message = (
f"{len(raw_items)} items were checked, {len(errors)} error(s)"
)

if errors:
result.add_error(schema_result_message, errors=errors)
else:
Expand Down
30 changes: 29 additions & 1 deletion tests/test_arche.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from arche import arche
from arche import arche, SH_URL
from arche.arche import Arche
from arche.rules.result import Level
from conftest import create_result
Expand Down Expand Up @@ -245,6 +245,34 @@ def test_validate_with_json_schema(mocker, get_job_items, get_schema):
assert a.report.results.get("JSON Schema Validation") == res


def test_validate_with_json_schema_fails(mocker, get_job_items, get_schema):
mocked_html = mocker.patch("arche.report.HTML", autospec=True)
key = f"112358/13/21"
url_base = f"{SH_URL}/{key}/item"
res = create_result(
"JSON Schema Validation",
{
Level.ERROR: [
(
"4 items were checked, 1 error(s)",
None,
{"'price' is a required property": {f"{key}/1"}},
)
]
},
)
schema = {"type": "object", "required": ["price"]}
a = Arche("source", schema=schema)
a._source_items = get_job_items
a.validate_with_json_schema()

assert len(a.report.results) == 1
assert a.report.results.get("JSON Schema Validation") == res
mocked_html.assert_any_call(
f"1 items affected - 'price' is a required property: <a href='{url_base}/1'>1</a>"
)


@pytest.mark.parametrize(
"source, expected_message",
[
Expand Down
10 changes: 8 additions & 2 deletions tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,22 @@ def test_write_detailed_errors(mocker, errors, short, keys_limit, expected_messa
(
pd.Series(f"{SH_URL}/112358/13/21/item/0"),
5,
[f"{SH_URL}/112358/13/21/item/5"],
pd.Series(f"{SH_URL}/112358/13/21/item/5"),
f"<a href='{SH_URL}/112358/13/21/item/0'>0</a>",
),
(
pd.Series([f"{SH_URL}/112358/13/21/item/{i}" for i in range(20)]),
10,
[f"{SH_URL}/112358/13/21/item/5"],
pd.Series(f"{SH_URL}/112358/13/21/item/5"),
f"<a href='{SH_URL}/112358/13/21/item/5'>5</a>",
),
(pd.Series([str(i) for i in range(5)]), 1, ["0"], "0"),
(
pd.Series("112358/13/21/0"),
1,
pd.Series("112358/13/21/0"),
f"<a href='{SH_URL}/112358/13/21/item/0'>0</a>",
),
],
)
def test_sample_keys(mocker, keys, limit, sample_mock, expected_sample):
Expand Down

0 comments on commit a0b72b1

Please sign in to comment.