Esempio n. 1
1
 def test_multiple_schemas(self):
     s1 = {
         "required": [
             "a",
             "c"
         ],
         "type": "object",
         "properties": {
             "a": {
                 "type": "string"
             },
             "c": {
                 "items": [
                     {
                         "type": "integer"
                     }
                 ],
                 "type": "array"
             }
         }
     }
     s2 = {
         "required": [
             "c"
         ],
         "type": "object",
         "properties": {
             "a": {
                 "type": "string"
             },
             "c": {
                 "items": [
                     {
                         "type": "integer"
                     }
                 ],
                 "type": "array"
             }
         }
     }
     s = Schema()
     s.add_schema(s1)
     s.add_schema(s2)
     self.assertEqual(s.to_dict(), s2)
Esempio n. 2
0
 def test_multitype_merge(self):
     s = Schema().add_object([1, "2", None, False])
     self.assertEqual(s.to_dict(), {
         "type": "array",
         "items": [{
             "type": ["boolean", "integer", "null", "string"]}]
         })
Esempio n. 3
0
 def test_three_deep(self):
     s = Schema().add_object(
         {"matryoshka": {
             "design": {
                 "principle": "FTW!"
             }
         }})
     self.assertEqual(
         s.to_dict(), {
             "required": ["matryoshka"],
             "type": "object",
             "properties": {
                 "matryoshka": {
                     "required": ["design"],
                     "type": "object",
                     "properties": {
                         "design": {
                             "required": ["principle"],
                             "type": "object",
                             "properties": {
                                 "principle": {
                                     "type": "string"
                                 }
                             }
                         }
                     }
                 }
             }
         })
Esempio n. 4
0
 def test_object_in_array(self):
     s = Schema().add_object([{
         "name": "Sir Lancelot of Camelot",
         "quest": "to seek the Holy Grail",
         "favorite colour": "blue"
     }, {
         "name": "Sir Robin of Camelot",
         "quest": "to seek the Holy Grail",
         "capitol of Assyria": None
     }])
     self.assertEqual(
         s.to_dict(), {
             "items": [{
                 "required": ["name", "quest"],
                 "type": "object",
                 "properties": {
                     "quest": {
                         "type": "string"
                     },
                     "name": {
                         "type": "string"
                     },
                     "favorite colour": {
                         "type": "string"
                     },
                     "capitol of Assyria": {
                         "type": "null"
                     }
                 }
             }],
             "type":
             "array"
         })
Esempio n. 5
0
 def test_object_in_array(self):
     s = Schema().add_object([
         {"name": "Sir Lancelot of Camelot",
          "quest": "to seek the Holy Grail",
          "favorite colour": "blue"},
         {"name": "Sir Robin of Camelot",
          "quest": "to seek the Holy Grail",
          "capitol of Assyria": None}])
     self.assertEqual(s.to_dict(), {
         "items": [
             {
                 "required": [
                     "name",
                     "quest"
                 ],
                 "type": "object",
                 "properties": {
                     "quest": {
                         "type": "string"
                     },
                     "name": {
                         "type": "string"
                     },
                     "favorite colour": {
                         "type": "string"
                     },
                     "capitol of Assyria": {
                         "type": "null"
                     }
                 }
             }
         ],
         "type": "array"
     })
Esempio n. 6
0
 def test_monotype(self):
     s = Schema().add_object(["spam", "spam", "spam", "egg", "spam"])
     self.assertEqual(s.to_dict(), {
         "type": "array",
         "items": [{
             "type": "string"
         }]
     })
Esempio n. 7
0
 def test_multitype_merge(self):
     s = Schema().add_object([1, "2", None, False])
     self.assertEqual(
         s.to_dict(), {
             "type": "array",
             "items": [{
                 "type": ["boolean", "integer", "null", "string"]
             }]
         })
Esempio n. 8
0
 def test_multitype_sep(self):
     s = Schema(merge_arrays=False).add_object([1, "2", None, False])
     self.assertEqual(s.to_dict(), {
         "type": "array",
         "items": [
             {"type": "integer"},
             {"type": "string"},
             {"type": "null"},
             {"type": "boolean"}]
         })
Esempio n. 9
0
 def test_multi_type(self):
     s = Schema()
     s.add_object('string')
     s.add_object(1.1)
     s.add_object(True)
     s.add_object(None)
     self.assertEqual(s.to_dict(),
                      {'type': ['boolean', 'null', 'number', 'string']})
Esempio n. 10
0
 def test_array_reduce(self):
     s = Schema().add_object([["surprise"],
                              ["fear", "surprise"],
                              ["fear", "surprise", "ruthless efficiency"],
                              ["fear", "surprise", "ruthless efficiency",
                               "an almost fanatical devotion to the Pope"]])
     self.assertEqual(s.to_dict(), {
         "type": "array",
         "items": [{
             "type": "array",
             "items": [{"type": "string"}]}]
         })
Esempio n. 11
0
 def test_basic_object(self):
     s = Schema().add_object({
         "Red Windsor": "Normally, but today the van broke down.",
         "Stilton": "Sorry.",
         "Gruyere": False})
     self.assertEqual(s.to_dict(), {
         "required": ["Gruyere", "Red Windsor", "Stilton"],
         "type": "object",
         "properties": {
             "Red Windsor": {"type": "string"},
             "Gruyere": {"type": "boolean"},
             "Stilton": {"type": "string"}}
         })
Esempio n. 12
0
 def test_multi_type(self):
     s = Schema()
     s.add_object("string")
     s.add_object(1.1)
     s.add_object(True)
     s.add_object(None)
     self.assertEqual(s.to_dict(), {"type": ["boolean", "null", "number", "string"]})
Esempio n. 13
0
 def test_three_deep(self):
     s = Schema().add_object(
         {"matryoshka": {"design": {"principle": "FTW!"}}})
     self.assertEqual(s.to_dict(), {
         "required": ["matryoshka"],
         "type": "object",
         "properties": {"matryoshka": {
             "required": ["design"],
             "type": "object",
             "properties": {"design": {
                 "required": ["principle"],
                 "type": "object",
                 "properties": {"principle": {"type": "string"}}}}
             }}
         })
Esempio n. 14
0
 def test_multi_type(self):
     s = Schema()
     s.add_object('string')
     s.add_object(1.1)
     s.add_object(True)
     s.add_object(None)
     self.assertSchema(s.to_dict(),
                       {'type': ['boolean', 'null', 'number', 'string']})
Esempio n. 15
0
 def test_multitype_sep(self):
     s = Schema(merge_arrays=False).add_object([1, "2", None, False])
     self.assertEqual(
         s.to_dict(), {
             "type":
             "array",
             "items": [{
                 "type": "integer"
             }, {
                 "type": "string"
             }, {
                 "type": "null"
             }, {
                 "type": "boolean"
             }]
         })
Esempio n. 16
0
 def test_array_reduce(self):
     s = Schema().add_object([["surprise"], ["fear", "surprise"],
                              ["fear", "surprise", "ruthless efficiency"],
                              [
                                  "fear", "surprise", "ruthless efficiency",
                                  "an almost fanatical devotion to the Pope"
                              ]])
     self.assertEqual(
         s.to_dict(), {
             "type": "array",
             "items": [{
                 "type": "array",
                 "items": [{
                     "type": "string"
                 }]
             }]
         })
Esempio n. 17
0
 def test_array_in_object(self):
     s = Schema().add_object({"a": "b", "c": [1, 2, 3]})
     self.assertEqual(
         s.to_dict(), {
             "required": ["a", "c"],
             "type": "object",
             "properties": {
                 "a": {
                     "type": "string"
                 },
                 "c": {
                     "items": [{
                         "type": "integer"
                     }],
                     "type": "array"
                 }
             }
         })
Esempio n. 18
0
 def test_array_in_object(self):
     s = Schema().add_object({"a": "b", "c": [1, 2, 3]})
     self.assertEqual(s.to_dict(), {
         "required": [
             "a",
             "c"
         ],
         "type": "object",
         "properties": {
             "a": {
                 "type": "string"
             },
             "c": {
                 "items": [
                     {
                         "type": "integer"
                     }
                 ],
                 "type": "array"
             }
         }
     })
Esempio n. 19
0
 def test_basic_object(self):
     s = Schema().add_object({
         "Red Windsor": "Normally, but today the van broke down.",
         "Stilton": "Sorry.",
         "Gruyere": False
     })
     self.assertEqual(
         s.to_dict(), {
             "required": ["Gruyere", "Red Windsor", "Stilton"],
             "type": "object",
             "properties": {
                 "Red Windsor": {
                     "type": "string"
                 },
                 "Gruyere": {
                     "type": "boolean"
                 },
                 "Stilton": {
                     "type": "string"
                 }
             }
         })
Esempio n. 20
0
class SchemaTestCase(unittest.TestCase):

    def setUp(self):
        self._schema = Schema()
        self._objects = []
        self._schemas = []

    def set_schema_options(self, **options):
        self._schema = Schema(**options)

    def add_object(self, obj):
        self._schema.add_object(obj)
        self._objects.append(obj)

    def add_schema(self, schema):
        self._schema.add_schema(schema)
        self._schemas.append(schema)

    def assertObjectValidates(self, obj):
        jsonschema.Draft4Validator(self._schema.to_dict()).validate(obj)

    def assertObjectDoesNotValidate(self, obj):
        with self.assertRaises(jsonschema.exceptions.ValidationError):
            jsonschema.Draft4Validator(self._schema.to_dict()).validate(obj)

    def assertResult(self, expected):
        self.assertEqual(self._schema.to_dict(), expected)
        self.assertUserContract()

    def assertUserContract(self):
        self._assertSchemaIsValid()
        self._assertComponentObjectsValidate()

    def _assertSchemaIsValid(self):
        jsonschema.Draft4Validator.check_schema(self._schema.to_dict())

    def _assertComponentObjectsValidate(self):
        compiled_schema = self._schema.to_dict()
        for obj in self._objects:
            jsonschema.Draft4Validator(compiled_schema).validate(obj)
Esempio n. 21
0
class SchemaTestCase(unittest.TestCase):
    def setUp(self):
        self._schema = Schema()
        self._objects = []
        self._schemas = []

    def set_schema_options(self, **options):
        self._schema = Schema(**options)

    def add_object(self, obj):
        self._schema.add_object(obj)
        self._objects.append(obj)

    def add_schema(self, schema):
        self._schema.add_schema(schema)
        self._schemas.append(schema)

    def assertObjectValidates(self, obj):
        jsonschema.Draft4Validator(self._schema.to_dict()).validate(obj)

    def assertObjectDoesNotValidate(self, obj):
        with self.assertRaises(jsonschema.exceptions.ValidationError):
            jsonschema.Draft4Validator(self._schema.to_dict()).validate(obj)

    def assertResult(self, expected):
        self.assertEqual(self._schema.to_dict(), expected)
        self.assertUserContract()

    def assertUserContract(self):
        self._assertSchemaIsValid()
        self._assertComponentObjectsValidate()

    def _assertSchemaIsValid(self):
        jsonschema.Draft4Validator.check_schema(self._schema.to_dict())

    def _assertComponentObjectsValidate(self):
        compiled_schema = self._schema.to_dict()
        for obj in self._objects:
            jsonschema.Draft4Validator(compiled_schema).validate(obj)
Esempio n. 22
0
 def test_boolean(self):
     s = Schema().add_object(True)
     self.assertEqual(s.to_dict(), {"type": "boolean"})
Esempio n. 23
0
 def __init__(self, filename=None):
     self.__yaml = ""
     self.__json = ""
     self.__genson = Schema()
     if filename:
         self.open(filename)
Esempio n. 24
0
 def test_single_type_unicode(self):
     schema = {u'type': u'string'}
     s = Schema()
     s.add_schema(schema)
     self.assertSchema(s.to_dict(), schema)
Esempio n. 25
0
 def test_preserves_existing_keys(self):
     schema = {'type': 'number', 'value': 5}
     s = Schema()
     s.add_schema(schema)
     self.assertSchema(s.to_dict(), schema)
Esempio n. 26
0
 def test_empty(self):
     s = Schema().add_object([])
     self.assertEqual(s.to_dict(),
                      {"type": "array", "items": []})
Esempio n. 27
0
 def test_preserves_existing_keys(self):
     schema = {'type': 'number', 'value': 5}
     s = Schema()
     s.add_schema(schema)
     self.assertEqual(s.to_dict(), schema)
Esempio n. 28
0
 def test_empty_object(self):
     s = Schema().add_object({})
     self.assertEqual(s.to_dict(), {"type": "object", "properties": {}})
Esempio n. 29
0
 def test_single_type(self):
     s = Schema()
     s.add_object('bacon')
     s.add_object('egg')
     s.add_object('spam')
     self.assertSchema(s.to_dict(), {'type': 'string'})
Esempio n. 30
0
 def test_empty_object(self):
     s = Schema().add_object({})
     self.assertEqual(s.to_dict(), {"type": "object", "properties": {}})
Esempio n. 31
0
import sys
import json
from genson import Schema

if __name__ == "__main__":
    fctypeMap = {}

    with open("packetLog.txt", encoding="utf-8") as log:
        for line in log:
            # Strip off the timestamp and filename
            line = line[38:]
            packet = json.loads(line)
            if (packet["FCType"] == "MANAGELIST"
                or packet["FCType"] == "TAGS"
                or packet["FCType"] == "ROOMDATA"):
                # These packet types are too diverse
                # or overly unique and generate schemas
                # that are not particularly useful
                continue
            schema = fctypeMap.setdefault(packet["FCType"], Schema())
            sMessage = packet.setdefault("sMessage", None)
            schema.add_object(sMessage)
        full_schema = "{"
        for k, v in fctypeMap.items():
            full_schema += '"{}": {},'.format(k, v.to_json())
        full_schema = full_schema[:-1]
        full_schema += "}"
        with open("packetLogSchema.json", "w") as output:
            output.write(json.dumps(json.loads(full_schema), sort_keys=True, indent=4))
Esempio n. 32
0
 def test_number(self):
     s = Schema().add_object(1.1)
     self.assertEqual(s.to_dict(), {"type": "number"})
Esempio n. 33
0
 def test_boolean(self):
     s = Schema().add_object(True)
     self.assertEqual(s.to_dict(), {"type": "boolean"})
Esempio n. 34
0
 def test_monotype(self):
     s = Schema().add_object(["spam", "spam", "spam", "egg", "spam"])
     self.assertEqual(s.to_dict(),
                      {"type": "array", "items": [{"type": "string"}]})
Esempio n. 35
0
 def test_recurse_deprecation_warning(self):
     with self.assertWarns(DeprecationWarning):
         builder = Schema()
         builder.add_object('Go away or I shall taunt you a second time!')
         builder.to_dict(recurse=True)
Esempio n. 36
0
 def set_schema_options(self, **options):
     self._schema = Schema(**options)
Esempio n. 37
0
 def test_number(self):
     s = Schema().add_object(1.1)
     self.assertEqual(s.to_dict(), {"type": "number"})
Esempio n. 38
0
 def test_empty(self):
     s = Schema().add_object([])
     self.assertEqual(s.to_dict(), {"type": "array", "items": []})
Esempio n. 39
0
 def test_redundant_integer_type(self):
     s = Schema()
     s.add_object(1)
     s.add_object(1.1)
     self.assertSchema(s.to_dict(),
                       {'type': 'number'})
Esempio n. 40
0
 def setUp(self):
     self._schema = Schema()
     self._objects = []
     self._schemas = []
Esempio n. 41
0
 def test_multi_type(self):
     schema = {'type': ['boolean', 'null', 'number', 'string']}
     s = Schema()
     s.add_schema(schema)
     self.assertEqual(s.to_dict(), schema)
Esempio n. 42
0
 def test_single_type(self):
     schema = {'type': 'string'}
     s = Schema()
     s.add_schema(schema)
     self.assertEqual(s.to_dict(), schema)
Esempio n. 43
0
 def test_no_schema(self):
     schema = {}
     s = Schema()
     s.add_schema(schema)
     self.assertSchema(s.to_dict(), schema)
Esempio n. 44
0
 def test_single_type(self):
     schema = {'type': 'string'}
     s = Schema()
     s.add_schema(schema)
     self.assertSchema(s.to_dict(), schema)
Esempio n. 45
0
 def test_multi_type(self):
     schema = {'type': ['boolean', 'null', 'number', 'string']}
     s = Schema()
     s.add_schema(schema)
     self.assertSchema(s.to_dict(), schema)
Esempio n. 46
0
 def test_to_dict_pending_deprecation_warning(self):
     with self.assertWarns(PendingDeprecationWarning):
         builder = Schema()
     with self.assertWarns(PendingDeprecationWarning):
         builder.add_object('I fart in your general direction!')
         builder.to_dict()
Esempio n. 47
0
 def _new_schema(self, value):
     return Schema().add_object(value).to_dict()
Esempio n. 48
0
 def test_recurse_deprecation_warning(self):
     with self.assertWarns(DeprecationWarning):
         builder = Schema()
         builder.add_object('Go away or I shall taunt you a second time!')
         builder.to_dict(recurse=True)
Esempio n. 49
0
class Yaml2JsonSchema:
    schemas = list()
    stack = list()
    cur = str()

    def __init__(self, filename=None):
        self.__yaml = ""
        self.__json = ""
        self.__genson = Schema()
        if filename:
            self.open(filename)

    def is_valid(self, value):
        try:
            validate(value, Json.loads(self.yaml_to_jsonschema()))
            sys.stdout.write("OK\n")
        except ValidationError as ve:
            sys.stderr.write("Validation Error\n")
            sys.stderr.write(str(ve) + "\n")
            return False
        return True

    def to_jsonschema(self):
        return self.__genson.to_json(indent=2)

    def to_jsonloads(self):
        return Json.loads(self.json)

    def yaml_to_jsonschema(self):
        return self.ex_yaml(self.yaml.split('\n'))

    @property
    def json(self):
        return self.__json

    @property
    def yaml(self):
        return self.__yaml

    @yaml.setter
    def yaml(self, yaml):
        self.__yaml = yaml
        json = Yaml.load(self.yaml)
        self.__json = Json.dumps(json, indent=2)
        self.__genson.add_object(json)

    def open(self, filename):
        with open(filename, 'r') as f:
            data = f.read()
            self.yaml = data

    def ex_yaml(self, v):
        schema_list = Yaml2JsonSchema.yaml_extention_parser(v)
        for s in schema_list:
            self.__genson.add_schema(s)
        return self.to_jsonschema()

    @staticmethod
    def make_json_schema(v, value, buf=None):
        if buf is None:
            buf = dict()
        if not v:
            buf.update({"properties": value})
            return buf
        name = v.pop(0)
        buf.update({"properties": {name: dict()}})

        return Yaml2JsonSchema.make_json_schema(
            v, value, buf["properties"][name])

    @staticmethod
    def yaml_extention_parser(v, depth=0):
        # 데이터 아님
        while True:
            if not v:
                return Yaml2JsonSchema.schemas
            d = v.pop(0)
            if -1 == d.find(":"):
                continue
            break

        # 하위 아이템 존재
        if ":" == d[-1]:
            len_current = len(d) - len(d.lstrip())
            # 하위 아이템
            if depth <= len_current:
                depth = len_current
                name = d[:-1].strip()
                Yaml2JsonSchema.stack.append(name)

            # 상위로 돌아감
            elif depth > len_current:
                depth = len_current
                Yaml2JsonSchema.stack.pop()
            # 같은 레벨
            else:
                pass
        # 확장 코드
        elif -1 < d.find("#! "):
            info = d.strip()[3:]
            name = info[:info.find(":")]
            value = info[info.find(":") + 2:]
            if value.isdigit():
                value = int(value) if value.isdecimal() else float(value)
            value = {Yaml2JsonSchema.cur: {name: value}}
            buf = dict()
            cs = copy.deepcopy(Yaml2JsonSchema.stack)
            temp = buf
            Yaml2JsonSchema.make_json_schema(cs, value, temp)
            Yaml2JsonSchema.schemas.append(buf)
        else:
            d = d.strip()
            name = d[:d.find(":")]
            Yaml2JsonSchema.cur = name
        return Yaml2JsonSchema.yaml_extention_parser(v, depth)
Esempio n. 50
0
 def test_single_type(self):
     s = Schema()
     s.add_object("bacon")
     s.add_object("egg")
     s.add_object("spam")
     self.assertEqual(s.to_dict(), {"type": "string"})
Esempio n. 51
0
 def test_to_dict_pending_deprecation_warning(self):
     with self.assertWarns(PendingDeprecationWarning):
         builder = Schema()
     with self.assertWarns(PendingDeprecationWarning):
         builder.add_object('I fart in your general direction!')
         builder.to_dict()
Esempio n. 52
0
 def test_null(self):
     s = Schema().add_object(None)
     self.assertEqual(s.to_dict(), {"type": "null"})
Esempio n. 53
0
 def set_schema_options(self, **options):
     self._schema = Schema(**options)
Esempio n. 54
0
 def test_redundant_integer_type(self):
     s = Schema()
     s.add_object(1)
     s.add_object(1.1)
     self.assertEqual(s.to_dict(), {"type": "number"})
Esempio n. 55
0
 def setUp(self):
     self._schema = Schema()
     self._objects = []
     self._schemas = []
Esempio n. 56
0
 def test_no_schema(self):
     schema = {}
     s = Schema()
     s.add_schema(schema)
     self.assertEqual(s.to_dict(), schema)
Esempio n. 57
0
 def test_integer(self):
     s = Schema().add_object(1)
     self.assertEqual(s.to_dict(), {"type": "integer"})
Esempio n. 58
0
 def test_single_type_unicode(self):
     schema = {u'type': u'string'}
     s = Schema()
     s.add_schema(schema)
     self.assertEqual(s.to_dict(), schema)
Esempio n. 59
0
 def assertGenSchema(self, instance, options, expected):
     actual = Schema(**options).add_object(instance).to_dict()
     self.assertSchema(actual, expected)
     self.assertObjectValid(instance, actual)
     return actual
Esempio n. 60
0
 def test_null(self):
     s = Schema().add_object(None)
     self.assertEqual(s.to_dict(), {"type": "null"})