def test_outerjointo_count(self): """test the join_to and outerjoin_to functions on SelectResults""" mapper(tables.User, tables.users, properties={ 'orders':relation(mapper(tables.Order, tables.orders, properties={ 'items':relation(mapper(tables.Item, tables.orderitems)) })) }) session = create_session() query = SelectResults(session.query(tables.User)) x = query.outerjoin_to('orders').outerjoin_to('items').select(or_(tables.Order.c.order_id==None,tables.Item.c.item_id==2)).count() assert x==2
def test_dslish(self): """test the same as witheagerload except building the query via SelectResults""" s = create_session() q=SelectResults(s.query(Test).options(eagerload('category'))) l=q.select ( and_(tests.c.owner_id==1,or_(options.c.someoption==None,options.c.someoption==False)) ).outerjoin_to('owner_option') result = ["%d %s" % ( t.id,t.category.name ) for t in l] print result assert result == [u'1 Some Category', u'3 Some Category']
def test_from(self): mapper(tables.User, tables.users, properties={ 'orders':relation(mapper(tables.Order, tables.orders, properties={ 'items':relation(mapper(tables.Item, tables.orderitems)) })) }) session = create_session() query = SelectResults(session.query(tables.User)) x = query.select_from([tables.users.outerjoin(tables.orders).outerjoin(tables.orderitems)]).\ filter(or_(tables.Order.c.order_id==None,tables.Item.c.item_id==2)) print x.compile() self.assert_result(list(x), tables.User, *tables.user_result[1:3])
def test_outerjointo(self): """test the join_to and outerjoin_to functions on SelectResults""" mapper(tables.User, tables.users, properties={ 'orders':relation(mapper(tables.Order, tables.orders, properties={ 'items':relation(mapper(tables.Item, tables.orderitems)) })) }) session = create_session() query = SelectResults(session.query(tables.User)) x = query.outerjoin_to('orders').outerjoin_to('items').select(or_(tables.Order.c.order_id==None,tables.Item.c.item_id==2)) print x.compile() self.assert_result(list(x), tables.User, *tables.user_result[1:3])
def test_dslish(self): """test the same as witheagerload except building the query via SelectResults""" s = create_session() q = SelectResults(s.query(Test).options(eagerload('category'))) l = q.select( and_( tests.c.owner_id == 1, or_(options.c.someoption == None, options.c.someoption == False))).outerjoin_to('owner_option') result = ["%d %s" % (t.id, t.category.name) for t in l] print result assert result == [u'1 Some Category', u'3 Some Category']
def test_basic(self): class Department(object): def __init__(self, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) def __repr__(self): return "<Department %s>" % (self.name,) class Employee(object): def __init__(self, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) def __repr__(self): return "<Employee %s>" % (self.name,) mapper(Employee, employees) mapper(Department, departments, properties=dict(employees=relation(Employee, lazy=False, backref='department'))) d1 = Department(name='One') for e in 'Jim Jack John Susan'.split(): d1.employees.append(Employee(name=e)) d2 = Department(name='Two') for e in 'Joe Bob Mary Wally'.split(): d2.employees.append(Employee(name=e)) sess = create_session() sess.save(d1) sess.save(d2) sess.flush() q = sess.query(Department) filters = [q.join_to('employees'), Employee.c.name.startswith('J')] d = SelectResults(q) d = d.join_to('employees').filter(Employee.c.name.startswith('J')) d = d.distinct() d = d.order_by([desc(Department.c.name)]) assert d.count() == 2 assert d[0] is d2
def test_basic(self): class Department(object): def __init__(self, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) def __repr__(self): return "<Department %s>" % (self.name, ) class Employee(object): def __init__(self, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) def __repr__(self): return "<Employee %s>" % (self.name, ) mapper(Employee, employees) mapper(Department, departments, properties=dict(employees=relation( Employee, lazy=False, backref='department'))) d1 = Department(name='One') for e in 'Jim Jack John Susan'.split(): d1.employees.append(Employee(name=e)) d2 = Department(name='Two') for e in 'Joe Bob Mary Wally'.split(): d2.employees.append(Employee(name=e)) sess = create_session() sess.save(d1) sess.save(d2) sess.flush() q = sess.query(Department) filters = [q.join_to('employees'), Employee.c.name.startswith('J')] d = SelectResults(q) d = d.join_to('employees').filter(Employee.c.name.startswith('J')) d = d.distinct() d = d.order_by([desc(Department.c.name)]) assert d.count() == 2 assert d[0] is d2