def test_update_mixin_returns_errors_if_data_is_not_valid(self):
        self.db.test.insert_one({'_id': 1, 'test': 1})

        response = self.update_client.put(
            '/1/', data=serialize({'test': '1'})
        )

        self.assertEqual(response.status_code, 400)
        errors = response.json
        errors['document'].pop('updated_at')

        self.assertEqual(
            errors,
            {
                'error_code': 21,
                'error_type': 'DocumentValidationError',
                'error_message': 'Validation of document from collection \'Test\' failed.',
                'errors': [
                    {
                        'error_code': 25,
                        'error_type': 'FieldTypeError',
                        'error_message': 'Field \'test\' on collection \'Test\' must be of type integer.',
                        'collection': 'Test',
                        'field': 'test',
                        'type': 'integer',
                    },
                ],
                'collection': 'Test',
                'schema': {'test': {'required': True, 'type': 'integer'}},
                'document': {'_id': 1, 'test': '1'},
            }
        )
    def test_create_mixin_returns_errors_if_invalid_data(self):
        response = self.create_client.post('/', data=serialize({}))

        self.assertEqual(response.status_code, 400)

        errors = deserialize(response.get_data(as_text=True))
        errors['document'].pop('created_at')
        errors['document'].pop('updated_at')

        self.assertEqual(
            errors,
            {
                'error_code': 21,
                'error_type': 'DocumentValidationError',
                'error_message': 'Validation of document from collection \'Test\' failed.',
                'errors': [
                    {
                        'error_code': 23,
                        'error_type': 'RequiredFieldError',
                        'error_message': 'Field \'test\' on collection \'Test\' is required.',
                        'collection': 'Test',
                        'field': 'test',
                    },
                ],
                'collection': 'Test',
                'schema': {'test': {'required': True, 'type': 'integer'}},
                'document': {},
            }
        )
    def test_create_mixin_returns_errors_if_invalid_data(self):
        response = self.create_client.post('/', data=serialize({}))

        self.assertEqual(response.status_code, 400)

        errors = response.json
        errors['document'].pop('created_at')
        errors['document'].pop('updated_at')

        self.assertEqual(
            errors,
            {
                'error_code': 21,
                'error_type': 'DocumentValidationError',
                'error_message': 'Validation of document from collection \'Test\' failed.',
                'errors': [
                    {
                        'error_code': 23,
                        'error_type': 'RequiredFieldError',
                        'error_message': 'Field \'test\' on collection \'Test\' is required.',
                        'collection': 'Test',
                        'field': 'test',
                    },
                ],
                'collection': 'Test',
                'schema': {'test': {'required': True, 'type': 'integer'}},
                'document': {},
            }
        )
Beispiel #4
0
    def test_update_mixin_returns_updated_document_if_data_is_valid(self):
        self.db.test.insert_one({'_id': 1, 'test': 1})

        response = self.update_client.put('/1/', data=serialize({'test': 2}))
        data = response.json
        data.pop('updated_at')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data, {'_id': 1, 'test': 2})
        self.assertEqual(self.db.test.find_one({'_id': 1})['test'], 2)
    def test_update_mixin_returns_updated_document_if_data_is_valid(self):
        self.db.test.insert_one({'_id': 1, 'test': 1})

        response = self.update_client.put(
            '/1/', data=serialize({'test': 2})
        )
        data = response.json
        data.pop('updated_at')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(data, {'_id': 1, 'test': 2})
        self.assertEqual(self.db.test.find_one({'_id': 1})['test'], 2)
    def test_create_mixin_returns_201_and_created_documents_id(self):
        response = self.create_client.post(
            '/', data=serialize({'test': 1, '_id': 1})
        )

        data = response.json
        data.pop('created_at')
        data.pop('updated_at')

        self.assertEqual(response.status_code, 201)
        self.assertEqual(data, {'test': 1, '_id': 1})
        self.assertEqual(self.db.test.find_one({'_id': 1})['test'], 1)
    def test_create_mixin_returns_201_and_created_documents_id(self):
        response = self.create_client.post(
            '/', data=serialize({'test': 1, '_id': 1})
        )

        data = deserialize(response.get_data(as_text=True))
        data.pop('created_at')
        data.pop('updated_at')

        self.assertEqual(response.status_code, 201)
        self.assertEqual(data, {'test': 1, '_id': 1})
        self.assertEqual(self.db.test.find_one({'_id': 1})['test'], 1)
Beispiel #8
0
    def test_update_mixin_returns_not_found_if_no_document_matches_id(self):
        response = self.update_client.put('/1/', data=serialize({}))

        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.json, {
                'error_code': 12,
                'error_type': 'DocumentNotFoundError',
                'error_message':
                '1 is not a valid _id for a document from collection \'Test\'.',
                'collection': 'Test',
                '_id': 1,
            })
    def test_update_mixin_returns_not_found_if_no_document_matches_id(self):
        response = self.update_client.put('/1/', data=serialize({}))

        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.json,
            {
                'error_code': 12,
                'error_type': 'DocumentNotFoundError',
                'error_message': '1 is not a valid _id for a document from collection \'Test\'.',
                'collection': 'Test',
                '_id': 1,
            }
        )
Beispiel #10
0
    def test_update_mixin_returns_errors_if_data_is_not_valid(self):
        self.db.test.insert_one({'_id': 1, 'test': 1})

        response = self.update_client.put('/1/', data=serialize({'test': '1'}))

        self.assertEqual(response.status_code, 400)
        errors = response.json
        errors['document'].pop('updated_at')

        self.assertEqual(
            errors, {
                'error_code':
                21,
                'error_type':
                'DocumentValidationError',
                'error_message':
                'Validation of document from collection \'Test\' failed.',
                'errors': [
                    {
                        'error_code': 25,
                        'error_type': 'FieldTypeError',
                        'error_message':
                        'Field \'test\' on collection \'Test\' must be of type integer.',
                        'collection': 'Test',
                        'field': 'test',
                        'type': 'integer',
                    },
                ],
                'collection':
                'Test',
                'schema': {
                    'test': {
                        'required': True,
                        'type': 'integer'
                    }
                },
                'document': {
                    '_id': 1,
                    'test': '1'
                },
            })
Beispiel #11
0
 def test_serializing_a_dict_returns_the_same_dict(self):
     self.assertEqual(serialize({'test': 'test'}), '{"test": "test"}')
Beispiel #12
0
 def test_serializing_a_float_returns_the_same_float(self):
     self.assertEqual(serialize(3.14)[:4], '3.14')
Beispiel #13
0
 def test_serializing_an_in_returns_the_same_int(self):
     self.assertEqual(serialize(1), '1')
Beispiel #14
0
 def test_serializing_a_string_returns_the_same_string(self):
     self.assertEqual(serialize('test'), '"test"')
Beispiel #15
0
 def test_serializing_a_tuple_returns_the_same_tuple_as_a_list(self):
     self.assertEqual(serialize(tuple((1, 2, 3))), '[1, 2, 3]')
Beispiel #16
0
    def test_serializing_a_datetime_returns_a_dict_with_date_as_key_and_the_millis_as_value(
            self):
        date = datetime(2015, 1, 1, 1, 1, 1)

        self.assertEqual(serialize(date), '{"$date": 1420074061000}')
Beispiel #17
0
    def test_serializing_an_objectid_returns_a_dict_with_oid_as_key_and_the_str_as_value(
            self):
        oid = ObjectId('0123456789ab0123456789ab')

        self.assertEqual(serialize(oid),
                         '{"$oid": "0123456789ab0123456789ab"}')
Beispiel #18
0
 def test_serializing_a_float_returns_the_same_float(self):
     self.assertEqual(serialize(3.14)[:4], '3.14')
Beispiel #19
0
 def test_serializing_a_dict_returns_the_same_dict(self):
     self.assertEqual(serialize({'test': 'test'}), '{"test": "test"}')
Beispiel #20
0
    def test_serializing_an_uuid_returns_a_dict_with_uuid_as_key_and_the_hex_as_value(self):
        uuid = UUID('149ead99640043a2874ad83e00c559c2')

        self.assertEqual(
            serialize(uuid), '{"$uuid": "149ead99640043a2874ad83e00c559c2"}'
        )
Beispiel #21
0
    def test_serializing_a_datetime_returns_a_dict_with_date_as_key_and_the_millis_as_value(self):
        date = datetime(2015, 1, 1, 1, 1, 1)

        self.assertEqual(serialize(date), '{"$date": 1420074061000}')
Beispiel #22
0
    def test_serializing_a_dbref_returns_a_dict_with_oid_as_key_and_the_str_as_value(self):
        dbref = DBRef('ref', 'id')

        self.assertEqual(serialize(dbref), '{"$ref": "ref", "$id": "id"}')
Beispiel #23
0
    def test_serializing_an_objectid_returns_a_dict_with_oid_as_key_and_the_str_as_value(self):
        oid = ObjectId('0123456789ab0123456789ab')

        self.assertEqual(serialize(oid), '{"$oid": "0123456789ab0123456789ab"}')
Beispiel #24
0
 def test_serializing_a_list_returns_the_same_list(self):
     self.assertEqual(serialize([1, 2, 3]), '[1, 2, 3]')
Beispiel #25
0
 def test_serializing_a_tuple_returns_the_same_tuple_as_a_list(self):
     self.assertEqual(serialize(tuple((1, 2, 3))), '[1, 2, 3]')
Beispiel #26
0
 def test_serializing_an_in_returns_the_same_int(self):
     self.assertEqual(serialize(1), '1')
Beispiel #27
0
    def test_serializing_a_dbref_returns_a_dict_with_oid_as_key_and_the_str_as_value(
            self):
        dbref = DBRef('ref', 'id')

        self.assertEqual(serialize(dbref), '{"$ref": "ref", "$id": "id"}')
Beispiel #28
0
 def test_serializing_a_string_returns_the_same_string(self):
     self.assertEqual(serialize('test'), '"test"')
Beispiel #29
0
    def test_serializing_an_uuid_returns_a_dict_with_uuid_as_key_and_the_hex_as_value(
            self):
        uuid = UUID('149ead99640043a2874ad83e00c559c2')

        self.assertEqual(serialize(uuid),
                         '{"$uuid": "149ead99640043a2874ad83e00c559c2"}')
Beispiel #30
0
 def test_serializing_a_list_returns_the_same_list(self):
     self.assertEqual(serialize([1, 2, 3]), '[1, 2, 3]')