예제 #1
0
파일: common.py 프로젝트: ride90/eve
    def test_serialize_null_list(self):
        schema = {
            "nullable_list": {
                "type": "list",
                "nullable": True,
                "schema": {"type": "objectid"},
            }
        }
        doc = {"nullable_list": None}
        with self.app.app_context():
            try:
                serialize(doc, schema=schema)
            except Exception:
                self.fail("Serializing null lists" " should not raise an exception")

        schema = {
            "nullable_list": {
                "type": "list",
                "nullable": True,
                "schema": {"type": "dbref"},
            }
        }
        doc = {"nullable_list": None}
        with self.app.app_context():
            try:
                serialize(doc, schema=schema)
            except Exception:
                self.fail("Serializing null lists" " should not raise an exception")
예제 #2
0
파일: common.py 프로젝트: jazzlly/eve
    def test_serialize_lists_of_lists(self):
        # serialize should handle list of lists of basic types
        schema = {
            'l_of_l': {
                'type': 'list',
                'schema': {
                    'type': 'list',
                    'schema': {
                        'type': 'objectid'
                    }
                }
            }
        }
        doc = {
            'l_of_l': [
                ['50656e4538345b39dd0414f0', '50656e4538345b39dd0414f0'],
                ['50656e4538345b39dd0414f0', '50656e4538345b39dd0414f0']
            ]
        }

        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        for sublist in serialized['l_of_l']:
            for item in sublist:
                self.assertTrue(isinstance(item, ObjectId))

        # serialize should handle list of lists of dicts
        schema = {
            'l_of_l': {
                'type': 'list',
                'schema': {
                    'type': 'list',
                    'schema': {
                        'type': 'dict',
                        'schema': {
                            '_id': {
                                'type': 'objectid'
                            }
                        }
                    }
                }
            }
        }
        doc = {
            'l_of_l': [
                [
                    {'_id': '50656e4538345b39dd0414f0'},
                    {'_id': '50656e4538345b39dd0414f0'}
                ],
                [
                    {'_id': '50656e4538345b39dd0414f0'},
                    {'_id': '50656e4538345b39dd0414f0'}
                ],
            ]
        }
        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        for sublist in serialized['l_of_l']:
            for item in sublist:
                self.assertTrue(isinstance(item['_id'], ObjectId))
예제 #3
0
    def test_serialize_null_list(self):
        schema = {
            'nullable_list': {
                'type': 'list',
                'nullable': True,
                'schema': {
                    'type': 'objectid'
                }
            }
        }
        doc = {'nullable_list': None}
        with self.app.app_context():
            try:
                serialize(doc, schema=schema)
            except Exception:
                self.fail('Serializing null lists'
                          ' should not raise an exception')

        schema = {
            'nullable_list': {
                'type': 'list',
                'nullable': True,
                'schema': {
                    'type': 'dbref'
                }
            }
        }
        doc = {'nullable_list': None}
        with self.app.app_context():
            try:
                serialize(doc, schema=schema)
            except Exception:
                self.fail('Serializing null lists'
                          ' should not raise an exception')
예제 #4
0
    def test_serialize_array_of_tipes(self):
        # see #1112.
        schema = {
            "val": {
                "type": "dict",
                "schema": {
                    "x": {
                        "type": ["string", "number"]
                    },
                    "timestamp": {
                        "type": "datetime"
                    },
                },
            }
        }

        doc = {"val": {"x": "1", "timestamp": "Tue, 06 Nov 2012 10:33:31 GMT"}}
        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        self.assertEqual(serialized["val"]["x"], 1)
        self.assertTrue(isinstance(serialized["val"]["timestamp"], datetime))

        doc = {"val": {"x": "s", "timestamp": "Tue, 06 Nov 2012 10:33:31 GMT"}}
        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        self.assertEqual(serialized["val"]["x"], "s")
        self.assertTrue(isinstance(serialized["val"]["timestamp"], datetime))
예제 #5
0
파일: common.py 프로젝트: kidaa/eve
    def test_serialize_lists_of_lists(self):
        # serialize should handle list of lists of basic types
        schema = {
            'l_of_l': {
                'type': 'list',
                'schema': {
                    'type': 'list',
                    'schema': {
                        'type': 'objectid'
                    }
                }
            }
        }
        doc = {
            'l_of_l': [
                ['50656e4538345b39dd0414f0', '50656e4538345b39dd0414f0'],
                ['50656e4538345b39dd0414f0', '50656e4538345b39dd0414f0']
            ]
        }

        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        for sublist in serialized['l_of_l']:
            for item in sublist:
                self.assertTrue(isinstance(item, ObjectId))

        # serialize should handle list of lists of dicts
        schema = {
            'l_of_l': {
                'type': 'list',
                'schema': {
                    'type': 'list',
                    'schema': {
                        'type': 'dict',
                        'schema': {
                            '_id': {
                                'type': 'objectid'
                            }
                        }
                    }
                }
            }
        }
        doc = {
            'l_of_l': [
                [
                    {'_id': '50656e4538345b39dd0414f0'},
                    {'_id': '50656e4538345b39dd0414f0'}
                ],
                [
                    {'_id': '50656e4538345b39dd0414f0'},
                    {'_id': '50656e4538345b39dd0414f0'}
                ],
            ]
        }
        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        for sublist in serialized['l_of_l']:
            for item in sublist:
                self.assertTrue(isinstance(item['_id'], ObjectId))
예제 #6
0
파일: common.py 프로젝트: ride90/eve
    def test_serialize_lists_of_lists(self):
        # serialize should handle list of lists of basic types
        schema = {
            "l_of_l": {
                "type": "list",
                "schema": {"type": "list", "schema": {"type": "objectid"}},
            }
        }
        doc = {
            "l_of_l": [
                ["50656e4538345b39dd0414f0", "50656e4538345b39dd0414f0"],
                ["50656e4538345b39dd0414f0", "50656e4538345b39dd0414f0"],
            ]
        }

        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        for sublist in serialized["l_of_l"]:
            for item in sublist:
                self.assertTrue(isinstance(item, ObjectId))

        # serialize should handle list of lists of dicts
        schema = {
            "l_of_l": {
                "type": "list",
                "schema": {
                    "type": "list",
                    "schema": {"type": "dict", "schema": {"_id": {"type": "objectid"}}},
                },
            }
        }
        doc = {
            "l_of_l": [
                [
                    {"_id": "50656e4538345b39dd0414f0"},
                    {"_id": "50656e4538345b39dd0414f0"},
                ],
                [
                    {"_id": "50656e4538345b39dd0414f0"},
                    {"_id": "50656e4538345b39dd0414f0"},
                ],
            ]
        }
        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        for sublist in serialized["l_of_l"]:
            for item in sublist:
                self.assertTrue(isinstance(item["_id"], ObjectId))
예제 #7
0
 def test_serialize_subdocument(self):
     # tests fix for #244, serialization of sub-documents.
     schema = {
         'personal': {
             'type': 'dict',
             'schema': {
                 'best_friend': {
                     'type': 'objectid'
                 },
                 'born': {
                     'type': 'datetime'
                 }
             }
         },
         'without_type': {}
     }
     doc = {
         'personal': {
             'best_friend': '50656e4538345b39dd0414f0',
             'born': 'Tue, 06 Nov 2012 10:33:31 GMT'
         },
         'without_type': 'foo'
     }
     with self.app.app_context():
         serialized = serialize(doc, schema=schema)
     self.assertTrue(
         isinstance(serialized['personal']['best_friend'], ObjectId))
     self.assertTrue(isinstance(serialized['personal']['born'], datetime))
예제 #8
0
    def test_mongo_serializes(self):
        schema = {
            "id": {"type": "objectid"},
            "date": {"type": "datetime"},
            "count": {"type": "integer"},
            "average": {"type": "float"},
            "dict_keyschema": {"keyschema": {"type": "objectid"}},
        }
        with self.app.app_context():
            # Success
            res = serialize(
                {
                    "id": "50656e4538345b39dd0414f0",
                    "date": "Tue, 06 Nov 2012 10:33:31 GMT",
                    "count": 42,
                    "average": 42.42,
                    "dict_keyschema": {"foo1": "50656e4538345b39dd0414f0", "foo2": "50656e4538345b39dd0414f0"},
                },
                schema=schema,
            )
            self.assertTrue(isinstance(res["id"], ObjectId))
            self.assertTrue(isinstance(res["date"], datetime))
            self.assertTrue(isinstance(res["count"], int))
            self.assertTrue(isinstance(res["average"], float))

            ks = res["dict_keyschema"]
            self.assertTrue(isinstance(ks["foo1"], ObjectId))
            self.assertTrue(isinstance(ks["foo2"], ObjectId))

            # Fails
            self.assertRaises(InvalidId, serialize, **dict(document={"id": "test"}, schema=schema))
            self.assertRaises(ValueError, serialize, **dict(document={"date": "test"}, schema=schema))
            self.assertRaises(ValueError, serialize, **dict(document={"count": "10.0"}, schema=schema))
            self.assertRaises(ValueError, serialize, **dict(document={"average": "test"}, schema=schema))
예제 #9
0
    def test_non_blocking_on_simple_field_serialization_exception(self):
        schema = {
            'extract_time': {
                'type': 'datetime'
            },
            'date': {
                'type': 'datetime'
            },
            'total': {
                'type': 'integer'
            }
        }

        with self.app.app_context():
            # Success
            res = serialize(
                {
                    'extract_time': 'Tue, 06 Nov 2012 10:33:31 GMT',
                    'date': 'Tue, 06 Nov 2012 10:33:31 GMT',
                    'total': 'r123'
                },
                schema=schema)
            # this has been left untouched as it could not be serialized.
            self.assertEqual(res['total'], 'r123')
            # these have been both serialized.
            self.assertTrue(isinstance(res['extract_time'], datetime))
            self.assertTrue(isinstance(res['date'], datetime))
예제 #10
0
파일: common.py 프로젝트: ride90/eve
 def test_serialize_inside_x_of_rules(self):
     for x_of in ["allof", "anyof", "oneof", "noneof"]:
         schema = {"x_of-field": {x_of: [{"type": "objectid"}, {"required": True}]}}
         doc = {"x_of-field": "50656e4538345b39dd0414f0"}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized["x_of-field"], ObjectId))
예제 #11
0
파일: common.py 프로젝트: nicolaiarocci/eve
 def test_serialize_inside_nested_x_of_rules(self):
     schema = {
         'nested-x_of-field': {
             'oneof': [
                 {
                     'anyof': [
                         {'type': 'objectid'},
                         {'type': 'datetime'}
                     ],
                     'required': True
                 },
                 {
                     'allof': [
                         {'type': 'boolean'},
                         {'required': True}
                     ]
                 }
             ]
         }
     }
     doc = {'nested-x_of-field': '50656e4538345b39dd0414f0'}
     with self.app.app_context():
         serialized = serialize(doc, schema=schema)
         self.assertTrue(
             isinstance(serialized['nested-x_of-field'], ObjectId))
예제 #12
0
 def test_serialize_list_alongside_x_of_rules(self):
     for x_of in ["allof", "anyof", "oneof", "noneof"]:
         schema = {
             "x_of-field": {
                 "type":
                 "list",
                 x_of: [
                     {
                         "schema": {
                             "type": "objectid"
                         }
                     },
                     {
                         "schema": {
                             "type": "datetime"
                         }
                     },
                 ],
             }
         }
         doc = {"x_of-field": ["50656e4538345b39dd0414f0"]}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(
                 isinstance(serialized["x_of-field"][0], ObjectId))
예제 #13
0
파일: common.py 프로젝트: jazzlly/eve
 def test_serialize_inside_nested_x_of_rules(self):
     schema = {
         'nested-x_of-field': {
             'oneof': [
                 {
                     'anyof': [
                         {'type': 'objectid'},
                         {'type': 'datetime'}
                     ],
                     'required': True
                 },
                 {
                     'allof': [
                         {'type': 'boolean'},
                         {'required': True}
                     ]
                 }
             ]
         }
     }
     doc = {'nested-x_of-field': '50656e4538345b39dd0414f0'}
     with self.app.app_context():
         serialized = serialize(doc, schema=schema)
         self.assertTrue(
             isinstance(serialized['nested-x_of-field'], ObjectId))
예제 #14
0
    def test_mongo_serializes(self):
        schema = {
            'id': {'type': 'objectid'},
            'date': {'type': 'datetime'},
            'count': {'type': 'integer'},
            'average': {'type': 'float'},
            'dict_valueschema': {
                'valueschema': {'type': 'objectid'}
            }
        }
        with self.app.app_context():
            # Success
            res = serialize(
                {
                    'id': '50656e4538345b39dd0414f0',
                    'date': 'Tue, 06 Nov 2012 10:33:31 GMT',
                    'count': 42,
                    'average': 42.42,
                    'dict_valueschema': {
                        'foo1': '50656e4538345b39dd0414f0',
                        'foo2': '50656e4538345b39dd0414f0',
                    }
                },
                schema=schema
            )
            self.assertTrue(isinstance(res['id'], ObjectId))
            self.assertTrue(isinstance(res['date'], datetime))
            self.assertTrue(isinstance(res['count'], int))
            self.assertTrue(isinstance(res['average'], float))

            ks = res['dict_valueschema']
            self.assertTrue(isinstance(ks['foo1'], ObjectId))
            self.assertTrue(isinstance(ks['foo2'], ObjectId))
예제 #15
0
파일: common.py 프로젝트: ride90/eve
 def test_serialize_number(self):
     schema = {"anumber": {"type": "number"}}
     for expected_type, value in [(int, "35"), (float, "3.5")]:
         doc = {"anumber": value}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized["anumber"], expected_type))
예제 #16
0
    def test_non_blocking_on_simple_field_serialization_exception(self):
        schema = {
            "extract_time": {
                "type": "datetime"
            },
            "date": {
                "type": "datetime"
            },
            "total": {
                "type": "integer"
            },
        }

        with self.app.app_context():
            # Success
            res = serialize(
                {
                    "extract_time": "Tue, 06 Nov 2012 10:33:31 GMT",
                    "date": "Tue, 06 Nov 2012 10:33:31 GMT",
                    "total": "r123",
                },
                schema=schema,
            )
            # this has been left untouched as it could not be serialized.
            self.assertEqual(res["total"], "r123")
            # these have been both serialized.
            self.assertTrue(isinstance(res["extract_time"], datetime))
            self.assertTrue(isinstance(res["date"], datetime))
예제 #17
0
파일: common.py 프로젝트: nicolaiarocci/eve
 def test_serialize_inside_list_of_schema_of_x_of_rules(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = {
             'list-field': {
                 'type': 'list',
                 'schema': {
                     x_of: [
                         {
                             'type': 'dict',
                             'schema': {
                                 'x_of-field': {
                                     'type': 'objectid',
                                     'required': True
                                 }
                             }
                         }
                     ]
                 }
             }
         }
         doc = {'list-field': [{'x_of-field': '50656e4538345b39dd0414f0'}]}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             serialized_oid = serialized['list-field'][0]['x_of-field']
             self.assertTrue(isinstance(serialized_oid, ObjectId))
예제 #18
0
 def test_serialize_subdocument(self):
     # tests fix for #244, serialization of sub-documents.
     schema = {
         "personal": {
             "type": "dict",
             "schema": {
                 "best_friend": {
                     "type": "objectid"
                 },
                 "born": {
                     "type": "datetime"
                 },
             },
         },
         "without_type": {},
     }
     doc = {
         "personal": {
             "best_friend": "50656e4538345b39dd0414f0",
             "born": "Tue, 06 Nov 2012 10:33:31 GMT",
         },
         "without_type": "foo",
     }
     with self.app.app_context():
         serialized = serialize(doc, schema=schema)
     self.assertTrue(
         isinstance(serialized["personal"]["best_friend"], ObjectId))
     self.assertTrue(isinstance(serialized["personal"]["born"], datetime))
예제 #19
0
 def test_serialize_inside_nested_x_of_rules(self):
     schema = {
         "nested-x_of-field": {
             "oneof": [
                 {
                     "anyof": [{
                         "type": "objectid"
                     }, {
                         "type": "datetime"
                     }],
                     "required": True,
                 },
                 {
                     "allof": [{
                         "type": "boolean"
                     }, {
                         "required": True
                     }]
                 },
             ]
         }
     }
     doc = {"nested-x_of-field": "50656e4538345b39dd0414f0"}
     with self.app.app_context():
         serialized = serialize(doc, schema=schema)
         self.assertTrue(
             isinstance(serialized["nested-x_of-field"], ObjectId))
예제 #20
0
파일: common.py 프로젝트: jazzlly/eve
 def test_serialize_inside_list_of_schema_of_x_of_rules(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = {
             'list-field': {
                 'type': 'list',
                 'schema': {
                     x_of: [
                         {
                             'type': 'dict',
                             'schema': {
                                 'x_of-field': {
                                     'type': 'objectid',
                                     'required': True
                                 }
                             }
                         }
                     ]
                 }
             }
         }
         doc = {'list-field': [{'x_of-field': '50656e4538345b39dd0414f0'}]}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             serialized_oid = serialized['list-field'][0]['x_of-field']
             self.assertTrue(isinstance(serialized_oid, ObjectId))
예제 #21
0
파일: common.py 프로젝트: nicolaiarocci/eve
    def test_serialize_boolean(self):
        schema = {'bool': {'type': 'boolean'}}

        with self.app.app_context():
            for val in [1, '1', 0, '0', 'true', 'True', 'false', 'False']:
                doc = {'bool': val}
                serialized = serialize(doc, schema=schema)
                self.assertTrue(isinstance(serialized['bool'], bool))
예제 #22
0
파일: common.py 프로젝트: rtalukder/eve
    def test_serialize_boolean(self):
        schema = {'bool': {'type': 'boolean'}}

        with self.app.app_context():
            for val in [1, '1', 0, '0', 'true', 'True', 'false', 'False']:
                doc = {'bool': val}
                serialized = serialize(doc, schema=schema)
                self.assertTrue(isinstance(serialized['bool'], bool))
예제 #23
0
파일: common.py 프로젝트: ride90/eve
    def test_serialize_boolean(self):
        schema = {"bool": {"type": "boolean"}}

        with self.app.app_context():
            for val in [1, "1", 0, "0", "true", "True", "false", "False"]:
                doc = {"bool": val}
                serialized = serialize(doc, schema=schema)
                self.assertTrue(isinstance(serialized["bool"], bool))
예제 #24
0
파일: common.py 프로젝트: ride90/eve
 def test_serialize_null_dictionary(self):
     # Serialization should continue after encountering a null value dict
     # field. Field may be nullable, or error will be caught in validation.
     schema = {
         "nullable_dict": {
             "type": "dict",
             "nullable": True,
             "schema": {"simple_field": {"type": "number"}},
         }
     }
     doc = {"nullable_dict": None}
     with self.app.app_context():
         try:
             serialize(doc, schema=schema)
         except Exception:
             self.assertTrue(
                 False,
                 "Serializing null dictionaries should " "not raise an exception.",
             )
예제 #25
0
파일: common.py 프로젝트: AndrewPashkin/eve
 def test_serialize_null_list(self):
     schema = {
         'nullable_list': {
             'type': 'list',
             'nullable': True,
             'schema': {
                 'type': 'objectid'
             }
         }
     }
     doc = {
         'nullable_list': None
     }
     with self.app.app_context():
         try:
             serialize(doc, schema=schema)
         except Exception:
             self.fail('Serializing null lists'
                       ' should not raise an exception')
예제 #26
0
파일: common.py 프로젝트: nicolaiarocci/eve
 def test_serialize_inside_x_of_typesavers(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = {
             'x_of-field': {
                 '{0}_type'.format(x_of): ['objectid', 'float', 'boolean']
             }
         }
         doc = {'x_of-field': '50656e4538345b39dd0414f0'}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized['x_of-field'], ObjectId))
예제 #27
0
 def test_serialize_inside_x_of_typesavers(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = {
             'x_of-field': {
                 '{0}_type'.format(x_of): ['objectid', 'float', 'boolean']
             }
         }
         doc = {'x_of-field': '50656e4538345b39dd0414f0'}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized['x_of-field'], ObjectId))
예제 #28
0
파일: common.py 프로젝트: ride90/eve
 def test_serialize_inside_x_of_typesavers(self):
     for x_of in ["allof", "anyof", "oneof", "noneof"]:
         schema = {
             "x_of-field": {
                 "{0}_type".format(x_of): ["objectid", "float", "boolean"]
             }
         }
         doc = {"x_of-field": "50656e4538345b39dd0414f0"}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized["x_of-field"], ObjectId))
예제 #29
0
 def test_serialize_number(self):
     schema = {
         'anumber': {
             'type': 'number',
         }
     }
     for expected_type, value in [(int, '35'), (float, '3.5')]:
         doc = {'anumber': value}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(
                 isinstance(serialized['anumber'], expected_type))
예제 #30
0
파일: common.py 프로젝트: FerminYang/eve
 def test_serialize_subdocument(self):
     # tests fix for #244, serialization of sub-documents.
     schema = {'personal': {'type': 'dict',
                            'schema': {'best_friend': {'type': 'objectid'},
                                       'born': {'type': 'datetime'}}}}
     doc = {'personal': {'best_friend': '50656e4538345b39dd0414f0',
                         'born': 'Tue, 06 Nov 2012 10:33:31 GMT'}}
     with self.app.app_context():
         serialized = serialize(doc, schema=schema)
     self.assertTrue(
         isinstance(serialized['personal']['best_friend'], ObjectId))
     self.assertTrue(
         isinstance(serialized['personal']['born'], datetime))
예제 #31
0
파일: common.py 프로젝트: ride90/eve
 def test_serialize_inside_list_of_x_of_rules(self):
     for x_of in ["allof", "anyof", "oneof", "noneof"]:
         schema = {
             "list-field": {
                 "type": "list",
                 "schema": {x_of: [{"type": "objectid", "required": True}]},
             }
         }
         doc = {"list-field": ["50656e4538345b39dd0414f0"]}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             serialized_oid = serialized["list-field"][0]
             self.assertTrue(isinstance(serialized_oid, ObjectId))
예제 #32
0
 def test_serialize_null_dictionary(self):
     # Serialization should continue after encountering a null value dict
     # field. Field may be nullable, or error will be caught in validation.
     schema = {
         'nullable_dict': {
             'type': 'dict',
             'nullable': True,
             'schema': {
                 'simple_field': {
                     'type': 'number'
                 }
             }
         }
     }
     doc = {'nullable_dict': None}
     with self.app.app_context():
         try:
             serialize(doc, schema=schema)
         except Exception:
             self.assertTrue(
                 False, "Serializing null dictionaries should "
                 "not raise an exception.")
예제 #33
0
파일: common.py 프로젝트: jazzlly/eve
 def test_serialize_inside_x_of_rules(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = {
             'x_of-field': {
                 x_of: [
                     {'type': 'objectid'},
                     {'required': True}
                 ]
             }
         }
         doc = {'x_of-field': '50656e4538345b39dd0414f0'}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized['x_of-field'], ObjectId))
예제 #34
0
파일: common.py 프로젝트: nicolaiarocci/eve
 def test_serialize_inside_x_of_rules(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = {
             'x_of-field': {
                 x_of: [
                     {'type': 'objectid'},
                     {'required': True}
                 ]
             }
         }
         doc = {'x_of-field': '50656e4538345b39dd0414f0'}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized['x_of-field'], ObjectId))
예제 #35
0
파일: common.py 프로젝트: kidaa/eve
 def test_serialize_null_dictionary(self):
     # Serialization should continue after encountering a null value dict
     # field. Field may be nullable, or error will be caught in validation.
     schema = {
         'nullable_dict': {
             'type': 'dict',
             'nullable': True,
             'schema': {
                 'simple_field': {
                     'type': 'number'
                 }
             }
         }
     }
     doc = {
         'nullable_dict': None
     }
     with self.app.app_context():
         try:
             serialize(doc, schema=schema)
         except Exception:
             self.assertTrue(False, "Serializing null dictionaries should "
                                    "not raise an exception.")
예제 #36
0
파일: common.py 프로젝트: jazzlly/eve
    def test_serialize_array_of_tipes(self):
        # see #1112.
        schema = {
            'val': {
                'type': 'dict',
                'schema': {
                    'x': {'type': ['string', 'number']},
                    'timestamp': {'type': 'datetime'}
                }
            }
        }

        doc = {'val': {'x': '1', 'timestamp': 'Tue, 06 Nov 2012 10:33:31 GMT'}}
        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        self.assertEqual(serialized['val']['x'], 1)
        self.assertTrue(isinstance(serialized['val']['timestamp'], datetime))

        doc = {'val': {'x': 's', 'timestamp': 'Tue, 06 Nov 2012 10:33:31 GMT'}}
        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        self.assertEqual(serialized['val']['x'], 's')
        self.assertTrue(isinstance(serialized['val']['timestamp'], datetime))
예제 #37
0
 def test_serialize_subdocument(self):
     # tests fix for #244, serialization of sub-documents.
     schema = {
         "personal": {"type": "dict", "schema": {"best_friend": {"type": "objectid"}, "born": {"type": "datetime"}}},
         "without_type": {},
     }
     doc = {
         "personal": {"best_friend": "50656e4538345b39dd0414f0", "born": "Tue, 06 Nov 2012 10:33:31 GMT"},
         "without_type": "foo",
     }
     with self.app.app_context():
         serialized = serialize(doc, schema=schema)
     self.assertTrue(isinstance(serialized["personal"]["best_friend"], ObjectId))
     self.assertTrue(isinstance(serialized["personal"]["born"], datetime))
 def test_serialize_list_alongside_x_of_rules(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = {
             'x_of-field': {
                 "type": "list",
                 x_of: [
                     {"schema": {'type': 'objectid'}},
                     {"schema": {'type': 'datetime'}}
                 ]
             }
         }
         doc = {'x_of-field': ['50656e4538345b39dd0414f0']}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized['x_of-field'][0], ObjectId))
예제 #39
0
파일: common.py 프로젝트: nicolaiarocci/eve
 def test_serialize_number(self):
     schema = {
         'anumber': {
             'type': 'number',
         }
     }
     for expected_type, value in [(int, '35'), (float, '3.5')]:
         doc = {
             'anumber': value
         }
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(
                 isinstance(serialized['anumber'], expected_type)
             )
 def test_serialize_alongside_x_of_rules(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = OrderedDict([
             ('x_of-field', {
                 x_of: [
                     {'type': 'objectid'},
                     {'required': True}
                 ]
             }),
             ('oid-field', {'type': 'objectid'})
         ])
         doc = OrderedDict([('x_of-field', '50656e4538345b39dd0414f0'), ('oid-field', '50656e4538345b39dd0414f0')])
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized['x_of-field'], ObjectId))
             self.assertTrue(isinstance(serialized['oid-field'], ObjectId))
예제 #41
0
파일: common.py 프로젝트: nicolaiarocci/eve
 def test_serialize_list_alongside_x_of_rules(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = {
             'x_of-field': {
                 "type": "list",
                 x_of: [
                     {"schema": {'type': 'objectid'}},
                     {"schema": {'type': 'datetime'}}
                 ]
             }
         }
         doc = {'x_of-field': ['50656e4538345b39dd0414f0']}
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized['x_of-field'][0],
                                        ObjectId))
예제 #42
0
파일: common.py 프로젝트: nicolaiarocci/eve
    def test_mongo_serializes(self):
        schema = {
            'id': {'type': 'objectid'},
            'date': {'type': 'datetime'},
            'count': {'type': 'integer'},
            'average': {'type': 'float'},
            'dict_valueschema': {
                'valueschema': {'type': 'objectid'}
            },
            'refobj': {'type': 'dbref'},
            'decobjstring': {'type': 'decimal'},
            'decobjnumber': {'type': 'decimal'}
        }
        with self.app.app_context():
            # Success
            res = serialize(
                {
                    'id': '50656e4538345b39dd0414f0',
                    'date': 'Tue, 06 Nov 2012 10:33:31 GMT',
                    'count': 42,
                    'average': 42.42,
                    'dict_valueschema': {
                        'foo1': '50656e4538345b39dd0414f0',
                        'foo2': '50656e4538345b39dd0414f0',
                    },
                    'refobj': {
                        '$id': '50656e4538345b39dd0414f0',
                        '$col': 'SomeCollection'
                    },
                    'decobjstring': "200.0",
                    'decobjnumber': 200.0
                },
                schema=schema
            )
            self.assertTrue(isinstance(res['id'], ObjectId))
            self.assertTrue(isinstance(res['date'], datetime))
            self.assertTrue(isinstance(res['count'], int))
            self.assertTrue(isinstance(res['average'], float))

            ks = res['dict_valueschema']
            self.assertTrue(isinstance(ks['foo1'], ObjectId))
            self.assertTrue(isinstance(ks['foo2'], ObjectId))
            self.assertTrue(isinstance(res['refobj'], DBRef))
            self.assertTrue(isinstance(res['decobjstring'],
                                       decimal128.Decimal128))
            self.assertTrue(isinstance(res['decobjnumber'],
                                       decimal128.Decimal128))
예제 #43
0
파일: common.py 프로젝트: jazzlly/eve
    def test_mongo_serializes(self):
        schema = {
            'id': {'type': 'objectid'},
            'date': {'type': 'datetime'},
            'count': {'type': 'integer'},
            'average': {'type': 'float'},
            'dict_valueschema': {
                'valueschema': {'type': 'objectid'}
            },
            'refobj': {'type': 'dbref'},
            'decobjstring': {'type': 'decimal'},
            'decobjnumber': {'type': 'decimal'}
        }
        with self.app.app_context():
            # Success
            res = serialize(
                {
                    'id': '50656e4538345b39dd0414f0',
                    'date': 'Tue, 06 Nov 2012 10:33:31 GMT',
                    'count': 42,
                    'average': 42.42,
                    'dict_valueschema': {
                        'foo1': '50656e4538345b39dd0414f0',
                        'foo2': '50656e4538345b39dd0414f0',
                    },
                    'refobj': {
                        '$id': '50656e4538345b39dd0414f0',
                        '$col': 'SomeCollection'
                    },
                    'decobjstring': "200.0",
                    'decobjnumber': 200.0
                },
                schema=schema
            )
            self.assertTrue(isinstance(res['id'], ObjectId))
            self.assertTrue(isinstance(res['date'], datetime))
            self.assertTrue(isinstance(res['count'], int))
            self.assertTrue(isinstance(res['average'], float))

            ks = res['dict_valueschema']
            self.assertTrue(isinstance(ks['foo1'], ObjectId))
            self.assertTrue(isinstance(ks['foo2'], ObjectId))
            self.assertTrue(isinstance(res['refobj'], DBRef))
            self.assertTrue(isinstance(res['decobjstring'],
                                       decimal128.Decimal128))
            self.assertTrue(isinstance(res['decobjnumber'],
                                       decimal128.Decimal128))
예제 #44
0
파일: utils.py 프로젝트: stt/eve-ntifier
    def _find(self, resource, req, **lookup):
        spec = {}
        sort = []
        """
        if req:
          if req.where:
            spec = json.loads(req.where)
          if req.sort:
              for sort_arg in [s.strip() for s in req.sort.split(",")]:
                  sn = sort_arg[1:] if sort_arg[0] == "-" else sort_arg
                  try:
                      if sort_arg[0] == "-":
                          sort.append(getattr(model, sn).desc())
                      else:
                          sort.append(getattr(model, sn))
                  except AttributeError:
                      abort(400, description='Unknown field name: %s' % sn)

        """
        if 'lookup' in lookup and lookup['lookup']:
            spec = self.combine_queries(
                spec, lookup['lookup'])
            spec = lookup['lookup']

        client_projection = self._client_projection(req)

        datasource, spec, projection, sort = self._datasource_ex(
            resource,
            spec,
            client_projection,
            sort)

        if len(spec) == 0:
            return self.repo[resource].values()

        # id from query is str, in repo it's int
        spec = serialize(spec, resource)
        spec = set(spec.items())

        rs = []

        for id,r in self.repo[resource].items():
            ok = len(set(r.items()) & spec) == len(spec)
            if ok: rs.append(r.copy())

        return rs
예제 #45
0
파일: common.py 프로젝트: nicolaiarocci/eve
 def test_serialize_alongside_x_of_rules(self):
     for x_of in ['allof', 'anyof', 'oneof', 'noneof']:
         schema = OrderedDict([
             ('x_of-field', {
                 x_of: [
                     {'type': 'objectid'},
                     {'required': True}
                 ]
             }),
             ('oid-field', {'type': 'objectid'})
         ])
         doc = OrderedDict([('x_of-field', '50656e4538345b39dd0414f0'),
                            ('oid-field', '50656e4538345b39dd0414f0')])
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized['x_of-field'], ObjectId))
             self.assertTrue(isinstance(serialized['oid-field'], ObjectId))
예제 #46
0
파일: common.py 프로젝트: ride90/eve
 def test_serialize_alongside_x_of_rules(self):
     for x_of in ["allof", "anyof", "oneof", "noneof"]:
         schema = OrderedDict(
             [
                 ("x_of-field", {x_of: [{"type": "objectid"}, {"required": True}]}),
                 ("oid-field", {"type": "objectid"}),
             ]
         )
         doc = OrderedDict(
             [
                 ("x_of-field", "50656e4538345b39dd0414f0"),
                 ("oid-field", "50656e4538345b39dd0414f0"),
             ]
         )
         with self.app.app_context():
             serialized = serialize(doc, schema=schema)
             self.assertTrue(isinstance(serialized["x_of-field"], ObjectId))
             self.assertTrue(isinstance(serialized["oid-field"], ObjectId))
예제 #47
0
파일: common.py 프로젝트: kidaa/eve
    def test_non_blocking_on_simple_field_serialization_exception(self):
        schema = {
            'extract_time': {'type': 'datetime'},
            'date': {'type': 'datetime'},
            'total': {'type': 'integer'}
        }

        with self.app.app_context():
            # Success
            res = serialize(
                {
                    'extract_time': 'Tue, 06 Nov 2012 10:33:31 GMT',
                    'date': 'Tue, 06 Nov 2012 10:33:31 GMT',
                    'total': 'r123'
                },
                schema=schema
            )
            # this has been left untouched as it could not be serialized.
            self.assertEqual(res['total'], 'r123')
            # these have been both serialized.
            self.assertTrue(isinstance(res['extract_time'], datetime))
            self.assertTrue(isinstance(res['date'], datetime))
예제 #48
0
파일: common.py 프로젝트: pfreixes/eve
    def test_mongo_serializes(self):
        schema = {
            'id': {'type': 'objectid'},
            'date': {'type': 'datetime'},
            'count': {'type': 'integer'},
            'average': {'type': 'float'},
        }
        with self.app.app_context():
            # Success
            res = serialize(
                {
                    'id': '50656e4538345b39dd0414f0',
                    'date': 'Tue, 06 Nov 2012 10:33:31 GMT',
                    'count': '42',
                    'average': '42.42',
                },
                schema=schema
            )
            self.assertTrue(isinstance(res['id'], ObjectId))
            self.assertTrue(isinstance(res['date'], datetime))
            self.assertTrue(isinstance(res['count'], int))
            self.assertTrue(isinstance(res['average'], float))

            # Fails
            self.assertRaises(InvalidId, serialize, **dict(
                document={'id': 'test'}, schema=schema
            ))
            self.assertRaises(ValueError, serialize, **dict(
                document={'date': 'test'}, schema=schema
            ))
            self.assertRaises(ValueError, serialize, **dict(
                document={'count': '10.0'}, schema=schema
            ))
            self.assertRaises(ValueError, serialize, **dict(
                document={'average': 'test'}, schema=schema
            ))
예제 #49
0
파일: common.py 프로젝트: nicolaiarocci/eve
    def test_dbref_serialize_lists_of_lists(self):
        # serialize should handle list of lists of basic types
        schema = {
            'l_of_l': {
                'type': 'list',
                'schema': {
                    'type': 'list',
                    'schema': {
                        'type': 'dbref'
                    }
                }
            }
        }
        doc = {
            'l_of_l': [
                [{'$col': 'SomeCollection', '$id': '50656e4538345b39dd0414f0'},
                 {'$col': 'SomeCollection', '$id': '50656e4538345b39dd0414f0'}
                 ],
                [{'$col': 'SomeCollection', '$id': '50656e4538345b39dd0414f0'},
                 {'$col': 'SomeCollection', '$id': '50656e4538345b39dd0414f0'}
                 ]
            ]
        }

        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        for sublist in serialized['l_of_l']:
            for item in sublist:
                self.assertTrue(isinstance(item, DBRef))

        # serialize should handle list of lists of dicts
        schema = {
            'l_of_l': {
                'type': 'list',
                'schema': {
                    'type': 'list',
                    'schema': {
                        'type': 'dict',
                        'schema': {
                            '_id': {
                                'type': 'dbref'
                            }
                        }
                    }
                }
            }
        }
        doc = {
            'l_of_l': [
                [
                    {'_id': {'$col': 'SomeCollection',
                             '$id': '50656e4538345b39dd0414f0'}
                     },
                    {'_id': {'$col': 'SomeCollection',
                             '$id': '50656e4538345b39dd0414f0'}
                     }
                ],
                [
                    {'_id': {'$col': 'SomeCollection',
                             '$id': '50656e4538345b39dd0414f0'}
                     },
                    {'_id': {'$col': 'SomeCollection',
                             '$id': '50656e4538345b39dd0414f0'}
                     }
                ],
            ]
        }
        with self.app.app_context():
            serialized = serialize(doc, schema=schema)
        for sublist in serialized['l_of_l']:
            for item in sublist:
                self.assertTrue(isinstance(item['_id'], DBRef))