def test_deserailize_colprop(self): TestTable = self.classes.TestTable s = scoped_session(sessionmaker()) expr = s.query(TestTable).filter(TestTable.some_primary_id == 123456) expr2 = serializer.loads(serializer.dumps(expr), TestTable.metadata, s) # note in the original, the same bound parameter is used twice self.assert_compile( expr, "SELECT test.some_id AS test_some_id, " "CAST(left(test.some_id, :left_1) AS INTEGER) AS anon_1, " "test.id AS test_id FROM test WHERE " "CAST(left(test.some_id, :left_1) AS INTEGER) = :param_1", checkparams={"left_1": 6, "param_1": 123456}, ) # in the deserialized, it's two separate parameter objects which # need to have different anonymous names. they still have # the same value however self.assert_compile( expr2, "SELECT test.some_id AS test_some_id, " "CAST(left(test.some_id, :left_1) AS INTEGER) AS anon_1, " "test.id AS test_id FROM test WHERE " "CAST(left(test.some_id, :left_2) AS INTEGER) = :param_1", checkparams={"left_1": 6, "left_2": 6, "param_1": 123456}, )
def test_columns(self): assert ( serializer.loads( serializer.dumps(users.c.name, -1), users.metadata, Session ) is users.c.name )
def test_tables(self): assert ( serializer.loads( serializer.dumps(users, -1), users.metadata, Session ) is users )
def test_query_one(self): q = ( Session.query(User) .filter(User.name == "ed") .options(joinedload(User.addresses)) ) q2 = serializer.loads(serializer.dumps(q, -1), users.metadata, Session) def go(): eq_( q2.all(), [ User( name="ed", addresses=[ Address(id=2), Address(id=3), Address(id=4), ], ) ], ) self.assert_sql_count(testing.db, go, 1) eq_( q2.join(User.addresses) .filter(Address.email == "*****@*****.**") .value(func.count(literal_column("*"))), 1, ) u1 = Session.query(User).get(8) q = ( Session.query(Address) .filter(Address.user == u1) .order_by(desc(Address.email)) ) q2 = serializer.loads(serializer.dumps(q, -1), users.metadata, Session) eq_( q2.all(), [ Address(email="*****@*****.**"), Address(email="*****@*****.**"), Address(email="*****@*****.**"), ], )
def test_orm_join(self): from sqlalchemy_1_3.orm.util import join j = join(User, Address, User.addresses) j2 = serializer.loads(serializer.dumps(j, -1), users.metadata) assert j2.left is j.left assert j2.right is j.right
def test_annotated_one(self): j = join(users, addresses)._annotate({"foo": "bar"}) query = select([addresses]).select_from(j) str(query) for prot in pickle_protocols(): pickled_failing = serializer.dumps(j, prot) serializer.loads(pickled_failing, users.metadata, None)
def test_query_two(self): q = ( Session.query(User) .join(User.addresses) .filter(Address.email.like("%fred%")) ) q2 = serializer.loads(serializer.dumps(q, -1), users.metadata, Session) eq_(q2.all(), [User(name="fred")]) eq_(list(q2.values(User.id, User.name)), [(9, "fred")])
def test_expression(self): expr = select([users]).select_from(users.join(addresses)).limit(5) re_expr = serializer.loads( serializer.dumps(expr, -1), users.metadata, None ) eq_(str(expr), str(re_expr)) assert re_expr.bind is testing.db eq_( re_expr.execute().fetchall(), [(7, "jack"), (8, "ed"), (8, "ed"), (8, "ed"), (9, "fred")], )
def test_query_three(self): ua = aliased(User) q = ( Session.query(ua) .join(ua.addresses) .filter(Address.email.like("%fred%")) ) for prot in pickle_protocols(): q2 = serializer.loads( serializer.dumps(q, prot), users.metadata, Session ) eq_(q2.all(), [User(name="fred")]) # try to pull out the aliased entity here... ua_2 = q2._entities[0].entity_zero.entity eq_(list(q2.values(ua_2.id, ua_2.name)), [(9, "fred")])
def test_aliases(self): u7, u8, u9, u10 = Session.query(User).order_by(User.id).all() ualias = aliased(User) q = ( Session.query(User, ualias) .join(ualias, User.id < ualias.id) .filter(User.id < 9) .order_by(User.id, ualias.id) ) eq_( list(q.all()), [(u7, u8), (u7, u9), (u7, u10), (u8, u9), (u8, u10)] ) q2 = serializer.loads(serializer.dumps(q, -1), users.metadata, Session) eq_( list(q2.all()), [(u7, u8), (u7, u9), (u7, u10), (u8, u9), (u8, u10)], )
def test_unicode(self): m = MetaData() t = Table( ue("\u6e2c\u8a66"), m, Column(ue("\u6e2c\u8a66_id"), Integer) ) expr = select([t]).where(t.c[ue("\u6e2c\u8a66_id")] == 5) expr2 = serializer.loads(serializer.dumps(expr, -1), m) self.assert_compile( expr2, ue( 'SELECT "\u6e2c\u8a66"."\u6e2c\u8a66_id" FROM "\u6e2c\u8a66" ' 'WHERE "\u6e2c\u8a66"."\u6e2c\u8a66_id" = :\u6e2c\u8a66_id_1' ), dialect="default", )
def test_any(self): r = User.addresses.any(Address.email == "x") ser = serializer.dumps(r, -1) x = serializer.loads(ser, users.metadata) eq_(str(r), str(x))
def test_attribute(self): assert ( serializer.loads(serializer.dumps(User.name, -1), None, None) is User.name )
def test_mapper(self): user_mapper = class_mapper(User) assert ( serializer.loads(serializer.dumps(user_mapper, -1), None, None) is user_mapper )