Skip to content

Commit

Permalink
Patch: closes: #3 - Also makes schema more explicit
Browse files Browse the repository at this point in the history
* use already loaded SCHEMA from kaiba.schema instead of reading file in tests
* Make sure that mappings is required based on if 'default' is present or not, and both should of course have 'name' required
* Make sure we have at least 1 item in mappings if default is not provided
* Add patch entry in CHANGELOG
* bump patch in pyproject
  • Loading branch information
thomasborgen committed Apr 5, 2021
1 parent 137eae4 commit 4f713db
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
| New Feature | minor |
| otherwise | patch |


## Version 1.0.0 Release: Kaiba

Kaiba is a data transformation tool written in Python that uses a DTL(Data Transformation Language) expressed in normal JSON to govern output structure and input transformation and mappings.
Expand All @@ -24,3 +25,11 @@ Kaiba is forked from the greenbird/piri @ version 2.2.0
* integer, decimal, iso date
* Regular Expressions - Regex
* Slicing


## Version 0.2.1 - Schema troubles

Fixes problems with Schema validation

* In attribute make sure either name + mappings or name + default is required
* In mappings make sure that length of path is above 1 if default is not provided.
37 changes: 26 additions & 11 deletions kaiba/schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "testid",
"$id": "#",
"title": "Kaiba configuration schema",
"$ref": "#/definitions/mapping_object",
"definitions": {
Expand Down Expand Up @@ -89,17 +89,17 @@
}
},
"slicing": {
"$ref": "#definitions/slicing",
"$ref": "#/definitions/slicing",
"default": {}
},
"regexp": {
"$ref": "#definitions/regexp",
"$ref": "#/definitions/regexp",
"default": {}
},
"if_statements": {
"type": "array",
"items": {
"$ref": "#definitions/if_statement"
"$ref": "#/definitions/if_statement"
},
"default": []
},
Expand All @@ -114,7 +114,15 @@
}
},
"then": {
"properties": {
"path": {
"minItems": 1
}
},
"required": ["path"]
},
"else": {
"required": ["default"]
}
},
"attribute": {
Expand All @@ -126,7 +134,7 @@
"mappings": {
"type": "array",
"items": {
"$ref": "#definitions/mapping"
"$ref": "#/definitions/mapping"
}
},
"separator": {
Expand All @@ -136,27 +144,34 @@
"if_statements": {
"type": "array",
"items": {
"$ref": "#definitions/if_statement"
"$ref": "#/definitions/if_statement"
},
"default": []
},
"casting": {
"$ref": "#definitions/casting",
"$ref": "#/definitions/casting",
"default": {}
},
"default": {
"type": ["string", "integer", "number", "boolean", "null"],
"default": null
}
},
"required": ["name"],
"if": {
"not": {
"required": ["default"]
}
},
"then": {
"required": ["mappings"]
"properties": {
"mappings": {
"minItems": 1
}
},
"required": ["name", "mappings"]
},
"else": {
"required": ["name", "default"]
}
},
"if_statement": {
Expand Down Expand Up @@ -204,7 +219,7 @@
"iterables": {
"type": "array",
"items": {
"$ref": "#definitions/iterable"
"$ref": "#/definitions/iterable"
},
"default": []
},
Expand Down Expand Up @@ -233,7 +248,7 @@
"iterables": {
"type": "array",
"items": {
"$ref": "#definitions/iterable"
"$ref": "#/definitions/iterable"
},
"default": []
},
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "kaiba"
version = "0.2.0"
version = "0.2.1"
description = "Configurable and documentable Json transformation and mapping"
authors = ["Thomas Borgen <thomas.borgen@greenbird.com>"]

Expand Down
8 changes: 2 additions & 6 deletions tests/schema/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

import pytest
from jsonschema import Draft7Validator, draft7_format_checker
from returns.result import safe

from kaiba.common import ReadLocalFile
from kaiba.schema import SchemaValidator
from kaiba.schema import SCHEMA, SchemaValidator


@pytest.fixture(scope='session')
def schema():
"""Get the schema."""
return ReadLocalFile()('kaiba/schema.json', 'r').bind(
safe(json.loads),
).unwrap()
return SCHEMA


@pytest.fixture(scope='session')
Expand Down
8 changes: 7 additions & 1 deletion tests/schema/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
from returns.pipeline import is_successful


def test_validates(mapping_validator, valid):
def test_validates_with_only_default(mapping_validator):
"""Test that we get dict back on valid validation."""
test: dict = {'default': 'test'}
assert mapping_validator(test).unwrap() == test


def test_validates_with_only_path(mapping_validator):
"""Test that we validate ok when only supplying path."""
test: dict = {'path': ['bob'], 'if_statements': []}
assert mapping_validator(test).unwrap() == test


def test_invalid(mapping_validator, invalid):
"""Test that we get a list of errors."""
test: dict = {'if_statements': []}
Expand Down

0 comments on commit 4f713db

Please sign in to comment.