-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from reworkd/schema-validation
More additions to schema validation
- Loading branch information
Showing
4 changed files
with
202 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "harambe-sdk" | ||
version = "0.10.0" | ||
version = "0.11.0" | ||
description = "Data extraction SDK for Playwright 🐒🍌" | ||
authors = ["awtkns <[email protected]>"] | ||
readme = "README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
from pydantic import ValidationError | ||
|
||
from harambe.parser.parser import PydanticSchemaParser | ||
from harambe.parser.parser import PydanticSchemaParser, SchemaValidationError | ||
import tests.parser.schemas as schemas | ||
from harambe.types import Schema | ||
|
||
|
@@ -56,37 +55,69 @@ | |
"address": {"street": None, "city": None, "zip": None}, | ||
}, | ||
), | ||
# TODO: Support lists and nested objects | ||
# (documents_schema, {"documents": []}), | ||
# | ||
# (documents_schema, { | ||
# "documents": [ | ||
# {"title": "Document One", "document_url": "http://example.com/doc1"}, | ||
# ] | ||
# }), | ||
# | ||
# (list_of_strings_schema, {"tags": ["python", "pydantic", "typing"]}), | ||
# | ||
# (list_of_objects_schema, | ||
# {"users": [{"name": "Alice", "email": "[email protected]"}, {"name": "Bob", "email": "[email protected]"}]}), | ||
# | ||
# (object_with_list_schema, {"team": {"name": "Developers", "members": ["Alice", "Bob"]}}), | ||
# | ||
# (list_of_lists_schema, {"matrix": [[1, 2], [3, 4]]}), | ||
# | ||
# (nested_lists_and_objects_schema, { | ||
# "departments": [ | ||
# { | ||
# "name": "Engineering", | ||
# "teams": [ | ||
# { | ||
# "team_name": "Backend", | ||
# "members": ["Alice", "Bob"] | ||
# } | ||
# ] | ||
# } | ||
# ] | ||
# }), | ||
( | ||
# Schema | ||
schemas.documents_schema, | ||
# Data | ||
{"documents": []}, | ||
), | ||
( | ||
# Schema | ||
schemas.documents_schema, | ||
# Data | ||
{ | ||
"documents": [ | ||
{ | ||
"title": "Document One", | ||
"document_url": "http://example.com/doc1", | ||
}, | ||
] | ||
}, | ||
), | ||
( | ||
# Schema | ||
schemas.list_of_strings_schema, | ||
# Data | ||
{"tags": ["python", "pydantic", "typing"]}, | ||
), | ||
( | ||
# Schema | ||
schemas.list_of_objects_schema, | ||
# Data | ||
{ | ||
"users": [ | ||
{"name": "Alice", "email": "[email protected]"}, | ||
{"name": "Bob", "email": "[email protected]"}, | ||
] | ||
}, | ||
), | ||
( | ||
# Schema | ||
schemas.object_with_list_schema, | ||
# Data | ||
{"team": {"name": "Developers", "members": ["Alice", "Bob"]}}, | ||
), | ||
( | ||
# Schema | ||
schemas.list_of_lists_schema, | ||
# Data | ||
{"matrix": [[1, 2], [3, 4]]}, | ||
), | ||
( | ||
# Schema | ||
schemas.nested_lists_and_objects_schema, | ||
# Data | ||
{ | ||
"departments": [ | ||
{ | ||
"name": "Engineering", | ||
"teams": [ | ||
{"team_name": "Backend", "members": ["Alice", "Bob"]} | ||
], | ||
} | ||
] | ||
}, | ||
), | ||
], | ||
) | ||
def test_pydantic_schema_validator_success( | ||
|
@@ -153,31 +184,97 @@ def test_pydantic_schema_validator_success( | |
"address": None, # ❌ No sub-fields | ||
}, | ||
), | ||
# TODO: Support lists and nested objects | ||
# (documents_schema, {"documents": None}), # Null list | ||
# (documents_schema, {"documents": [None]}), # Null item in list | ||
# (list_of_strings_schema, {"tags": [None, "pydantic", "typing"]}), # None in list of strings | ||
# (list_of_objects_schema, {"users": [{"name": "Alice", "email": 12345}]}), # Invalid email type | ||
# (object_with_list_schema, {"team": {"name": "Developers", "members": [None]}}), # None in sub-list | ||
# (list_of_lists_schema, {"matrix": [[1, "a"], [3, 4]]}), # Invalid type in nested list | ||
# (nested_lists_and_objects_schema, { | ||
# "departments": [ | ||
# { | ||
# "name": "Engineering", | ||
# "teams": [ | ||
# { | ||
# "team_name": "Backend", | ||
# "members": ["Alice", None] # None in nested object list | ||
# } | ||
# ] | ||
# } | ||
# ] | ||
# }), | ||
( | ||
# Schema | ||
schemas.documents_schema, | ||
# Data | ||
{ | ||
"documents": None # ❌ Null list | ||
}, | ||
), | ||
( | ||
# Schema | ||
schemas.documents_schema, | ||
# Data | ||
{ | ||
"documents": [ | ||
None # ❌ Null item in list | ||
] | ||
}, | ||
), | ||
( | ||
# Schema | ||
schemas.list_of_strings_schema, | ||
# Data | ||
{ | ||
"tags": [ | ||
None, # ❌ None in list of strings | ||
"pydantic", | ||
"typing", | ||
] | ||
}, | ||
), | ||
( | ||
# Schema | ||
schemas.list_of_objects_schema, | ||
# Data | ||
{ | ||
"users": [ | ||
{ | ||
"name": "Alice", | ||
"email": 12345, # ❌ Invalid email type | ||
} | ||
] | ||
}, | ||
), | ||
( | ||
# Schema | ||
schemas.object_with_list_schema, | ||
# Data | ||
{ | ||
"team": { | ||
"name": "Developers", | ||
"members": [None], # ❌ None in sub-list | ||
} | ||
}, | ||
), | ||
( | ||
# Schema | ||
schemas.list_of_lists_schema, | ||
# Data | ||
{ | ||
"matrix": [ | ||
[1, "a"], # ❌ Invalid type in nested list | ||
[3, 4], | ||
] | ||
}, | ||
), | ||
( | ||
# Schema | ||
schemas.nested_lists_and_objects_schema, | ||
# Data | ||
{ | ||
"departments": [ | ||
{ | ||
"name": "Engineering", | ||
"teams": [ | ||
{ | ||
"team_name": "Backend", | ||
"members": [ | ||
"Alice", | ||
None, # ❌ None in nested object list | ||
], | ||
} | ||
], | ||
} | ||
] | ||
}, | ||
), | ||
], | ||
) | ||
def test_pydantic_schema_validator_error(schema: Schema, data: dict[str, Any]) -> None: | ||
validator = PydanticSchemaParser(schema) | ||
with pytest.raises(ValidationError): | ||
with pytest.raises(SchemaValidationError): | ||
validator.validate(data) | ||
|
||
|
||
|