def test_enum(): items = [{"a": "alpha"}, {"a": "ah"}] schema = json_schema_generator.process_to_schema(items).to_json() print(json.dumps(schema, indent=2, sort_keys=True)) assert schema["properties"]["a"]["type"] == "string" assert "enum" in schema["properties"]["a"] assert ["ah", "alpha"] == schema["properties"]["a"]["enum"]
def test_random_content(): street_prefix = ("High", "Station", "Hill", "Owl", "Badger", "Civit") street_suffix = ("Street", "Lane", "Drive", "Road") names = ("Alice", "Bob", "Carol", "David", "Emma", "Fred", "Gini", "Harry", "Irene", "John") def random_address(rng): return { "street": rng.choice(street_prefix) + " " + rng.choice(street_suffix) } def random_name(rng): return rng.choice(names) def random_content(rng): return { "from": { "name": random_name(rng), "address": random_address(rng) }, "to": { "name": random_name(rng), "address": random_address(rng) } } # create some content rng = random.Random(42) source = [random_content(rng) for i in range(25)] schema = process_to_schema(source) print(json.dumps(schema.to_json(), indent=2, sort_keys=True))
def test_types(): items = [{ "a": True, "b": "Hello world", "c": 1, "d": 1.5, "e": None, "f": [], "g": {} }] schema = json_schema_generator.process_to_schema(items).to_json() print(json.dumps(schema, indent=2, sort_keys=True)) assert schema["properties"]["a"]["type"] == "boolean" assert schema["properties"]["b"]["type"] == "string" assert schema["properties"]["c"]["type"] == "integer" assert schema["properties"]["d"]["type"] == "number" assert schema["properties"]["e"]["type"] == "null" assert schema["properties"]["f"]["type"] == "array" assert schema["properties"]["g"]["type"] == "object"
def test_combining_types(): items = [{ "a": True, "b": "Hello world", "c": 1, "d": 1.5, "e": None, }, { "b": True, "c": "Hello world", "d": 1, "e": 1.5, "a": None, }] schema = json_schema_generator.process_to_schema(items).to_json() print(json.dumps(schema, indent=2, sort_keys=True)) # because we are mixing types there cannot be any type validation assert "type" not in schema["properties"]["a"] assert "type" not in schema["properties"]["b"] assert "type" not in schema["properties"]["c"] assert "type" not in schema["properties"]["d"] assert "type" not in schema["properties"]["e"]