def test_is_literal(): assert json_schema.is_literal({'type': ['integer', 'null']}) assert json_schema.is_literal({'type': ['string']}) assert not json_schema.is_literal({ 'type': ['array'], 'items': { 'type': ['boolean'] } }) assert not json_schema.is_literal({})
def _denest_schema(table_path, table_json_schema, key_prop_schemas, subtables, level=-1): new_properties = {} for prop, item_json_schema in _denest_schema__singular_schemas( table_json_schema): if json_schema.is_object(item_json_schema): _denest_schema_helper(table_path + (prop, ), (prop, ), item_json_schema, json_schema.is_nullable(item_json_schema), new_properties, key_prop_schemas, subtables, level) elif json_schema.is_iterable(item_json_schema): _create_subtable(table_path + (prop, ), item_json_schema, key_prop_schemas, subtables, level + 1) elif json_schema.is_literal(item_json_schema): if (prop, ) in new_properties: new_properties[(prop, )]['anyOf'].append(item_json_schema) else: new_properties[(prop, )] = {'anyOf': [item_json_schema]} table_json_schema['properties'] = new_properties
def _denest_schema_helper(table_path, prop_path, table_json_schema, nullable, top_level_schema, key_prop_schemas, subtables, level): for prop, item_json_schema in _denest_schema__singular_schemas( table_json_schema): if json_schema.is_object(item_json_schema): _denest_schema_helper(table_path + (prop, ), prop_path + (prop, ), item_json_schema, nullable, top_level_schema, key_prop_schemas, subtables, level) elif json_schema.is_iterable(item_json_schema): _create_subtable(table_path + (prop, ), item_json_schema, key_prop_schemas, subtables, level + 1) elif json_schema.is_literal(item_json_schema): if nullable: item_json_schema = json_schema.make_nullable(item_json_schema) p = prop_path + (prop, ) if p in top_level_schema: top_level_schema[p]['anyOf'].append(item_json_schema) else: top_level_schema[p] = {'anyOf': [item_json_schema]}
def test__anyOf__schema__implicit_any_of__objects(): denested = error_check_denest( { 'properties': { 'every_type': { 'type': ['integer', 'null', 'number', 'boolean', 'string', 'array', 'object'], 'items': {'type': 'integer'}, 'format': 'date-time', 'properties': { 'i': {'anyOf': [ {'type': 'integer'}, {'type': 'number'}, {'type': 'boolean'}] } } } } }, [], []) assert 2 == len(denested) table_batch = _get_table_batch_with_path(denested, tuple()) denested_props = table_batch['streamed_schema']['schema']['properties'] print(denested_props) anyof_schemas = denested_props[('every_type', 'i')]['anyOf'] assert 3 == len(anyof_schemas) assert 3 == len([x for x in anyof_schemas if json_schema.is_literal(x)])
def test_complex_objects__logical_statements(): every_type = { 'type': ['integer', 'null', 'number', 'boolean', 'string', 'array', 'object'], 'items': { 'type': 'integer' }, 'format': 'date-time', 'properties': { 'a': { 'type': 'integer' }, 'b': { 'type': 'number' }, 'c': { 'type': 'boolean' } } } assert json_schema.is_iterable(every_type) assert json_schema.is_nullable(every_type) assert json_schema.is_literal(every_type) assert json_schema.is_object(every_type)
def test_is_literal(): # null is a weird value...it _is_ a literal...but... assert not json_schema.is_literal({'type': ['null']}) assert json_schema.is_literal({'type': ['integer', 'null']}) assert json_schema.is_literal({'type': ['integer', 'object', 'null']}) assert json_schema.is_literal({'type': ['string']}) assert not json_schema.is_literal({'type': ['array'], 'items': {'type': ['boolean']}}) assert not json_schema.is_literal({})
def _denest_schema_helper(table_path, table_json_schema, nullable, top_level_schema, key_prop_schemas, subtables, level): for prop, item_json_schema in table_json_schema['properties'].items(): if json_schema.is_object(item_json_schema): _denest_schema_helper(table_path + (prop, ), item_json_schema, nullable, top_level_schema, key_prop_schemas, subtables, level) if json_schema.is_iterable(item_json_schema): _create_subtable(table_path + (prop, ), item_json_schema, key_prop_schemas, subtables, level + 1) if json_schema.is_literal(item_json_schema): if nullable and not json_schema.is_nullable(item_json_schema): item_json_schema['type'].append('null') top_level_schema[table_path + (prop, )] = _literal_only_schema(item_json_schema)
def test__anyOf__schema__stitch_date_times(): denested = error_check_denest( {'properties': { 'a': { "anyOf": [ { "type": "string", "format": "date-time" }, {"type": ["string", "null"]}]}}}, [], []) table_batch = _get_table_batch_with_path(denested, tuple()) anyof_schemas = table_batch['streamed_schema']['schema']['properties'][('a',)]['anyOf'] assert 2 == len(anyof_schemas) assert 2 == len([x for x in anyof_schemas if json_schema.is_literal(x)]) assert 2 == len([x for x in anyof_schemas if json_schema.is_nullable(x)]) assert 1 == len([x for x in anyof_schemas if json_schema.is_datetime(x)])
def _denest_schema__singular_schemas(table_json_schema): ret = [] assert json_schema.is_object( table_json_schema ), 'Cannot denest non-object json_schema for tables. Passed: {}'.format( table_json_schema) for prop, sub_schema in table_json_schema['properties'].items(): singular_sub_schemas = [sub_schema] if json_schema.is_anyof(sub_schema): singular_sub_schemas = sub_schema['anyOf'] for s in singular_sub_schemas: assert json_schema.is_object(s) or json_schema.is_iterable(s) or json_schema.is_literal(s), \ 'Table schema cannot be denested due to: {} {}'.format( s, table_json_schema) ret.append((prop, s)) return ret
def _denest_schema(table_path, table_json_schema, key_prop_schemas, subtables, level=-1): new_properties = {} for prop, item_json_schema in table_json_schema['properties'].items(): if json_schema.is_object(item_json_schema): _denest_schema_helper(table_path + (prop, ), item_json_schema, json_schema.is_nullable(item_json_schema), new_properties, key_prop_schemas, subtables, level) if json_schema.is_iterable(item_json_schema): _create_subtable(table_path + (prop, ), item_json_schema, key_prop_schemas, subtables, level + 1) if json_schema.is_literal(item_json_schema): new_properties[(prop, )] = _literal_only_schema(item_json_schema) table_json_schema['properties'] = new_properties