class Doc(BaseDocument, dot_notation=True, client=client, db='nanotestdb'): foo = Field(str) bar = Field(datetime.datetime, auto_update=True) moo = Field(int, required=False)
class Doc(BaseDocument, dot_notation=True, client=client, db='nanotestdb'): foo = Field(list) bar = Field(dict) moo = Field(str)
class Doc(BaseDocument, dot_notation=True, client=client, db='nanotestdb'): foo = Field(str) bar = Field(int, required=False) moo = Field(list)
class Doc(BaseDocument): foo = Field(str) bar = Field(int) __indexes__ = [ Index('foo.fail'), Index([('foo.fail', pymongo.ASCENDING), ('bar.fail', pymongo.ASCENDING)]), ]
def test_nanomongo_inits(self): self.assertRaises(TypeError, Nanomongo, *(1, )) self.assertRaises(TypeError, Nanomongo, **{'foo': 'bar'}) self.assertRaises(TypeError, Nanomongo, **{'fields': 'bar'}) self.assertRaises(TypeError, Nanomongo.from_dicts) self.assertRaises(TypeError, Nanomongo.from_dicts, *(1, )) fields_1 = {'foo': Field(str)} fields_2 = {'foo': Field(str), 'bar': Field(int)} nano_1 = Nanomongo(fields=fields_1) nano_2 = Nanomongo.from_dicts(fields_1, fields_2) self.assertEqual(['foo'], nano_1.list_fields()) self.assertEqual(['bar', 'foo'], nano_2.list_fields())
class Doc(BaseDocument, dot_notation=True, client=client, db='nanotestdb'): foo = Field(str) bar = Field(int) __indexes__ = [ Index('foo'), Index([('bar', pymongo.ASCENDING), ('foo', pymongo.DESCENDING)], unique=True), ]
class Doc4(BaseDocument): # test indexes on dotted keys foo = Field(list) bar = Field(dict) __indexes__ = [ Index('bar.moo'), # compund on two embedded document fields Index([('bar.moo', pymongo.ASCENDING), ('bar.zoo', pymongo.ASCENDING)]), # compound on embedded document field + list element document field Index([('bar.moo', pymongo.ASCENDING), ('foo.whatever', pymongo.ASCENDING)]), ]
class Doc2(Doc, client=client, db='nanotestdb'): moo = Field(float) __indexes__ = [ Index('bar'), # pointless, but index test on superclass field Index([('moo', pymongo.DESCENDING), ('foo', pymongo.ASCENDING)]), ]
def test_field_validators(self): """Test some validators""" bad_dicts = [ {'foo.bar': 42}, {'$foo': 42}, {'foo': {'bar.foo': 42}}, {'foo': {'$bar': 42}}, ] for dct in bad_dicts: self.assertRaises(ValidationError, Field(dict).validator, *(dct,))
class Doc2(Doc, dot_notation=True, client=client, db='nanomongotest', collection='othercollection'): bar = Field(int)
def wrapper(): Field(*args, **kwargs)
class Doc(BaseDocument, dot_notation=True): foo = Field(str) bar = Field(int, required=False) moo = Field(list)
class Doc(BaseDocument): foo = Field(str) __indexes__ = [Index('foo')]
class Doc2(Doc, dot_notation=True): foo = Field(dict) zoo = Field(list)
class Doc(BaseDocument, dot_notation=True, client=MOTOR_CLIENT, db='nanotestdb'): foo = Field(str) bar = Field(int, required=False)
class Doc3(Doc): bar = Field(float)
class NewDoc(Doc): foo = Field(dict)
class Doc(BaseDocument, dot_notation=True, client=client, db='nanomongotest'): foo = Field(str)
class Doc(BaseDocument): foo = Field()
class Doc(BaseDocument, DotNotationMixin): foo = Field(str)
class Doc(BaseDocument): foo = Field(str, default=1)
class Doc(BaseDocument): nanomongo = Field(int)
def test_good_types(self): """Test Field definitions with valid types""" types = (bool, int, float, bytes, str, list, dict, datetime.datetime, DBRef, ObjectId) [Field(t) for t in types]
class Doc(BaseDocument): bool_a, bool_b = Field(bool), Field(bool) foo = Field(int, default=42) bar = Field(str, default=None, required=False)
class Doc(BaseDocument, dot_notation=True): foo = Field(str)
class Doc2(BaseDocument, dot_notation=True): optional = Field(dict, required=False)
class Doc(BaseDocument, client=PYMONGO_CLIENT, db='nanotestdb'): foo = Field(str)
class Doc2(Doc): bar = Field(str, required=False)