Ejemplo n.º 1
0
    def test_bulk_update_future_synchronize_evaluate(self):
        sess = self._fixture_data()

        eq_(
            set(row.temperature
                for row in sess.execute(future_select(Report.temperature))),
            {80.0, 75.0, 85.0},
        )

        temps = sess.execute(future_select(Report)).scalars().all()
        eq_(set(t.temperature for t in temps), {80.0, 75.0, 85.0})

        sess.execute(
            update(Report).filter(Report.temperature >= 80).values(
                {
                    "temperature": Report.temperature + 6
                }, ).execution_options(synchronize_session="evaluate"))

        eq_(
            set(row.temperature
                for row in sess.execute(future_select(Report.temperature))),
            {86.0, 75.0, 91.0},
        )

        # test synchronize session as well
        eq_(set(t.temperature for t in temps), {86.0, 75.0, 91.0})
Ejemplo n.º 2
0
    def test_bulk_update_future_synchronize_fetch(self):
        sess = self._fixture_data()

        eq_(
            set(row.temperature
                for row in sess.execute(future_select(Report.temperature))),
            {80.0, 75.0, 85.0},
        )

        temps = sess.execute(future_select(Report)).scalars().all()
        eq_(set(t.temperature for t in temps), {80.0, 75.0, 85.0})

        # MARKMARK
        # omitting the criteria so that the UPDATE affects three out of
        # four shards
        sess.execute(
            update(Report).values({
                "temperature": Report.temperature + 6
            }, ).execution_options(synchronize_session="fetch"))

        eq_(
            set(row.temperature
                for row in sess.execute(future_select(Report.temperature))),
            {86.0, 81.0, 91.0},
        )

        # test synchronize session as well
        eq_(set(t.temperature for t in temps), {86.0, 81.0, 91.0})
Ejemplo n.º 3
0
    def test_roundtrip_future(self):
        sess = self._fixture_data()

        tokyo = (sess.execute(
            future_select(WeatherLocation).filter_by(
                city="Tokyo")).scalars().one())
        eq_(tokyo.city, "Tokyo")

        asia_and_europe = sess.execute(
            future_select(WeatherLocation).filter(
                WeatherLocation.continent.in_(["Europe", "Asia"]))).scalars()
        eq_(
            {c.city
             for c in asia_and_europe},
            {"Tokyo", "London", "Dublin"},
        )
Ejemplo n.º 4
0
 def test_filter_by_no_property(self):
     assert_raises_message(
         exc.InvalidRequestError,
         'Entity namespace for "mytable" has no property "foo"',
         future_select(table1).filter_by,
         foo="bar",
     )
Ejemplo n.º 5
0
def test_orm_query_new_style_using_embedded_lambdas(n):
    """new style ORM select() of the full entity w/ embedded lambdas."""
    session = Session(bind=engine)
    for id_ in random.sample(ids, n):
        stmt = future_select(lambda: Customer).where(
            lambda: Customer.id == id_)
        session.execute(stmt).scalar_one()
Ejemplo n.º 6
0
def test_core_just_statement_construct_plus_cache_key(n):
    for i in range(n):
        stmt = future_select(Customer.__table__).where(
            Customer.id == bindparam("id")
        )

        stmt._generate_cache_key()
Ejemplo n.º 7
0
    def test_join_nofrom_explicit_left_side_implicit_onclause(self):
        stmt = future_select(parent).join_from(parent, child)

        self.assert_compile(
            stmt,
            "SELECT parent.id, parent.data FROM parent JOIN child "
            "ON parent.id = child.parent_id",
        )
Ejemplo n.º 8
0
def test_orm_query_new_style_ext_lambdas_cols_only(n):
    """new style ORM query w/ external lambdas against columns."""
    s = Session(bind=engine)
    for id_ in random.sample(ids, n):
        stmt = lambdas.lambda_stmt(lambda: future_select(
            Customer.id, Customer.name, Customer.description)) + (
                lambda s: s.filter(Customer.id == id_))
        s.execute(stmt).one()
Ejemplo n.º 9
0
    def test_join_froms_explicit_left_side_explicit_onclause(self):
        stmt = (future_select(table1).select_from(table1).join_from(
            table1, table2, table1.c.myid == table2.c.otherid))

        self.assert_compile(
            stmt,
            "SELECT mytable.myid, mytable.name, mytable.description "
            "FROM mytable JOIN myothertable "
            "ON mytable.myid = myothertable.otherid",
        )
Ejemplo n.º 10
0
    def test_bulk_delete_future_synchronize_fetch(self):
        sess = self._fixture_data()

        temps = sess.execute(future_select(Report)).scalars().all()
        eq_(set(t.temperature for t in temps), {80.0, 75.0, 85.0})

        sess.execute(
            delete(Report).filter(Report.temperature >= 80).execution_options(
                synchronize_session="fetch"))

        eq_(
            set(row.temperature
                for row in sess.execute(future_select(Report.temperature))),
            {75.0},
        )

        # test synchronize session as well
        for t in temps:
            assert inspect(t).deleted is (t.temperature >= 80)
Ejemplo n.º 11
0
    def test_query_explicit_shard_via_bind_opts(self):
        sess = self._fixture_data()

        stmt = future_select(WeatherLocation).filter(WeatherLocation.id == 1)

        tokyo = (sess.execute(stmt, bind_arguments={
            "shard_id": "asia"
        }).scalars().first())

        eq_(tokyo.city, "Tokyo")
Ejemplo n.º 12
0
 def test_temp_table(self, connection):
     table_name = "#tmp"
     t = Table(
         table_name,
         self.metadata,
         Column("id", Integer, primary_key=True),
         Column("txt", String(50)),
     )
     t.create(connection)
     connection.execute(t.insert({"txt": "temp table test"}))
     result = connection.scalar(future_select(t.c.id))
     eq_(result, 1)
     connection.execute(text(f"DROP TABLE {table_name}"))
Ejemplo n.º 13
0
    def test_future_selects_w_orm_joins(self):

        User, Address, Keyword, Order, Item = self.classes(
            "User", "Address", "Keyword", "Order", "Item")

        a1 = aliased(Address)

        self._run_cache_key_fixture(
            lambda: (
                future_select(User).join(User.addresses),
                future_select(User).join(User.orders),
                future_select(User).join(User.addresses).join(User.orders),
                future_select(User).join(Address, User.addresses),
                future_select(User).join(a1, User.addresses),
                future_select(User).join(User.addresses.of_type(a1)),
                future_select(User).join(Address, User.addresses).join_from(
                    User, Order),
                future_select(User).join(Address, User.addresses).join_from(
                    User, User.orders),
                future_select(User.id, Order.id).select_from(
                    orm_join(User, Order, User.orders)),
            ),
            compare_values=True,
        )
Ejemplo n.º 14
0
    def test_bulk_update_evaluate(self):
        Edge, Point = (self.classes.Edge, self.classes.Point)

        sess = self._fixture()

        e1 = sess.execute(
            future_select(Edge).filter(Edge.start == Point(14, 5))
        ).scalar_one()

        eq_(e1.end, Point(2, 7))

        stmt = (
            update(Edge)
            .filter(Edge.start == Point(14, 5))
            .values({Edge.end: Point(16, 10)})
        )
        sess.execute(stmt)

        eq_(e1.end, Point(16, 10))
Ejemplo n.º 15
0
    def test_joins_w_filter_by(self):
        stmt = (future_select(parent).filter_by(
            data="p1").join(child).filter_by(data="c1").join_from(
                table1, table2,
                table1.c.myid == table2.c.otherid).filter_by(otherid=5))

        self.assert_compile(
            stmt,
            "SELECT parent.id, parent.data FROM parent JOIN child "
            "ON parent.id = child.parent_id, mytable JOIN myothertable "
            "ON mytable.myid = myothertable.otherid "
            "WHERE parent.data = :data_1 AND child.data = :data_2 "
            "AND myothertable.otherid = :otherid_1",
            checkparams={
                "data_1": "p1",
                "data_2": "c1",
                "otherid_1": 5
            },
        )
Ejemplo n.º 16
0
    def test_wp_objects(self):
        Person, Manager, Engineer, Boss = self.classes("Person", "Manager",
                                                       "Engineer", "Boss")

        self._run_cache_key_fixture(
            lambda: (
                inspect(with_polymorphic(Person, [Manager, Engineer])),
                inspect(with_polymorphic(Person, [Manager])),
                inspect(with_polymorphic(Person, [Manager, Engineer, Boss])),
                inspect(
                    with_polymorphic(Person, [Manager, Engineer], flat=True)),
                inspect(
                    with_polymorphic(
                        Person,
                        [Manager, Engineer],
                        future_select(Person).outerjoin(Manager).outerjoin(
                            Engineer).subquery(),
                    )),
            ),
            compare_values=True,
        )
Ejemplo n.º 17
0
    def test_bulk_update_sql(self):
        Edge, Point = (self.classes.Edge, self.classes.Point)

        sess = self._fixture()

        e1 = sess.execute(
            future_select(Edge).filter(Edge.start == Point(14, 5))
        ).scalar_one()

        eq_(e1.end, Point(2, 7))

        stmt = (
            update(Edge)
            .filter(Edge.start == Point(14, 5))
            .values({Edge.end: Point(16, 10)})
        )

        self.assert_compile(
            stmt,
            "UPDATE edges SET x2=:x2, y2=:y2 WHERE edges.x1 = :x1_1 "
            "AND edges.y1 = :y1_1",
            params={"x2": 16, "x1_1": 14, "y2": 10, "y1_1": 5},
            dialect="default",
        )