Example #1
0
File: common.py Project: 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")
Example #2
0
    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))
Example #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')
    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))
Example #5
0
File: common.py Project: 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))
Example #6
0
File: common.py Project: 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))
Example #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))
    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))
Example #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))
Example #10
0
File: common.py Project: 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))
Example #11
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))
 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))
Example #13
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))
    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))
Example #15
0
File: common.py Project: 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))
    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))
Example #17
0
 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))
 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_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))
Example #20
0
 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))
Example #21
0
    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))
Example #22
0
    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))
Example #23
0
File: common.py Project: 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))
Example #24
0
File: common.py Project: 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.",
             )
Example #25
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')
Example #26
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))
Example #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))
Example #28
0
File: common.py Project: 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))
Example #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))
Example #30
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'}}}}
     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))
Example #31
0
File: common.py Project: 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))
Example #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.")
Example #33
0
 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))
Example #34
0
 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))
Example #35
0
File: common.py Project: 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.")
Example #36
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))
 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))
Example #39
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)
             )
 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))
Example #41
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))
Example #42
0
    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))
Example #43
0
    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))
Example #44
0
    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
Example #45
0
 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))
Example #46
0
File: common.py Project: 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))
Example #47
0
File: common.py Project: 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))
Example #48
0
    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
            ))
Example #49
0
    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))