def test_schema_with_dict(self): data = { '$this.thing': 'that', 'a.t': { 'a.b': 'thing', 'dudeman': { 'hello.world': 'nothing', } }, } list_data = [{'this.thing': 'that'}] tmp = SchemaWithDict.create(data=data, list_data=list_data) raw = SchemaWithDict.collection.find_one({'_id': tmp.id}) self.assertTrue('$this.thing' in raw['data']) self.assertTrue('a.t' in raw['data']) self.assertTrue('a.b' in raw['data']['a.t']) self.assertTrue(MongoSchema._unfix_dict_keys(raw['data']) == data) self.assertTrue( MongoSchema._unfix_dict_keys(raw['list_data']) == list_data) self.assertTrue(type(tmp.data) is dict) new_data = { 'hello': 'world', 'dudeman': { 'h.w': 'nothing', } } tmp.data = new_data tmp.save() raw = SchemaWithDict.collection.find_one({'_id': tmp.id}) self.assertTrue('h.w' in raw['data']['dudeman']) self.assertTrue( MongoSchema._unfix_dict_keys(raw['data']) == new_data, new_data)
def test_schema_with_dict(self): data = { '$this.thing': 'that', 'a.t': { 'a.b': 'thing', 'dudeman': { 'hello.world': 'nothing', } }, } list_data = [ {'this.thing': 'that'} ] tmp = SchemaWithDict.create(data=data, list_data=list_data) raw = SchemaWithDict.collection.find_one({'_id': tmp.id}) self.assertTrue('$this.thing' in raw['data']) self.assertTrue('a.t' in raw['data']) self.assertTrue('a.b' in raw['data']['a.t']) self.assertTrue(MongoSchema._unfix_dict_keys(raw['data']) == data) self.assertTrue( MongoSchema._unfix_dict_keys(raw['list_data']) == list_data) self.assertTrue(type(tmp.data) is dict) new_data = { 'hello': 'world', 'dudeman': { 'h.w': 'nothing', } } tmp.data = new_data tmp.save() raw = SchemaWithDict.collection.find_one({'_id': tmp.id}) self.assertTrue('h.w' in raw['data']['dudeman']) self.assertTrue( MongoSchema._unfix_dict_keys(raw['data']) == new_data, new_data)
def test_list(self): users = [] for i in range(0, 10): users.append(_create_user(username='******' % i)) MongoSchema.clear_cache_and_init() users_from_db = User.list() for user in users_from_db: self.assertTrue(user in users)
def test_reference_query(self): user = _create_user() email = self._create_email(user) MongoSchema.clear_cache_and_init() email_from_query = Email.get(user=user) self.assertTrue(email.id == email_from_query.id) MongoSchema.clear_cache_and_init() email_from_find = [x for x in Email.find(user=user)][0] self.assertTrue(email.id == email_from_find.id)
def test_disabled_cache(self): MongoSchema.disable_cache() user = _create_user() user_again = User.get(id=user.id) self.assertTrue(user is not user_again) MongoSchema.enable_cache()
def test_schemaless_embeded_doc(self): data = {'testing': 'nothing', 'hellow': 'world'} doc = EmbedDoc.create(data=data) raw_doc = doc.ms.collection.find_one({'_id': doc.id}) self.assertEqual(MongoSchema._unfix_dict_keys(raw_doc['data']), data)
def _setUp(): conn.drop_database(TEST_DB_NAME) MongoSchema.clear_cache_and_init()