Ejemplo n.º 1
0
    def test_repr_returns_correct_representation_of_the_document(self):
        document = Document(Collection)

        self.assertEqual(
            document.__repr__(),
            '<Document<Collection> object at {0}>'.format(hex(id(document)))
        )
Ejemplo n.º 2
0
    def test_set_attr_correctly_sets__collection(self):
        document = Document(Collection)

        class TestCollection(Collection):
            pass

        document._collection = TestCollection

        self.assertEqual(document._collection, TestCollection)
Ejemplo n.º 3
0
    def test_save_returns_errors_if_error_ocurred_during_save(self):
        Collection.collection.create_index('test', unique=True)
        Collection.insert_one({'test': 'test'})

        document = Document(Collection)
        document.test = 'test'
        errors = document.save()

        self.assertEqual(errors['error_code'], 0)
        self.assertEqual(errors['error_type'], 'PyMongoError')

        Collection.drop_index('test_1')
Ejemplo n.º 4
0
    def test_update_returns_errors_if_error_ocurred_during_save(self):
        Collection.collection.create_index('test', unique=True)
        Collection.insert_one({'test': 'test1'})
        _id = Collection.insert_one({'test': 'test2'})

        errors = Document(Collection, {'_id': _id, 'test': 'test1'}).update()

        errors.pop('error_message')
        self.assertEqual(
            errors,
            {
                'error_code': 0,
                'error_type': 'PyMongoError',
                'operation': 'update',
                'collection': 'Collection',
                'document': {'_id': _id, 'test': 'test1'}
            }
        )

        Collection.drop_index('test_1')
Ejemplo n.º 5
0
    def test_set_attr_correctly_sets_field(self):
        document = Document(Collection)
        document.test = 1

        self.assertEqual(document._fields['test'], 1)
Ejemplo n.º 6
0
    def test_set_attr_correctly_sets__fields(self):
        document = Document(Collection)
        document._fields = {'test': 'test'}

        self.assertEqual(document._fields, {'test': 'test'})
Ejemplo n.º 7
0
    def test_save_returns_fields_if_document_has_id_and_is_valid(self):
        document = Document(Collection)
        document._id = ObjectId()
        fields = document.save()

        self.assertEqual(fields, document._fields)
Ejemplo n.º 8
0
    def test_errors_returns_empty_dict_if_no_errors(self):
        document = Document(Collection, {})
        document._errors = {}

        self.assertEqual({}, document.errors)
Ejemplo n.º 9
0
    def test_is_valid_returns_false_if_there_are_errors(self):
        document = Document(Collection)
        document._errors = {'test': 'test'}

        self.assertFalse(document.is_valid)
Ejemplo n.º 10
0
    def test_is_valid_returns_true_if_no_errors(self):
        document = Document(Collection)
        document._errors = {}

        self.assertTrue(Document(Collection).is_valid)