def test_basic_loads(): kson.add_schema("""["schema", "basic", ["a", "b", "c"], [0, 0, "[]"]]""") data = kson.loads(""" ["[]basic", "foo", "bar", ["biz", "baz"], "one", "two", [1,2,3,4,5]] """) assert len(data) == 2 assert data[0] == {'a': "foo", 'b': "bar", 'c': ["biz", "baz"]} assert data[1] == {'a': "one", 'b': "two", 'c': [1, 2, 3, 4, 5]}
def test_codec_round_trip(): kson.add_schema("""[ "schema", "codec_test", ["c_field", "c_arr"], ["date|int36", "[]enum:a:b:c"] ]""") date = datetime(1955, 11, 5, 0, 0, 0) raw = kson.dumps({ 'c_field': date, 'c_arr': ["a", "a", "b", "b", "c", "a", "b", "a", "no enum"] }, "codec_test") assert raw == '["codec_test","-7dzxc0",[1,1,2,2,3,1,2,1,"no enum"]]' assert kson.loads(raw)['c_field'] == date
def test_schema_loading(): assert 'schema' in kson.SCHEMAS kson.add_schema({ 'id': 'dict_schema', 'fields': ['first', 'second'], 'meta': [0, 0] }) schema = kson.SCHEMAS['dict_schema'] assert schema['fields'] == ['first', 'second'] assert schema['meta'] == [0, 0] kson.add_schema(""" { "id": "json_schema", "fields": ["first", "second"], "meta": [0, 0] } """) schema = kson.SCHEMAS['json_schema'] assert schema['fields'] == ['first', 'second'] assert schema['meta'] == [0, 0] kson.add_schema(""" ["schema", "kson_schema", ["first", "second"], [0, 0]] """) schema = kson.SCHEMAS['kson_schema'] assert schema['fields'] == ['first', 'second'] assert schema['meta'] == [0, 0]
def test_schema_validation(): test_schema = {} try: kson.add_schema(test_schema) assert False, "fail for schema missing id" except ValueError: pass test_schema['id'] = 'test_schema' try: kson.add_schema(test_schema) assert False, "fail for schema missing fields" except ValueError: pass test_schema['fields'] = ['first', 'second'] try: kson.add_schema(test_schema) assert False, "fail for schema missing meta" except ValueError: pass test_schema['meta'] = [0] try: kson.add_schema(test_schema) assert False, "fail for 'fields', 'meta' missmatch" except AssertionError: pass test_schema['meta'] = [0, 0] kson.add_schema(test_schema) assert 'test_schema' in kson.SCHEMAS