def setup_example_fixtures(connection): connection.register(User) connection.generate_index(User) try: User.create(connection.db, u'hadrien', u'*****@*****.**') except Exception: log.exception('setup fixture failed')
def setup_example_fixtures(connection): from example.model import User try: User.create(connection.db, u'hadrien', u'*****@*****.**') except Exception: log.exception('setup fixture failed')
def list_users(request): if 'name' in request.params: users = User.filter_by_name(request.params['name']) else: users = User.all() return [{ 'id': user.id, 'name': user.name, } for user in users]
def test_auto_incr(self): from example.model import User, UserNote # create user and user notes user = User(id=1, name=u'Bob') msg0 = UserNote(id=11, content='I ray') msg1 = UserNote(id=22, content='Jah') user.notes = [msg0, msg1] self.sqla_session.add(user) self.sqla_session.commit() key_user_v0 = self.versioner.get_multi_keys([user]) key_msg0_v0 = self.versioner.get_multi_keys([msg0]) key_msg1_v0 = self.versioner.get_multi_keys([msg1]) # modify user and msg0 user.name = 'Bob Marley' msg0.content = 'I RAY' self.sqla_session.commit() key_user_v1 = self.versioner.get_multi_keys([user]) key_msg0_v1 = self.versioner.get_multi_keys([msg0]) # keys are automaticaly changed self.assertNotEqual(key_user_v0, key_user_v1) self.assertNotEqual(key_msg0_v0, key_msg0_v1) # not msg1 self.assertEqual(key_msg1_v0, self.versioner.get_multi_keys([msg1])) # we can get a key via a new object instance self.assertEqual( key_msg0_v1, self.versioner.get_multi_keys([UserNote(id=11, user_id=user.id)]), ) user = self.sqla_session.query(User).get(1) # remove msg0 user.notes.pop(0) user_id = user.id self.sqla_session.commit() # msg0 key has changed key_msg0_v2 = self.versioner.get_multi_keys( [UserNote(id=11, user_id=user_id)]) self.assertNotEqual(key_msg0_v1, key_msg0_v2) # delete user which cascade delete msg1 user = self.sqla_session.query(User).get(1) self.sqla_session.delete(user) self.sqla_session.commit() self.assertNotEqual(key_user_v1, self.versioner.get_multi_keys([user])) self.assertNotEqual(key_msg1_v0, self.versioner.get_multi_keys([msg1]))
def test_auto_incr(self): from example.model import User, UserNote # create user and user notes user = User(id=1, name=u"Bob") msg0 = UserNote(id=11, content="I ray") msg1 = UserNote(id=22, content="Jah") user.notes = [msg0, msg1] self.sqla_session.add(user) self.sqla_session.commit() key_user_v0 = self.versioner.get_multi_keys([user]) key_msg0_v0 = self.versioner.get_multi_keys([msg0]) key_msg1_v0 = self.versioner.get_multi_keys([msg1]) # modify user and msg0 user.name = "Bob Marley" msg0.content = "I RAY" self.sqla_session.commit() key_user_v1 = self.versioner.get_multi_keys([user]) key_msg0_v1 = self.versioner.get_multi_keys([msg0]) # keys are automaticaly changed self.assertNotEqual(key_user_v0, key_user_v1) self.assertNotEqual(key_msg0_v0, key_msg0_v1) # not msg1 self.assertEqual(key_msg1_v0, self.versioner.get_multi_keys([msg1])) # we can get a key via a new object instance self.assertEqual(key_msg0_v1, self.versioner.get_multi_keys([UserNote(id=11, user_id=user.id)])) user = self.sqla_session.query(User).get(1) # remove msg0 user.notes.pop(0) user_id = user.id self.sqla_session.commit() # msg0 key has changed key_msg0_v2 = self.versioner.get_multi_keys([UserNote(id=11, user_id=user_id)]) self.assertNotEqual(key_msg0_v1, key_msg0_v2) # delete user which cascade delete msg1 user = self.sqla_session.query(User).get(1) self.sqla_session.delete(user) self.sqla_session.commit() self.assertNotEqual(key_user_v1, self.versioner.get_multi_keys([user])) self.assertNotEqual(key_msg1_v0, self.versioner.get_multi_keys([msg1]))
def test_unicity_composite_pk(self): from example.model import User, UserNote msg0 = UserNote(id=11, content="I ray") msg1 = UserNote(id=22, content="Jah") user = User(name=u"Bob Marley") user.notes = [msg0, msg1] self.sqla_session.add(user) self.sqla_session.commit() self.assertNotEqual(self.versioner.get_multi_keys([msg0]), self.versioner.get_multi_keys([msg1]))
def test_unicity_simple_pk(self): from example.model import User user0 = User(name=u'Bob Marley') user1 = User(name=u'Peter Tosh') self.sqla_session.add(user0) self.sqla_session.add(user1) self.sqla_session.commit() self.assertNotEqual( self.versioner.get_multi_keys([user0]), self.versioner.get_multi_keys([user1]), )
def test_unicity_composite_pk(self): from example.model import User, UserNote msg0 = UserNote(id=11, content='I ray') msg1 = UserNote(id=22, content='Jah') user = User(name=u'Bob Marley') user.notes = [msg0, msg1] self.sqla_session.add(user) self.sqla_session.commit() self.assertNotEqual( self.versioner.get_multi_keys([msg0]), self.versioner.get_multi_keys([msg1]), )
def _add_users(self): from example.model import User for i in range(50): name = u'user%s' % i user = User.create(self.db, name, name + '@email.com') setattr(self, 'user%s' % i, user) self.addCleanup(self._delete_users)
def get_user(request): user = User.get(request.matchdict['user_id']) if user is None: raise HTTPNotFound() return { 'id': user.id, 'name': user.name, }
def setUp(self): os.environ['CACHING_ENABLED'] = 'false' # creating app to setup cache and decorate stuff. self.app from example.model import User, Session session = Session() session.add(User(id=1, name='Bob')) session.commit()
async def send(agent, count=100): await asyncio.gather( *[ agent_worker.send( key="Onna", value=User(name=f"Onna-{age}", age=age + 1, city=City(name="Durham")), ) for age in range(count) ] )
def setUp(self): # creating app to setup cache and decorate stuff. self.app from example.model import User, Session session = Session() session.add(User(id=1, name='Bob')) session.commit() session.close() self.cache_client.client.delete('cache')
def replace(self, params): new_username = params['username'] new_email = params['email'] user = User.replace(self.root.db, self.name, new_username=new_username, new_email=new_email) Photo.replace_author(self.root.db, self.name, new_username) new_item = Item(new_username, self.__parent__, self.request, user) user['href'] = new_item.url() user['photos'] = {'href': new_item['photos'].url()} return user
def replace(self, params): new_username = params['username'] new_email = params['email'] user = User.replace(self.root.db, self.name, new_username=new_username, new_email=new_email) Photo.replace_author(self.root.db, self.name, new_username) new_item = Item(new_username, self.parent, user) user['href'] = new_item.url() user['photos'] = { 'href': new_item['photos'].url() } return user
def test_incr(self): from example.model import User user = User() key_cls_0 = self.versioner.get_multi_keys([User]) key_obj_0 = self.versioner.get_multi_keys([user]) self.versioner.incr(User) self.versioner.incr(user) key_cls_1 = self.versioner.get_multi_keys([User]) key_obj_1 = self.versioner.get_multi_keys([user]) self.assertNotEqual(key_cls_1, key_cls_0) self.assertNotEqual(key_obj_1, key_obj_0)
def index(self, offset, limit): cursor = User.get_newests(self.root.db, offset, limit) documents = [Item(user.username, self, user).show() for user in cursor] result = { 'users': documents, 'href': self.url(offset=offset, limit=limit), 'first': self.url(offset=0, limit=limit), } has_previous = offset > 0 if has_previous: result['previous'] = self.url(offset=max(offset - limit, 0), limit=limit) has_next = len(documents) == limit if has_next: result['next'] = self.url(offset=offset + limit, limit=limit) return result
async def send(count=100): producer = ProducerComponent( User, key_serializer=lambda key: key.encode(), bootstrap_servers="kafka-intra01.intra.onna.internal:9092", ) await producer.start() try: print(await asyncio.gather(*[ producer.send( key=f"Onna-{age}", value=User( name=f"Onna-{age}", age=age + 1, city=City(name="Durham")), ) for age in range(count) ])) finally: await producer.stop()
def index(self, params): offset = params['offset'] limit = params['limit'] cursor = User.get_newests(self.root.db, offset, limit) documents = [ Item(user.username, self, self.request, user).show() for user in cursor ] result = { 'users': documents, 'href': self.url(offset=offset, limit=limit), 'first': self.url(offset=0, limit=limit), } has_previous = offset > 0 if has_previous: result['previous'] = self.url(offset=max(offset - limit, 0), limit=limit) has_next = len(documents) == limit if has_next: result['next'] = self.url(offset=offset + limit, limit=limit) return result
def delete(self): result = User.delete_one(self.root.db, username=self.name) if result and result['ok'] != 1: raise NotFound(self)
def _add_single_user(self, name, email): from example.model import User user = User.create(self.db, unicode(name), unicode(email)) self.addCleanup(user.delete) return user
def load_document(self): if self.document is None: self.document = User.get_one(self.root.db, username=self.name) if self.document is None: raise NotFound(self) return self.document
def index(self, offset, limit): cursor = User.get_newests(self.root.db, offset, limit) query = dict(offset=offset, limit=limit) return royal.PaginatedResult(self, cursor, Resource, query, cursor.count())