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_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 = deserialize(response.get_data(as_text=True))
        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'},
            }
        )
예제 #3
0
    def test_deserializing_a_serialized_datetime_returns_the_original_datetime_with_tz(self):
        date = datetime(2015, 1, 1, 1, 1, 1)

        deserialized_date = deserialize('{"$date": 1420074061000}')
        self.assertEqual(
            deserialized_date.utctimetuple(), date.utctimetuple()
        )
    def test_list_mixin_returns_empty_list_if_no_documents_in_collection(self):
        response = self.documents_client.get('/')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            deserialize(response.get_data(as_text=True)), []
        )
    def test_retrieve_mixin_returns_document_containing_given_id(self):
        self.db.collection.insert_one({'_id': 1})

        response = self.retrieve_client.get('/1/')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            deserialize(response.get_data(as_text=True)),
            {'_id': 1}
        )
    def test_list_mixin_returns_list_of_collection_documents_that_pass_filter_with_only_specified_fields(self):
        self.db.collection.insert_one({'_id': 1, 'test': 'test1', 'number': 1})
        self.db.collection.insert_one({'_id': 2, 'test': 'test2', 'number': 1})

        response = self.documents_client.get('/?match={"number": 1, "test": "test1"}&project={"test": 1, "_id": 1}')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            deserialize(response.get_data(as_text=True)),
            [{'_id': 1, 'test': 'test1'}]
        )
    def test_list_mixin_returns_list_of_all_collection_documents_if_no_filter(self):
        self.db.collection.insert_one({'_id': 1, 'test': 'test1'})
        self.db.collection.insert_one({'_id': 2, 'test': 'test2'})

        response = self.documents_client.get('/')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            deserialize(response.get_data(as_text=True)),
            [{'_id': 1, 'test': 'test1'}, {'_id': 2, 'test': 'test2'}]
        )
    def test_delete_deletes_and_returns_deleted_document(self):
        self.db.collection.insert_one({'_id': 1})

        self.assertIsNotNone(self.db.collection.find_one({'_id': 1}))

        response = self.delete_client.delete('/1/')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            deserialize(response.get_data(as_text=True)), {'_id': 1}
        )
        self.assertIsNone(self.db.collection.find_one({'_id': 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)
    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 = deserialize(response.get_data(as_text=True))
        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_retrieve_mixin_returns_not_found_if_no_document_matches_id(self):
        response = self.retrieve_client.get('/1/')

        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            deserialize(response.get_data(as_text=True)),
            {
                'error_code': 12,
                'error_type': 'DocumentNotFoundError',
                'error_message': '1 is not a valid _id for a document from collection \'Collection\'.',
                'collection': 'Collection',
                '_id': 1,
            }
        )
예제 #12
0
    def test_list_mixin_returns_list_of_collection_documents_sorted_by_given_sort_keys(self):
        self.db.collection.insert_one({'_id': 1, 'test': 1, 'number': 1})
        self.db.collection.insert_one({'_id': 2, 'test': 1, 'number': 2})
        self.db.collection.insert_one({'_id': 3, 'test': 2, 'number': 2})

        response = self.documents_client.get('/?sort={"number": -1, "test": 1}&project={"number": 1, "test": 1, "_id": 0}')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            deserialize(response.get_data(as_text=True)),
            [
                {'number': 2, 'test': 1}, {'number': 2, 'test': 2},
                {'number': 1, 'test': 1}
            ]
        )
예제 #13
0
 def test_deserialize_serializes_and_deserializes_back_if_to_deserialize_is_already_a_json(self):
     self.assertEqual(deserialize({'123': 123}), {'123': 123})
예제 #14
0
    def test_deserializing_a_serialized_uuid_returns_the_original_uuid(self):
        uuid = UUID('149ead99640043a2874ad83e00c559c2')

        self.assertEqual(
            deserialize('{"$uuid": "149ead99640043a2874ad83e00c559c2"}'), uuid
        )
예제 #15
0
    def test_deserializing_a_serialized_dbref_returns_the_original_dbref(self):
        dbref = DBRef('ref', 'id')

        self.assertEqual(deserialize('{"$ref": "ref", "$id": "id"}'), dbref)
예제 #16
0
    def test_deserializing_a_serialized_object_id_returns_the_original_object_id(self):
        oid = ObjectId('0123456789ab0123456789ab')

        self.assertEqual(
            deserialize('{"$oid": "0123456789ab0123456789ab"}'), oid
        )
예제 #17
0
 def test_deserializing_a_serialized_list_returns_the_same_list(self):
     self.assertEqual(deserialize('[1, 2, 3]'), [1, 2, 3])
예제 #18
0
 def test_deserializing_a_serialized_float_returns_the_same_float(self):
     self.assertEqual(deserialize('3.14'), 3.14)
예제 #19
0
 def test_deserializing_a_serialized_int_returns_the_same_int(self):
     self.assertEqual(deserialize('1'), 1)
예제 #20
0
 def test_deserializing_a_serialized_string_returns_the_same_string(self):
     self.assertEqual(deserialize('"test"'), 'test')
예제 #21
0
 def test_deserializing_a_serialized_dict_returns_the_same_dict(self):
     self.assertEqual(deserialize('{"test": "test"}'), {'test': 'test'})
예제 #22
0
    def test_deserializing_an_object_id_string_returns_an_object_id(self):
        oid = ObjectId('0123456789ab0123456789ab')

        self.assertEqual(deserialize('0123456789ab0123456789ab'), oid)