Skip to content

Commit

Permalink
Test price (#163)
Browse files Browse the repository at this point in the history
* Fix price after merge

* Bump coverage
  • Loading branch information
manycoding committed Sep 5, 2019
1 parent a47e551 commit ced8822
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
19 changes: 6 additions & 13 deletions src/arche/rules/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def compare_was_now(df: pd.DataFrame, tagged_fields: TaggedFields):
result.add_error(
f"{price_less_percent} ({len(df_prices_less)}) of "
f"items with {price_was_field} < {price_field}",
errors={error: list(df_prices_less.index)},
errors={error: set(df_prices_less.index)},
)

df_prices_equals = pd.DataFrame(
Expand All @@ -52,15 +52,14 @@ def compare_was_now(df: pd.DataFrame, tagged_fields: TaggedFields):
),
errors=(
{
f"Prices equal for {len(df_prices_equals)} items": list(
f"Prices equal for {len(df_prices_equals)} items": set(
df_prices_equals.index
)
}
),
)

result.items_count = len(df.index)

return result


Expand Down Expand Up @@ -98,7 +97,7 @@ def compare_prices_for_same_urls(

errors = {}
for url, group in missing_urls.groupby(missing_urls):
errors[f"Missing {url}"] = pd.Series(group.index)
errors[f"Missing {url}"] = set(group.index)

if not missing_urls.empty:
result.add_info(
Expand Down Expand Up @@ -207,12 +206,6 @@ def compare_prices_for_same_names(
return result

name_field = name_field[0]

product_url_field = tagged_fields.get("product_url_field")
if not product_url_field:
result.add_info("product_url_field tag is not set")
else:
product_url_field = product_url_field[0]
source_df = source_df[source_df[name_field].notnull()]
target_df = target_df[target_df[name_field].notnull()]

Expand All @@ -228,13 +221,13 @@ def compare_prices_for_same_names(

errors = {}
for name, group in missing_names.groupby(missing_names):
errors[f"Missing {name}"] = pd.Series(group.index)
errors[f"Missing {name}"] = set(group.index)

if missing_names:
if not missing_names.empty:
result.add_info(
f"{len(missing_names)} names missing from the tested job", errors=errors
)
if new_names:
if not new_names.empty:
result.add_info(f"{len(new_names)} new names in the tested job")
result.add_info(f"{len(same_names)} same names in both jobs")

Expand Down
43 changes: 41 additions & 2 deletions tests/rules/test_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
(
"22.22% (2) of items with original_price < sale_price",
None,
{"Past price is less than current for 2 items": [0, 8]},
{"Past price is less than current for 2 items": {0, 8}},
)
],
Level.WARNING: [
(
"11.11% (1) of items with original_price = sale_price",
None,
{"Prices equal for 1 items": [1]},
{"Prices equal for 1 items": {1}},
)
],
},
Expand Down Expand Up @@ -124,3 +124,42 @@ def test_compare_names_for_same_urls(
pd.DataFrame(source_data), pd.DataFrame(target_data), tagged_fields
)
assert result == create_result("Compare Names Per Url", expected_messages)


@pytest.mark.parametrize(
"source_data, target_data, tagged_fields, expected_messages",
[
(
{"name": ["Coffee", "Tea", "Juice"], "price": [3.0, 5.0, 2.0]},
{"name": ["Coffee", "Tea", "Wine"], "price": [4.0, 4.8, 20.0]},
{"name_field": ["name"], "product_price_field": ["price"]},
{
Level.INFO: [
(
"1 names missing from the tested job",
None,
{"Missing Wine": {2}},
),
("1 new names in the tested job",),
("2 same names in both jobs",),
],
Level.ERROR: [
(
"2 checked, 1 errors",
(
"different price for Coffee\nsource price is 3.0 for 0\n"
"target price is 4.0 for 0"
),
)
],
},
)
],
)
def test_compare_prices_for_same_names(
source_data, target_data, tagged_fields, expected_messages
):
result = p.compare_prices_for_same_names(
pd.DataFrame(source_data), pd.DataFrame(target_data), tagged_fields
)
assert result == create_result("Compare Prices For Same Names", expected_messages)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ mock_use_standalone_module = true
branch = true

[coverage:report]
fail_under = 78
fail_under = 81

0 comments on commit ced8822

Please sign in to comment.