Skip to content
Snippets Groups Projects
Commit e540fd4d authored by Mateusz Kudela's avatar Mateusz Kudela
Browse files

Add schema reducer and cover it with tests

parent 1eeedcd5
No related branches found
No related tags found
1 merge request!24Schemas modification tools
from schemas.__private.fundamental_schemas import Array, ArrayStrict, Map, Schema
from schemas.__private.custom_schemas import ApiOperationObject, Proposal
from .local_tools import get_schema
def get_schema_to_reduce(source: Schema, path: str):
# Remove last element from path, we don't want to get schema that will be deleted. We want to get box which holds
# this element.
try:
path = path[:path.rindex('.')]
except ValueError:
path = 'source'
schema_to_reduce = get_schema(source, path)
return schema_to_reduce
def remove_schema_from_map(source: Schema, *, path: str = 'source'):
key_to_delete = path.split('.')[-1]
schema_to_reduce = get_schema_to_reduce(source, path)
assert isinstance(schema_to_reduce, (Map, ApiOperationObject, Proposal)), 'You are trying to remove schema from' \
' object which is not a Map, ' \
'ApiOperationObject or Proposal.'
del schema_to_reduce[key_to_delete]
def remove_schema_from_array(source: Schema, *, path: str = 'source'):
index_to_delete = int(path.split('.')[-1])
schema_to_reduce = get_schema_to_reduce(source, path)
assert isinstance(schema_to_reduce, (Array, ArrayStrict)), 'You are trying to remove schema from object which ' \
'is not an Array or ArrayStrict'
del schema_to_reduce[index_to_delete]
from schemas.__private.modify.schema_expander import add_schema_to_array, add_schema_to_map
from schemas.__private.modify.schema_reducer import remove_schema_from_map, remove_schema_from_array
from schemas.predefined import *
......@@ -108,3 +109,74 @@ def test_single_addition_to_array_nested_in_different_types():
)
)
def test_single_deletion_in_outer_map():
schema = Map({'number': Int()})
remove_schema_from_map(schema, path='number')
assert schema == Map({})
def test_single_deletion_in_inner_map():
schema = Map({
'inner_map': Map({
'id': Int()
})
})
remove_schema_from_map(schema, path='inner_map.id')
assert schema == Map({
'inner_map': Map({})
})
def test_single_deletion_in_map_nested_in_different_types():
schema = Array(
ArrayStrict(
Map({
'values': AnyOf(
Str(),
Int(),
Map({
'key_to_delete': Str(),
})
),
'name': AccountName(),
})
)
)
remove_schema_from_map(schema, path='0.0.values.2.key_to_delete')
assert schema == Array(
ArrayStrict(
Map({
'values': AnyOf(
Str(),
Int(),
Map({
})
),
'name': AccountName(),
})
)
)
def test_single_deletion_in_outer_array():
schema = Array(Int(), Str())
remove_schema_from_array(schema, path='1')
assert schema == Array(Int())
def test_single_deletion_in_inner_array():
schema = Array(
Array(
Hex(), Str(), Int(),
))
remove_schema_from_array(schema, path='0.2')
assert schema == Array(
Array(
Hex(), Str(),
)
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment