示例#1
0
 def test_save_old(self, mock_db):
     update: MagicMock = mock_db.__getitem__.return_value.update_one
     doc = Document(_id='test')
     doc.save()
     update.assert_called_once_with({'_id': 'test'}, {'$set': {}},
                                    session=None)
     assert doc._id == 'test'
示例#2
0
 def test_is_valid_with_raises(self):
     field_obj = Field()
     field_obj.is_valid = lambda: False
     doc = Document(collection='test')
     doc.f = field_obj
     with pytest.raises(doc.DocumentInvalid):
         doc.is_valid(raises=True)
示例#3
0
 def test_delete_legit(self, mock_db):
     delete_one = mock_db.__getitem__.return_value.delete_one
     delete_one.return_value = True
     doc = Document(_id='foo')
     doc.delete()
     assert doc._id is None
     delete_one.assert_called_once_with({'_id': 'foo'}, session=None)
示例#4
0
 def test_fields_append(self):
     doc = Document(collection='test')
     assert doc.fields == []
     doc.name = Field(default=lambda: 'alpha')
     doc.age = Field(default=lambda: 'bravo')
     assert doc.fields == ['name', 'age']
     assert doc.name == 'alpha'
     assert doc.age == 'bravo'
示例#5
0
 def test_refresh_legit(self, mock_db):
     find_one = mock_db.__getitem__.return_value.find_one
     find_one.return_value = {'test': True}
     doc = Document(_id=42)
     doc.test = Field()
     doc.refresh()
     find_one.assert_called_once_with({'_id': 42}, session=None)
     assert doc.test is True
示例#6
0
 def test_update(self):
     original = Document(collection='test')
     original.name = Field(value='named test')
     original.age = Field(value=12)
     cpy = original.copy()
     cpy.clear()
     cpy.update(name='bibou', age=30)
     assert cpy.name == 'bibou'
     assert original.name == 'named test'
     assert original.age == 12
     assert cpy.age == 30
示例#7
0
 def test_copy(self):
     doc = Document(collection='test', _id='abcdef')
     doc.name = Field(value='named test', default=lambda: 'tested')
     cpy = doc.copy()
     assert doc != cpy
     assert doc.name == cpy.name
     cpy.name = None
     assert cpy.name == 'tested'
     assert doc.name == 'named test'
     assert cpy._id is None
     assert cpy.collection == doc.collection
     assert doc.raw_attr('name') != cpy.raw_attr('name')
示例#8
0
    def test_delete_many(self, mock_db):
        delete_many = mock_db.__getitem__.return_value.delete_many
        delete_many.return_value = None

        documents_list = list(
            [Document('test', _id=object()) for _ in range(10)])

        ids = list([doc._id for doc in documents_list])
        Document.delete_many(documents_list)
        delete_many.assert_called_once_with({'_id': {
            '$in': ids
        }},
                                            session=None)
        for doc in documents_list:
            assert doc._id is None
示例#9
0
 def test_is_valid(self):
     doc = Document(collection='test')
     doc.foo = Field()
     assert doc.is_valid() is True
     del doc.foo
     doc.foo = InvalidField()
     assert doc.is_valid() is False
示例#10
0
 def test_to_dict(self):
     doc = Document(collection='test')
     doc.creation_date = Field(default=lambda: datetime.now().isoformat())
     doc.name = Field()
     doc.name = 'tomtom'
     doc_dict = doc.to_dict()
     assert 'creation_date' in doc_dict
     assert 'collection' not in doc_dict
     assert doc_dict['name'] == 'tomtom'
     assert len(doc_dict) == 2
示例#11
0
 def test_invalid_fields(self):
     doc = Document(collection='test')
     doc.foo = InvalidField()
     doc.bar = InvalidField()
     doc.ham = Field()
     invalid_fields = doc.invalid_fields()
     for field_name in ('foo', 'bar'):
         assert field_name in invalid_fields
         invalid_fields.remove(field_name)
     assert len(invalid_fields) == 0
示例#12
0
 def test_delete_unknow(self):
     doc = Document(collection='test')
     assert doc.delete() is None
示例#13
0
 def test_save_invalid(self):
     doc = Document(collection='test')
     doc.is_valid = lambda: False
     with pytest.raises(Document.DocumentInvalid):
         doc.save()
示例#14
0
 def test_save_new(self, mock_db):
     insert: MagicMock = mock_db.__getitem__.return_value.insert_one
     insert.return_value.inserted_id.return_value = 42
     doc = Document()
     doc.save()
     insert.assert_called_once_with({}, session=None)
示例#15
0
 def test_from_unknow_id(self, mock_db):
     mock_db.__getitem__.return_value.find_one.return_value = None
     with pytest.raises(Document.DoesNotExist):
         Document.from_id('test')
示例#16
0
 def test_from_id(self, mock_db):
     test_dict = {'test': True}
     mock_db.__getitem__.return_value.find_one.return_value = test_dict
     doc = Document.from_id('test')
     assert isinstance(doc, Document)
示例#17
0
 def test_refresh_without_id(self):
     doc = Document(collection='test')
     with pytest.raises(ValueError):
         doc.refresh()