Exemplo n.º 1
0
 def test_auto(self):
     schema = {
         "_id": {"type": "integer"},
         "name": {"type": "string"},
         "key": {"type": "string", "auto": lambda elem: elem.name.upper()},
         "key2": {"type": "string", "auto": lambda elem: elem.name.upper()},
         "subdoc": {"type": "dict", "schema": {
             "data": {"type":"integer"}
         }},
         "hash": {"type": "dict"},
         "num_list": {"type": "list", "schema": {"type": "integer"}},
         "doclist": {"type": "list", "schema": {"type": "dict", "schema": {
             "name": {"type":"string"},
             "title": {"type":"string", "auto_init": lambda elem: elem.get_parent().get_parent().name.upper()}
         }}},            
     }
     data = DBDoc({
         "name":"bob",
         "key":"fred",
         "subdoc": {
             "data": 1
         },
         "hash": {
             "data": 1,    
         },
         "num_list": [1,2],
         "doclist": [
             {"_id":1, "name": "fred", "title": "FRED"},
             {"name": "george"},
         ]
     })
     
     run_auto_funcs(schema, data)
 
     self.assertEqual(data, {
         "name":"bob",
         "key":"BOB",
         "key2":"BOB",
         "subdoc": {
             "data": 1
         },
         "hash": {
             "data": 1,    
         },
         "num_list": [1,2],
         "doclist": [
             {"_id":1, "name": "fred", "title": "FRED"},
             {"name": "george", "title": "BOB"},
         ]
     })
Exemplo n.º 2
0
    def process_insert(self, incoming):
        errs = enforce_datatypes(self.schema, incoming)
        if errs:
            return (None, errs)

        data = generate_prototype(self.schema)
        add_authstates(self.endpoint, data)
        enforce_auth(self.endpoint, data, incoming)
        merge(data, incoming)
        fill_in_prototypes(self.schema, data)
        run_auto_funcs(self.schema, data)

        errs = enforce_schema_behaviors(self.schema, data, self)
        if errs:
            return (None, errs)

        return (data, [])
Exemplo n.º 3
0
    def process_update(self, incoming):
        assert '_id' in incoming, "Cannot update document without _id attribute"

        errs = enforce_datatypes(self.schema, incoming)
        if errs:
            return (None, errs)

        data = self.coll.find_one({"_id": incoming["_id"]})
        add_authstates(self.endpoint, data)
        if not data._authstate['_edit']:
            remove_data(self.endpoint['schema'], incoming)
        enforce_auth(self.endpoint, data, incoming)
        merge(data, incoming)
        fill_in_prototypes(self.schema, data)
        run_auto_funcs(self.schema, data)

        errs = enforce_schema_behaviors(self.schema, data, self)
        if errs:
            return (None, errs)

        return (data, [])
Exemplo n.º 4
0
 def test_write_flow(self):
     schema = {
         "_id": {"type": "integer"},
         "name": {"type": "string"},
         "key": {"type": "string", "auto": lambda elem: elem.name.upper()},
         "key2": {"type": "string", "auto": lambda elem: elem.name.upper()},
         "subdoc": {"type": "dict", "schema": {
             "data": {"type":"integer"}
         }},
         "hash": {"type": "dict"},
         "num_list": {"type": "list", "schema": {"type": "integer"}},
         "doclist": {"type": "list", "schema": {"type": "dict", "schema": {
             "_id": {"type": "integer"},
             "name": {"type":"string"},
             "title": {"type":"string", "auto_init": lambda elem: elem.get_root().name.upper()}
         }}},            
     }
     data = DBDoc({
         "name":"fred",
         "key":"FRED",
         "hash": {
             "data": 1,    
         },
         "num_list": [1,2],
         "doclist": [
             {"_id":1, "name": "fred", "title": "GEORGE"},
         ]
     })
     incoming = {
         "name": "bob",
         "subdoc": {
             "data": "1"
         },
         "doclist": [
             {"_id":1},
             {"name": "amber"},
         ]            
     }
     enforce_datatypes(schema, incoming)
     merge(data, incoming)
     run_auto_funcs(schema, data)
     enforce_ids(data, 10)
     
     self.assertEqual(data, {
         "_id": 10,
         "name":"bob",
         "key":"BOB",
         "key2":"BOB",
         "subdoc": {
             "_id": 1,
             "data": 1
         },
         "hash": {
             "_id": 1,
             "data": 1,    
         },
         "num_list": [1,2],
         "doclist": [
             {"_id":1, "name": "fred", "title": "GEORGE"},
             {"_id":2, "name": "amber", "title": "BOB"},
         ]
     })
Exemplo n.º 5
0
    def test_write_flow(self):
        schema = {
            "_id": {
                "type": "integer"
            },
            "name": {
                "type": "string"
            },
            "key": {
                "type": "string",
                "auto": lambda elem: elem.name.upper()
            },
            "key2": {
                "type": "string",
                "auto": lambda elem: elem.name.upper()
            },
            "subdoc": {
                "type": "dict",
                "schema": {
                    "data": {
                        "type": "integer"
                    }
                }
            },
            "hash": {
                "type": "dict"
            },
            "num_list": {
                "type": "list",
                "schema": {
                    "type": "integer"
                }
            },
            "doclist": {
                "type": "list",
                "schema": {
                    "type": "dict",
                    "schema": {
                        "_id": {
                            "type": "integer"
                        },
                        "name": {
                            "type": "string"
                        },
                        "title": {
                            "type": "string",
                            "auto_init":
                            lambda elem: elem.get_root().name.upper()
                        }
                    }
                }
            },
        }
        data = DBDoc({
            "name":
            "fred",
            "key":
            "FRED",
            "hash": {
                "data": 1,
            },
            "num_list": [1, 2],
            "doclist": [
                {
                    "_id": 1,
                    "name": "fred",
                    "title": "GEORGE"
                },
            ]
        })
        incoming = {
            "name": "bob",
            "subdoc": {
                "data": "1"
            },
            "doclist": [
                {
                    "_id": 1
                },
                {
                    "name": "amber"
                },
            ]
        }
        enforce_datatypes(schema, incoming)
        merge(data, incoming)
        run_auto_funcs(schema, data)
        enforce_ids(data, 10)

        self.assertEqual(
            data, {
                "_id":
                10,
                "name":
                "bob",
                "key":
                "BOB",
                "key2":
                "BOB",
                "subdoc": {
                    "_id": 1,
                    "data": 1
                },
                "hash": {
                    "_id": 1,
                    "data": 1,
                },
                "num_list": [1, 2],
                "doclist": [
                    {
                        "_id": 1,
                        "name": "fred",
                        "title": "GEORGE"
                    },
                    {
                        "_id": 2,
                        "name": "amber",
                        "title": "BOB"
                    },
                ]
            })
Exemplo n.º 6
0
    def test_auto(self):
        schema = {
            "_id": {
                "type": "integer"
            },
            "name": {
                "type": "string"
            },
            "key": {
                "type": "string",
                "auto": lambda elem: elem.name.upper()
            },
            "key2": {
                "type": "string",
                "auto": lambda elem: elem.name.upper()
            },
            "subdoc": {
                "type": "dict",
                "schema": {
                    "data": {
                        "type": "integer"
                    }
                }
            },
            "hash": {
                "type": "dict"
            },
            "num_list": {
                "type": "list",
                "schema": {
                    "type": "integer"
                }
            },
            "doclist": {
                "type": "list",
                "schema": {
                    "type": "dict",
                    "schema": {
                        "name": {
                            "type": "string"
                        },
                        "title": {
                            "type":
                            "string",
                            "auto_init":
                            lambda elem: elem.get_parent().get_parent().name.
                            upper()
                        }
                    }
                }
            },
        }
        data = DBDoc({
            "name":
            "bob",
            "key":
            "fred",
            "subdoc": {
                "data": 1
            },
            "hash": {
                "data": 1,
            },
            "num_list": [1, 2],
            "doclist": [
                {
                    "_id": 1,
                    "name": "fred",
                    "title": "FRED"
                },
                {
                    "name": "george"
                },
            ]
        })

        run_auto_funcs(schema, data)

        self.assertEqual(
            data, {
                "name":
                "bob",
                "key":
                "BOB",
                "key2":
                "BOB",
                "subdoc": {
                    "data": 1
                },
                "hash": {
                    "data": 1,
                },
                "num_list": [1, 2],
                "doclist": [
                    {
                        "_id": 1,
                        "name": "fred",
                        "title": "FRED"
                    },
                    {
                        "name": "george",
                        "title": "BOB"
                    },
                ]
            })