Skip to content

Commit

Permalink
Make tests platform-agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Apr 8, 2020
1 parent 6cfa3bb commit 145378d
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ def chdir(path):
os.chdir(cwd)


def t(message):
path, rest = message.split(' ', 1)
return '{} {}'.join(path.replace('/', os.sep), rest)


def validate(name, *args, **kwargs):
filepath = 'schema/{}.json'.format(name)
filepath = os.path.join('schema', '{}.json'.format(name))
return getattr(jscc.testing.checks, 'validate_' + name)(path(filepath), parse(filepath), *args, **kwargs)


def test_get_empty_files():
directory = os.path.realpath(path('empty'))
directory = os.path.realpath(path('empty')) + os.sep
with chdir(directory):
paths = set()
for result in get_empty_files():
Expand All @@ -38,16 +43,16 @@ def test_get_empty_files():
assert len(result) == 1

assert paths == {
'/empty-array.json',
'/empty-object.json',
'/empty-string.json',
'/null.json',
'/whitespace.txt',
'empty-array.json',
'empty-object.json',
'empty-string.json',
'null.json',
'whitespace.txt',
}


def test_get_misindented_files():
directory = os.path.realpath(path('indent'))
directory = os.path.realpath(path('indent')) + os.sep
with chdir(directory):
paths = set()
for result in get_misindented_files():
Expand All @@ -56,14 +61,14 @@ def test_get_misindented_files():
assert len(result) == 1

assert paths == {
'/ascii.json',
'/compact.json',
'/no-newline.json',
'ascii.json',
'compact.json',
'no-newline.json',
}


def test_get_invalid_json_files():
directory = os.path.realpath(path('json'))
directory = os.path.realpath(path('json')) + os.sep
with chdir(directory):
results = {}
for result in get_invalid_json_files():
Expand All @@ -72,10 +77,10 @@ def test_get_invalid_json_files():
assert len(result) == 2

assert len(results) == 2
assert isinstance(results['/duplicate-key.json'], DuplicateKeyError)
assert isinstance(results['/invalid.json'], json.decoder.JSONDecodeError)
assert str(results['/duplicate-key.json']) == 'x'
assert str(results['/invalid.json']) == 'Expecting property name enclosed in double quotes: line 2 column 1 (char 2)' # noqa
assert isinstance(results['duplicate-key.json'], DuplicateKeyError)
assert isinstance(results['invalid.json'], json.decoder.JSONDecodeError)
assert str(results['duplicate-key.json']) == 'x'
assert str(results['invalid.json']) == 'Expecting property name enclosed in double quotes: line 2 column 1 (char 2)' # noqa


def test_get_invalid_csv_files():
Expand All @@ -91,7 +96,7 @@ def test_get_invalid_csv_files():


def test_validate_codelist_enum():
directory = os.path.realpath(path('schema'))
directory = os.path.realpath(path('schema')) + os.sep
with chdir(directory):
with pytest.warns(UserWarning) as records:
filepath = os.path.join(directory, 'codelist_enum.json')
Expand All @@ -100,15 +105,15 @@ def test_validate_codelist_enum():
errors = validate_codelist_enum(filepath, data)

assert sorted(str(record.message).replace(directory, '') for record in records) == [
'/codelist_enum.json is missing "codelist" and "openCodelist" at /properties/noCodelistArray',
'/codelist_enum.json is missing "codelist" and "openCodelist" at /properties/noCodelistString',
'/codelist_enum.json is missing "enum", though "openCodelist" is false, at /properties/failClosedArray',
'/codelist_enum.json is missing "enum", though "openCodelist" is false, at /properties/failClosedString',
"/codelist_enum.json refers to missing file codelists/missing.csv at /properties/missing",
'/codelist_enum.json sets "enum", though "openCodelist" is true, at /properties/failOpenArray',
'/codelist_enum.json sets "enum", though "openCodelist" is true, at /properties/failOpenString',
"/codelist_enum.json: /properties/mismatchArray/enum doesn't match codelists/test.csv; added {'extra'}",
"/codelist_enum.json: /properties/mismatchString/enum doesn't match codelists/test.csv; added {'extra'}; removed {None}", # noqa
'codelist_enum.json is missing "codelist" and "openCodelist" at /properties/noCodelistArray',
'codelist_enum.json is missing "codelist" and "openCodelist" at /properties/noCodelistString',
'codelist_enum.json is missing "enum", though "openCodelist" is false, at /properties/failClosedArray',
'codelist_enum.json is missing "enum", though "openCodelist" is false, at /properties/failClosedString',
"codelist_enum.json refers to missing file codelists/missing.csv at /properties/missing",
'codelist_enum.json sets "enum", though "openCodelist" is true, at /properties/failOpenArray',
'codelist_enum.json sets "enum", though "openCodelist" is true, at /properties/failOpenString',
"codelist_enum.json: /properties/mismatchArray/enum doesn't match codelists/test.csv; added {'extra'}",
"codelist_enum.json: /properties/mismatchString/enum doesn't match codelists/test.csv; added {'extra'}; removed {None}", # noqa
]
assert errors == len(records) == 9

Expand All @@ -118,7 +123,7 @@ def test_validate_deep_properties():
errors = validate('deep_properties', allow_deep={'/properties/allow'})

assert sorted(str(record.message) for record in records) == [
'tests/fixtures/schema/deep_properties.json has "properties" within "properties" at /properties/parent',
t('tests/fixtures/schema/deep_properties.json has "properties" within "properties" at /properties/parent'),
]
assert errors == len(records) == 1

Expand All @@ -129,7 +134,7 @@ def test_validate_items_type():
allow_invalid={'/properties/allow/items'})

assert sorted(str(record.message) for record in records) == [
'tests/fixtures/schema/items_type.json includes "object" in "items/type" at /properties/fail/items',
t('tests/fixtures/schema/items_type.json includes "object" in "items/type" at /properties/fail/items'),
]
assert errors == len(records) == 1

Expand All @@ -139,10 +144,10 @@ def test_validate_letter_case():
errors = validate('letter_case', property_exceptions={'Allow'}, definition_exceptions={'allow'})

assert sorted(str(record.message) for record in records) == [
"tests/fixtures/schema/letter_case.json: /definitions/Fail_Phrase block isn't UpperCamelCase ASCII letters",
"tests/fixtures/schema/letter_case.json: /definitions/fail block isn't UpperCamelCase ASCII letters",
"tests/fixtures/schema/letter_case.json: /properties/Fail field isn't lowerCamelCase ASCII letters",
"tests/fixtures/schema/letter_case.json: /properties/fail_phrase field isn't lowerCamelCase ASCII letters",
t("tests/fixtures/schema/letter_case.json: /definitions/Fail_Phrase block isn't UpperCamelCase ASCII letters"),
t("tests/fixtures/schema/letter_case.json: /definitions/fail block isn't UpperCamelCase ASCII letters"),
t("tests/fixtures/schema/letter_case.json: /properties/Fail field isn't lowerCamelCase ASCII letters"),
t("tests/fixtures/schema/letter_case.json: /properties/fail_phrase field isn't lowerCamelCase ASCII letters"),
]
assert errors == len(records) == 4

Expand All @@ -152,10 +157,10 @@ def test_validate_merge_properties():
errors = validate('merge_properties')

assert sorted(str(record.message) for record in records) == [
'tests/fixtures/schema/merge_properties.json sets "omitWhenMerged" to false or null at /properties/omitWhenMergedFalse', # noqa
'tests/fixtures/schema/merge_properties.json sets "wholeListMerge" to false or null at /properties/wholeListMergeFalse', # noqa
'tests/fixtures/schema/merge_properties.json sets "wholeListMerge", though the field is not an array of objects, at /properties/array', # noqa
'tests/fixtures/schema/merge_properties.json sets both "omitWhenMerged" and "wholeListMerge" at /properties/both', # noqa
t('tests/fixtures/schema/merge_properties.json sets "omitWhenMerged" to false or null at /properties/omitWhenMergedFalse'), # noqa: E501
t('tests/fixtures/schema/merge_properties.json sets "wholeListMerge" to false or null at /properties/wholeListMergeFalse'), # noqa: E501
t('tests/fixtures/schema/merge_properties.json sets "wholeListMerge", though the field is not an array of objects, at /properties/array'), # noqa: E501
t('tests/fixtures/schema/merge_properties.json sets both "omitWhenMerged" and "wholeListMerge" at /properties/both'), # noqa: E501
]
assert errors == len(records) == 4

Expand All @@ -168,9 +173,9 @@ def allow_missing(pointer):
errors = validate('metadata_presence', allow_missing=allow_missing)

assert sorted(str(record.message) for record in records) == [
'tests/fixtures/schema/metadata_presence.json is missing "description" at /properties/fail',
'tests/fixtures/schema/metadata_presence.json is missing "title" at /properties/fail',
'tests/fixtures/schema/metadata_presence.json is missing "type" or "$ref" or "oneOf" at /properties/fail',
t('tests/fixtures/schema/metadata_presence.json is missing "description" at /properties/fail'),
t('tests/fixtures/schema/metadata_presence.json is missing "title" at /properties/fail'),
t('tests/fixtures/schema/metadata_presence.json is missing "type" or "$ref" or "oneOf" at /properties/fail'),
]
assert errors == len(records) == 3

Expand All @@ -180,10 +185,10 @@ def test_validate_null_type():
errors = validate('null_type')

assert sorted(str(record.message) for record in records) == [
'tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failObject',
'tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failObjectArray',
'tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failRequired',
'tests/fixtures/schema/null_type.json is missing "null" in "type" at /properties/failOptional',
t('tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failObject'),
t('tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failObjectArray'),
t('tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failRequired'),
t('tests/fixtures/schema/null_type.json is missing "null" in "type" at /properties/failOptional'),
]
assert errors == len(records) == 4

Expand All @@ -193,10 +198,10 @@ def test_validate_null_type_no_null():
errors = validate('null_type', no_null=True)

assert sorted(str(record.message) for record in records) == [
'tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failObject',
'tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failObjectArray',
'tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failRequired',
'tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/passOptional',
t('tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failObject'),
t('tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failObjectArray'),
t('tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/failRequired'),
t('tests/fixtures/schema/null_type.json includes "null" in "type" at /properties/passOptional'),
]
assert errors == len(records) == 4

Expand All @@ -211,16 +216,16 @@ def allow_missing(pointer):
allow_optional='/properties/allowOptional')

assert sorted(str(record.message) for record in records) == [
'tests/fixtures/schema/object_id.json is missing "id" in "items/properties" at /definitions/Missing (from /refMissing)', # noqa
'tests/fixtures/schema/object_id.json is missing "id" in "items/properties" at /properties/missing',
'tests/fixtures/schema/object_id.json is missing "id" in "items/required" at /definitions/Optional (from /refOptional)', # noqa
'tests/fixtures/schema/object_id.json is missing "id" in "items/required" at /properties/optional',
t('tests/fixtures/schema/object_id.json is missing "id" in "items/properties" at /definitions/Missing (from /refMissing)'), # noqa
t('tests/fixtures/schema/object_id.json is missing "id" in "items/properties" at /properties/missing'),
t('tests/fixtures/schema/object_id.json is missing "id" in "items/required" at /definitions/Optional (from /refOptional)'), # noqa
t('tests/fixtures/schema/object_id.json is missing "id" in "items/required" at /properties/optional'),
]
assert errors == len(records) == 4


def test_validate_ref_pass():
filepath = 'schema/schema.json'
filepath = os.path.join('schema', 'schema.json')
with pytest.warns(None) as records:
errors = validate_ref(path(filepath), parse(filepath))

Expand All @@ -232,7 +237,7 @@ def test_validate_ref_fail():
errors = validate('ref')

assert sorted(str(record.message) for record in records) == [
"tests/fixtures/schema/ref.json has Unresolvable JSON pointer: '/definitions/Fail' at properties/fail",
t("tests/fixtures/schema/ref.json has Unresolvable JSON pointer: '/definitions/Fail' at properties/fail"),
]
assert errors == len(records) == 1

Expand All @@ -248,7 +253,7 @@ def test_validate_schema():


def test_validate_schema_codelists_match():
filepath = 'schema/codelist_enum.json'
filepath = os.path.join('schema', 'codelist_enum.json')
with pytest.warns(UserWarning) as records:
errors = validate_schema_codelists_match(path(filepath), parse(filepath), path('schema'))

Expand All @@ -261,7 +266,7 @@ def test_validate_schema_codelists_match():


def test_validate_schema_codelists_match_codelist():
filepath = 'schema/codelist_enum.json'
filepath = os.path.join('schema', 'codelist_enum.json')
with pytest.warns(UserWarning) as records:
errors = validate_schema_codelists_match(path(filepath), parse(filepath), path('schema'), is_extension=True,
external_codelists={'failOpenArray.csv', 'failOpenString.csv'})
Expand Down

0 comments on commit 145378d

Please sign in to comment.