Beispiel #1
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())
Beispiel #2
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)
Beispiel #3
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"))