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)
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'
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
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
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
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
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
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')
class TestDocument(Document): collection = 'test' name = Field() age = Field() useless = 'test' ignoreme = 42
class User(Document): collection = 'test' name = Field()
class Test(Document): collection = 'test' value = Field() def is_valid(self): return False
class Test(Document): collection = 'test' a = Field(value='a1') b = Field(value='b1')
class Book(Document): name = Field() nested = Field()
def __init__(self, **kwargs): self.x = Field() self.y = Field() super().__init__(**kwargs)