2018-11-30 22:16:14 +03:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2019-02-12 17:47:26 +03:00
|
|
|
from spacy.util import get_json_validator, validate_json, validate_schema
|
2019-02-07 11:44:31 +03:00
|
|
|
from spacy.cli._schemas import META_SCHEMA, TRAINING_SCHEMA
|
2019-02-12 17:47:26 +03:00
|
|
|
from spacy.matcher._schemas import TOKEN_PATTERN_SCHEMA
|
2018-11-30 22:16:14 +03:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2019-02-12 17:47:26 +03:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def training_schema_validator():
|
|
|
|
return get_json_validator(TRAINING_SCHEMA)
|
|
|
|
|
|
|
|
|
2019-02-07 11:44:31 +03:00
|
|
|
def test_validate_schema():
|
|
|
|
validate_schema({"type": "object"})
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
validate_schema({"type": lambda x: x})
|
2018-11-30 22:16:14 +03:00
|
|
|
|
|
|
|
|
2019-02-12 17:47:26 +03:00
|
|
|
@pytest.mark.parametrize("schema", [TRAINING_SCHEMA, META_SCHEMA, TOKEN_PATTERN_SCHEMA])
|
2019-02-07 11:44:31 +03:00
|
|
|
def test_schemas(schema):
|
|
|
|
validate_schema(schema)
|
2018-11-30 22:16:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"data",
|
|
|
|
[
|
|
|
|
{"text": "Hello world"},
|
|
|
|
{"text": "Hello", "ents": [{"start": 0, "end": 5, "label": "TEST"}]},
|
|
|
|
],
|
|
|
|
)
|
2019-02-12 17:47:26 +03:00
|
|
|
def test_json_schema_training_valid(data, training_schema_validator):
|
|
|
|
errors = validate_json([data], training_schema_validator)
|
2018-11-30 22:16:14 +03:00
|
|
|
assert not errors
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"data,n_errors",
|
|
|
|
[
|
|
|
|
({"spans": []}, 1),
|
|
|
|
({"text": "Hello", "ents": [{"start": "0", "end": "5", "label": "TEST"}]}, 2),
|
|
|
|
({"text": "Hello", "ents": [{"start": 0, "end": 5}]}, 1),
|
|
|
|
({"text": "Hello", "ents": [{"start": 0, "end": 5, "label": "test"}]}, 1),
|
|
|
|
({"text": "spaCy", "tokens": [{"pos": "PROPN"}]}, 2),
|
|
|
|
],
|
|
|
|
)
|
2019-02-12 17:47:26 +03:00
|
|
|
def test_json_schema_training_invalid(data, n_errors, training_schema_validator):
|
|
|
|
errors = validate_json([data], training_schema_validator)
|
2018-11-30 22:16:14 +03:00
|
|
|
assert len(errors) == n_errors
|