def test_simple_enum_mapping(asset): source = load(asset("product.json")).select("cost.currency").definition target = load( asset("invoice.json")).select("lines.price.currency").definition assert isinstance(source, Enum) assert isinstance(target, Enum) m = Mapping(source, target) assert m.is_valid assert not m.errors
def test_different_value_schemas(asset): source = load(asset("product.json")).select("cost.amount").definition target = load( asset("invoice.json")).select("lines.price.currency").definition assert isinstance(source, IntegerSchema) assert isinstance(target, Enum) m = Mapping(source, target) assert not m.is_valid assert m.errors
def test_simple_string_mapping(asset): source = load(asset("product.json")).select("cost.amount").definition target = load( asset("invoice.json")).select("lines.price.amount").definition assert isinstance(source, IntegerSchema) assert isinstance(target, IntegerSchema) m = Mapping(source, target) assert m.is_valid assert not m.errors
def test_none_selectors(asset): schema = load(asset("invoice.json")) assert schema.select(None) is None assert schema.select() is None assert schema.trace(None) == [] trace = schema.trace(None, "lines") assert len(trace) == 0
def test_ensure_dereferenced_fragment_includes_local_references(asset): original_file = asset("invoice.json") d = load(original_file).to_dict(deref=True) assert "definitions" in d["properties"]["lines"]["items"]["properties"][ "kind"] assert "kindEnum" in d["properties"]["lines"]["items"]["properties"][ "kind"]["definitions"]
def test_incomplete_trace(asset): schema = load(asset("invoice.json")) trace = schema.trace("lines.price.amount2") assert len(trace) == 3 assert trace[0].name == "lines" and isinstance(trace[0].definition, ArraySchema) assert trace[1].name == "price" and isinstance(trace[1].definition, ObjectSchema) assert trace[2].name == "amount2" and trace[2].definition is None
def test_simple_enum_mapping(asset): source = load(asset("product.json")).select("cost.currency").definition target = Enum(enum=[ConstantValueSchema(value="EUR")]) assert isinstance(source, Enum) assert isinstance(target, Enum) m = Mapping(source, target) assert not m.is_valid assert m.errors
def test_tracing(asset): schema = load(asset("invoice.json")) trace = schema.trace("lines.price.amount") assert len(trace) == 3 assert trace[0].name == "lines" and isinstance(trace[0].definition, ArraySchema) assert trace[1].name == "price" and isinstance(trace[1].definition, ObjectSchema) assert trace[2].name == "amount" and isinstance(trace[2].definition, IntegerSchema)
def test_none_string_selectors(asset): schema = load(asset("invoice.json")) try: schema.select(1) assert False except ValueError: pass try: schema.select("lines", 1) assert False except ValueError: pass
def test_round_trip_spec(asset): original_file = asset("json-schema-draft-07.json") with open(original_file) as fp: original = json.load(fp) # load, parse and generate schema = load(original_file).to_dict() original_dump = json.dumps(original, indent=2, sort_keys=True) gen_dump = json.dumps(schema, indent=2, sort_keys=True) print(diff(original_dump, gen_dump)) assert original == schema
def test_deref_to_dict(asset): original_file = asset("invoice.json") d = load(original_file).to_dict(deref=True) # undereferenced top-level definition and 1st level dereferenced definition items = d["properties"]["lines"]["items"] assert items["properties"]["type"]["$ref"] == "#/definitions/type" assert items["properties"]["price"]["properties"]["taxed"]["$ref"] == \ "#/properties/lines/items/properties/price/definitions/taxed" # simple included type and 2nd-level dereferenced definition product = items["properties"]["product"] assert product["properties"]["id"]["type"] == "string" assert product["properties"]["cost"]["properties"]["taxed"]["$ref"] == \ "#/properties/lines/items/properties/product/properties/cost/definitions/taxed"
def test_dependency_discovery(asset): original_file = asset("invoice.json") schema = load(original_file) # invoice # kind # product # guid # money # currencies # money # currencies # ---------- # product, guid, money assert len(schema.dependencies()) == 3 # kind, product & money assert len( schema.dependencies(external=True)) == 5 # + guid and currencies
def test_ensure_dereferenced_combinations_generate_correct_ref_path(asset): original_file = asset("combination.json") d = load(original_file).to_dict(deref=True) assert d["properties"]["lines"]["anyOf"][1]["properties"]["subproduct"]["$ref"] == \ "#/properties/lines/anyOf/1"
def test_ensure_derereferenced_schemas_drop_local_ids(asset): original_file = asset("invoice.json") d = load(original_file).to_dict(deref=True) assert "$id" not in d["properties"]["lines"]["items"]["properties"][ "product"]