Skip to content

Commit 94d5524

Browse files
committed
Fix tests for minimum properties and items (scrapinghub#432)
* Fix tests for minimum properties and items In JSON Schema validation for minimum properties and items the data should be non-empty. * Add tests for invalid empty properties and items Add validation messages for empty properties and items that should be non-empty. * Fix warning from pytest trylast Use decorator @pytest.hookimpl(trylast=True) instead @pytest.mark.trylast * Fix warnings about literal comparations with 'is' Change literal comparations from 'is' to '=='. * Apply pre-commit * Use jsonschema >= 3.2.0 for all python versions * Define minimum version for JSON Schema From version 4.21.0 JSON Schema to validation for minimum properties and items the data should be non-empty.
1 parent 8f8784c commit 94d5524

File tree

7 files changed

+44
-16
lines changed

7 files changed

+44
-16
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
zip_safe=False,
1515
include_package_data=True,
1616
install_requires=[
17-
"jsonschema[format]>=3.2.0",
17+
"jsonschema[format]>=4.21.0",
1818
"python-slugify",
1919
],
2020
extras_require={

spidermon/contrib/pytest/plugins/filter_monitors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def pytest_report_header(config):
77
return "Spidermon monitor filtering"
88

99

10-
@pytest.mark.trylast
10+
@pytest.hookimpl(trylast=True)
1111
def pytest_collection_modifyitems(session, config, items):
1212
items[:] = [
1313
item

spidermon/contrib/validation/jsonschema/translator.py

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class JSONSchemaMessageTranslator(MessageTranslator):
3838
r"^.* is not allowed for .*$": messages.NOT_ALLOWED_VALUE,
3939
r"^.+ is too short$": messages.FIELD_TOO_SHORT,
4040
r"^.+ is too long$": messages.FIELD_TOO_LONG,
41+
r"^.+ should be non-empty$": messages.SHOULD_BE_NON_EMPTY,
4142
r"^.+ does not match .*$": messages.REGEX_NOT_MATCHED,
4243
r"^.+ has non-unique elements$": messages.NOT_UNIQUE,
4344
}

spidermon/contrib/validation/messages.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@
4444
NOT_MULTIPLE_OF = "Not multiple of"
4545
NOT_ALLOWED_VALUE = "Not allowed value"
4646
NOT_UNIQUE = "Not unique"
47+
SHOULD_BE_NON_EMPTY = "should be non-empty"

tests/contrib/scrapy/test_pipelines.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class PipelineJSONSchemaValidator(PipelineTest):
8888
settings={SETTING_SCHEMAS: [test_schema]},
8989
cases=[
9090
f"'{STATS_ITEM_ERRORS}' not in {{stats}}",
91-
f"{{stats}}['{STATS_AMOUNTS}'] is 1",
91+
f"{{stats}}['{STATS_AMOUNTS}'] == 1",
9292
assert_type_in_stats(Item),
9393
],
9494
),
@@ -183,8 +183,8 @@ class PipelineJSONSchemaValidator(PipelineTest):
183183
item=TestItem(),
184184
settings={SETTING_SCHEMAS: {TestItem: [test_schema, tree_schema]}},
185185
cases=[
186-
f"{{stats}}['{STATS_AMOUNTS}'] is 2",
187-
f"{{stats}}['{STATS_ITEM_ERRORS}'] is 2",
186+
f"{{stats}}['{STATS_AMOUNTS}'] == 2",
187+
f"{{stats}}['{STATS_ITEM_ERRORS}'] == 2",
188188
],
189189
),
190190
DataTest(
@@ -198,7 +198,7 @@ class PipelineJSONSchemaValidator(PipelineTest):
198198
item=TreeItem(),
199199
settings={SETTING_SCHEMAS: {TestItem: test_schema, TreeItem: tree_schema}},
200200
cases=[
201-
f"{{stats}}['{STATS_MISSINGS}'] is 1",
201+
f"{{stats}}['{STATS_MISSINGS}'] == 1",
202202
assert_type_in_stats(TestItem),
203203
assert_type_in_stats(TreeItem),
204204
],

tests/test_validators_jsonschema.py

+34-8
Original file line numberDiff line numberDiff line change
@@ -907,35 +907,61 @@ class Maximum(SchemaTest):
907907

908908

909909
class MinItems(SchemaTest):
910-
schema = {"minItems": 1}
910+
schema = {"minItems": 2}
911911
data_tests = [
912-
DataTest(name="longer is valid", data=[1, 2], valid=True),
913-
DataTest(name="exact length is valid", data=[1], valid=True),
912+
DataTest(name="longer is valid", data=[1, 2, 3], valid=True),
913+
DataTest(name="exact length is valid", data=[1, 2], valid=True),
914914
DataTest(
915915
name="too short is invalid",
916-
data=[],
916+
data=[1],
917917
valid=False,
918918
expected_errors={"": [messages.FIELD_TOO_SHORT]},
919919
),
920920
DataTest(name="ignores non-arrays", data="", valid=True),
921921
]
922922

923923

924+
class EmptyItems(SchemaTest):
925+
schema = {"minItems": 1}
926+
data_tests = [
927+
DataTest(
928+
name="empty is invalid",
929+
data=list(),
930+
valid=False,
931+
expected_errors={"": [messages.SHOULD_BE_NON_EMPTY]},
932+
)
933+
]
934+
935+
924936
class MinProperties(SchemaTest):
925-
schema = {"minProperties": 1}
937+
schema = {"minProperties": 2}
926938
data_tests = [
927-
DataTest(name="longer is valid", data={"foo": 1, "bar": 2}, valid=True),
928-
DataTest(name="exact length is valid", data={"foo": 1}, valid=True),
939+
DataTest(
940+
name="longer is valid", data={"foo": 1, "bar": 2, "foobar": 3}, valid=True
941+
),
942+
DataTest(name="exact length is valid", data={"foo": 1, "bar": 2}, valid=True),
929943
DataTest(
930944
name="too short is invalid",
931-
data={},
945+
data={"foo": 1},
932946
valid=False,
933947
expected_errors={"": [messages.NOT_ENOUGH_PROPERTIES]},
934948
),
935949
DataTest(name="ignores non-objects", data="", valid=True),
936950
]
937951

938952

953+
class EmptyProperties(SchemaTest):
954+
schema = {"minProperties": 1}
955+
data_tests = [
956+
DataTest(
957+
name="empty is invalid",
958+
data=dict(),
959+
valid=False,
960+
expected_errors={"": [messages.SHOULD_BE_NON_EMPTY]},
961+
)
962+
]
963+
964+
939965
class Minimum(SchemaTest):
940966
# exclusiveMinimum behaviour changed from draft-04 to draft-06
941967
# http://json-schema.org/draft-06/json-schema-release-notes.html#backwards-incompatible-changes

tox.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ commands = pytest -s --ignore=./tests/contrib --ignore=./tests/utils/test_zyte.p
2222
basepython = python3.8
2323
deps =
2424
{[testenv]deps}
25-
jsonschema[format]==3.2.0
25+
jsonschema[format]==4.21.0
2626

2727
[testenv:docs]
28-
deps =
28+
deps =
2929
-r {toxinidir}/docs/requirements-docs.txt
3030
changedir = docs
3131
commands =

0 commit comments

Comments
 (0)