示例#1
0
 def test_shape_with_extras(self):
     extras = {
         "$test": Shape(data={"number": 1})
     }
     s1 = Shape(extras=extras)
     self.assertEqual(extras["$test"], s1.get_prop("$test"))
     self.assertEqual(1, s1.get_prop("$test.number"))
示例#2
0
    def test_shape_expected_types(self):
        with self.assertRaises(TypeError): Shape(schema={"type": "object"}, data=[{"number": 1}])
        with self.assertRaises(TypeError): Shape(schema={"type": "array"}, data={"number": 1})

        with self.assertRaises(TypeError): Shape(schema={"type": "object"}, data=[1])

        Shape(schema={"type": "array"}, data=None)
        Shape(schema={"type": "object"}, data=None)
示例#3
0
    def test_shape_array_functions(self):
        schema = {
            "type": "object",
            "properties": {
                "list": {
                    "type": "array"
                }
            }
        }

        data = {
            "lIst": [
                {
                    "numbeR": 1
                },
                {
                    "numbeR": 2
                }
            ]
        }

        s1 = Shape(schema, data)
        self.assertEqual(1, s1.get_prop("list.$0.number"))
        self.assertEqual(2, s1.get_prop("list.$length"))
        self.assertEqual(1, s1.get_prop("list.$1.$index"))
        self.assertEqual(data["lIst"][1], s1.get_prop("list.$1").get_data())
        with self.assertRaises(KeyError): self.assertDictEqual(1, s1.get_prop("list.0.number"))
示例#4
0
    def test_shape_with_validation(self):
        schema = {
            "type": "object",
            "properties": {
                "number": {
                    "type": "integer"
                },
                "obj": {
                    "type": "object",
                    "properties": {
                        "number": {
                            "type": "integer"
                        }
                    }
                }
            }
        }

        validator = Draft4Validator(schema=schema, format_checker=FormatChecker())
        data = {
            "number": "a",
            "obj": {
                "number": "c"
            }
        }

        s1 = Shape(schema=schema, data=data, validator=validator)
        self.assertListEqual([{'message': "'a' is not of type 'integer'"}, {'message': "'c' is not of type 'integer'"}],
                             s1.validate())
示例#5
0
    def test_shape_with_allocation(self):
        schema = {
            "type": "object",
            "properties": {
                "obj1": {
                    "type": "object",
                    "properties": {
                        "list": {
                            "type": "array"
                        },
                        "obj2": {
                            "type": "object",
                            "properties": {
                                "obj3": {
                                    "type": "object"
                                },
                                "obj4": {
                                    "properties": {

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        s1 = Shape(schema=schema)
        self.assertEqual(Shape, type(s1.get_prop("obj1")))
        self.assertEqual(Shape, type(s1.get_prop("obj1.list")))
        self.assertEqual(Shape, type(s1.get_prop("obj1.obj2.obj3")))
        self.assertEqual(None, s1.get_prop("obj1.obj2.obj4"))
示例#6
0
    def test_shape_with_default_value(self):
        schema = {
            "type": "object",
            "properties": {
                "number": {
                    "default": 1
                }
            }
        }
        s1 = Shape(schema=schema)
        self.assertEqual(1, s1.get_prop("number"))

        data = {
            "number": 2
        }
        s1 = Shape(schema=schema, data=data)
        self.assertEqual(2, s1.get_prop("number"))
示例#7
0
    def test_shape_lower_case_keys_access(self):
        schema = {
            "type": "object",
            "properties": {
                "obj": {
                    "type": "object"
                },
                "list": {
                    "type": "array"
                }
            }
        }

        data = {
            "Number": 1,
            "obj": {
                "nuMber": 1
            },
            "lIst": [
                {
                    "numbeR": 1
                },
                {
                    "numbeR": 2
                }
            ],
            "obj1": {
                "numBer": 1
            },
            "array1": [1, 2]
        }

        s1 = Shape(schema=schema, data=data)
        self.assertEqual(1, s1.get_prop("number"))
        self.assertEqual(1, s1.get_prop("obj.number"))
        self.assertDictEqual({"numBer": 1}, s1.get_prop("obj1"))

        self.assertEqual(1, s1.get_prop("list.$0.number"))

        with self.assertRaises(KeyError): self.assertDictEqual(1, s1.get_prop("list.0"))
        with self.assertRaises(KeyError): self.assertDictEqual(1, s1.get_prop("list.0.number"))

        s1 = Shape(data=data)
        self.assertEqual(1, s1.get_prop("number"))
示例#8
0
    def test_shape_extras_with_validation(self):
        schema = {
            "type": "object",
            "properties": {
                "number": {
                    "type": "integer"
                }
            }
        }

        validator = Draft4Validator(schema=schema, format_checker=FormatChecker())
        data = {
            "number": "a"
        }
        e = Shape(schema=schema, data=data, validator=validator)
        extras = {"e": e}
        s1 = Shape(schema=schema, data=data, validator=validator, extras=extras)

        self.assertListEqual([{'message': "'a' is not of type 'integer'", 'name': 'e'}, {'message': "'a' is not of type 'integer'"}],
                             s1.validate(True))
示例#9
0
    def test_shape_set_property1(self):
        data = {
            "number": 1,
            "Number": 2
        }

        s1 = Shape(data=data)
        self.assertDictEqual(data, s1.get_data())

        s1.set_prop("Number", 1)
        self.assertDictEqual({'Number': 1}, s1.get_data())
示例#10
0
    def test_shape_no_access_to_non_defined_properties(self):
        schema = {
            "type": "object",
            "properties": {
                "obj": {
                    "type": "object"
                }
            }
        }

        data = {
            "Number": 1,
            "obj": {
                "nuMber": 1
            },
            "obj1": {
                "numBer": 1
            },
            "array1": [1, 2]
        }

        s1 = Shape(schema, data)
        self.assertDictEqual({"numBer": 1}, s1.get_prop("obj1"))
        with self.assertRaises(KeyError): self.assertDictEqual({"numBer": 1}, s1.get_prop("obj1.number"))
示例#11
0
    def test_shape_with_allocation1(self):
        schema = {
            "type": "object",
            "properties": {
                "obj1": {
                    "type": "object"
                },
                "obj2": {
                    # TODO: validation require to check type property in schema definition,
                    # can be implemented with json schema validation
                    "properties": {
                        "obj3": {
                            "type": "object"
                        }
                    }
                }
            }
        }

        data = {
            "obj1": {
                "number": 1
            },
            "obj2": {
                "number": 1,
                "obj3": {
                    "number": 1
                }
            }
        }

        s1 = Shape(schema=schema, data=data)
        self.assertEqual(Shape, type(s1.get_prop("obj1")))
        self.assertEqual(1, s1.get_prop("obj1.number"))
        self.assertEqual(dict, type(s1.get_prop("obj2")))
        with self.assertRaises(KeyError): self.assertEqual(None, type(s1.get_prop("obj2.obj3")))
示例#12
0
def create_context(descriptor,
                   payload=None,
                   query=None,
                   path_values=None,
                   header=None,
                   cookie=None):
    query_str, path_str, header_str, cookie_str, payload_str = "query", "path", "header", "cookie", "payload"

    model = descriptor.get("model")
    validators = descriptor.get("_validators")
    if model and validators:
        if query_str in model:
            query_schema = model[query_str]
            query_validator = validators[query_str]
        else:
            query_schema = None
            query_validator = None

        if path_str in model:
            path_schema = model[path_str]
            path_validator = validators[path_str]
        else:
            path_schema = None
            path_validator = None

        if header_str in model:
            header_schema = model[header_str]
            header_validator = validators[header_str]
        else:
            header_schema = None
            header_validator = None

        if cookie_str in model:
            cookie_schema = model[cookie_str]
            cookie_validator = validators[cookie_str]
        else:
            cookie_schema = None
            cookie_validator = None

        if payload_str in model:
            payload_schema = model[payload_str]
            payload_validator = validators[payload_str]
        else:
            payload_schema = None
            payload_validator = None
    else:
        query_schema = None
        query_validator = None
        path_schema = None
        path_validator = None
        header_schema = None
        header_validator = None
        cookie_schema = None
        cookie_validator = None
        payload_schema = None
        payload_validator = None

    query_shape = Shape(schema=query_schema, validator=query_validator)
    if query:
        for k, v in query.items():
            query_shape.set_prop(k, v)

    path_shape = Shape(schema=path_schema, validator=path_validator)
    if path_values:
        for k, v in path_values.items():
            path_shape.set_prop(k, v)

    header_shape = Shape(schema=header_schema,
                         validator=header_validator,
                         data=header)
    cookie_shape = Shape(schema=cookie_schema,
                         validator=cookie_validator,
                         data=cookie)

    request_extras = {
        "$query": query_shape,
        "$path": path_shape,
        "$header": header_shape,
        "$cookie": cookie_shape
    }

    request_data = {"id": str(uuid.uuid4())}
    request_shape = Shape(data=request_data, extras=request_extras)

    response_extras = {"$header": Shape(), "$cookie": Shape()}
    response_shape = Shape(extras=response_extras)

    vars = {"path": descriptor["path"]}
    vars_shape = Shape(data=vars)

    extras = {
        "$params": vars_shape,
        "$query": query_shape,
        "$path": path_shape,
        "$header": header_shape,
        "$cookie": cookie_shape,
        "$request": request_shape,
        "$response": response_shape
    }

    return Shape(schema=payload_schema,
                 validator=payload_validator,
                 data=payload,
                 extras=extras)
示例#13
0
 def test_shape_no_reserved_keywords(self):
     with self.assertRaises(ValueError): Shape(data={"$parent": "1"})
     with self.assertRaises(ValueError): Shape(data={"$length": "1"})
示例#14
0
    def test_shape_with_set_property(self):
        schema = {
            "type": "object",
            "properties": {
                "obj1": {
                    "type": "object",
                    "properties": {
                        "number": {
                            "format": "integer"
                        },
                        "obj2": {
                            "type": "object",
                            "properties": {
                                "number": {
                                    "type": "integer"
                                }
                            }
                        }
                    }
                },
                "list": {
                    "type": "array",
                    "properties": {
                        "number": {
                            "type": "integer"
                        }
                    }
                }
            }
        }

        data = {
            "obj1": {
                "number": 1
            },
            "list": [
                {
                    "number": 1
                }
            ]
        }

        s1 = Shape(schema=schema, data=data)

        s1.set_prop("obj1.number", 1)
        self.assertEqual(1, s1.get_prop("obj1.number"))

        s1.set_prop("obj1.obj2.number", 1)
        self.assertEqual(1, s1.get_prop("obj1.obj2.number"))

        s1.set_prop("obj1.obj2.number", "1")
        self.assertEqual(1, s1.get_prop("obj1.obj2.number"))

        with self.assertRaises(ValueError): s1.set_prop("obj1.obj2.number", "a")

        s1.set_prop("list.$0.number", 1)
        self.assertEqual(1, s1.get_prop("list.$0.number"))

        s1.set_prop("list.$0.number", "1")
        self.assertEqual(1, s1.get_prop("list.$0.number"))
        with self.assertRaises(ValueError): s1.set_prop("list.$0.number", "a")

        with self.assertRaises(IndexError): s1.set_prop("list.$1.number", "1")
        #self.assertEqual(1, s1.get_prop("list.$1.number"))

        s1.set_prop("list.$0.Number", 2)
        self.assertEqual(2, s1.get_prop("list.$0.number"))

        s1.set_prop("obj1.number", None)
        self.assertEqual(None, s1.get_prop("obj1.number"))
示例#15
0
    def test_shape_with_parent(self):
        with self.assertRaises(TypeError): self.assertDictEqual(Shape(parent_shape={}))

        parent = Shape()
        s1 = Shape(parent_shape=parent)
        self.assertEqual(parent, s1.get_prop("$parent"))