text:
import pickle

import sqlalchemy as sa
from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy import inspect
from sqlalchemy import Integer
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import testing
from sqlalchemy.orm import aliased
from sqlalchemy.orm import attributes
from sqlalchemy.orm import column_property
from sqlalchemy.orm import contains_eager
from sqlalchemy.orm import defaultload
from sqlalchemy.orm import defer
from sqlalchemy.orm import exc as orm_exc
from sqlalchemy.orm import joinedload
from sqlalchemy.orm import Load
from sqlalchemy.orm import load_only
from sqlalchemy.orm import loading
from sqlalchemy.orm import relationship
from sqlalchemy.orm import strategy_options
from sqlalchemy.orm import subqueryload
from sqlalchemy.orm import synonym
from sqlalchemy.orm import undefer
from sqlalchemy.orm import util as orm_util
from sqlalchemy.orm import with_polymorphic
from sqlalchemy.testing import fixtures
from sqlalchemy.testing.assertions import assert_raises_message
from sqlalchemy.testing.assertions import AssertsCompiledSQL
from sqlalchemy.testing.assertions import emits_warning
from sqlalchemy.testing.assertions import eq_
from sqlalchemy.testing.assertions import expect_raises_message
from sqlalchemy.testing.fixtures import fixture_session
from test.orm import _fixtures
from .inheritance._poly_fixtures import _Polymorphic
from .inheritance._poly_fixtures import Company
from .inheritance._poly_fixtures import Engineer
from .inheritance._poly_fixtures import Manager
from .inheritance._poly_fixtures import Person

class QueryTest(_fixtures.FixtureTest):
    run_setup_mappers = "once"
    run_inserts = "once"
    run_deletes = None

    @classmethod
    def setup_mappers(cls):
        cls._setup_stock_mapping()

        class SubItem(cls.classes.Item):
            pass

        cls.mapper_registry.map_imperatively(
            SubItem,
            None,
            inherits=cls.classes.Item,
            properties={
                "extra_keywords": relationship(
                    cls.classes.Keyword,
                    viewonly=True,
                    secondary=cls.tables.item_keywords,
                )
            },
        )

        class OrderWProp(cls.classes.Order):
            @property
            def some_attr(self):
                return "hi"

        cls.mapper_registry.map_imperatively(
            OrderWProp, None, inherits=cls.classes.Order
        )

class PathTest:
    def _make_path(self, path):
        r = []
        for i, item in enumerate(path):
            if i % 2 == 0:
                item = inspect(item)
            else:
                if isinstance(item, str):
                    item = inspect(r[-1]).mapper.attrs[item]
            r.append(item)
        return tuple(r)

    def _make_path_registry(self, path):
        return orm_util.PathRegistry.coerce(self._make_path(path))

    def _assert_path_result(self, opt, q, paths):
        attr = {}

        compile_state = q._compile_state()
        compile_state.attributes = attr = {}
        opt.process_compile_state(compile_state)

        assert_paths = [k[1] for k in attr]
        eq_(
            {p for p in assert_paths},
            {self._make_path(p) for p in paths},
        )

class LoadTest(PathTest, QueryTest):
    def test_str(self):
        User = self.classes.User
        result = Load(User)
        eq_(
            str(result),
            "Load(Mapper[User(users)])",
        )

        result = Load(aliased(User))
        eq_(
            str(result),
            "Load(aliased(User))",
        )

    def test_gen_path_attr_entity(self):
        User = self.classes.User
        Address = self.classes.Address

        ll = Load(User)

        eq_(
            strategy_options._AttributeStrategyLoad.create(
                ll.path,
                User.addresses,
                ("strategy", True),
                "relationship",
                {},
                True,
            ).path,
            self._make_path_registry([User, "addresses", Address]),
        )

    def test_gen_path_attr_column(self):
        User = self.classes.User

        ll = Load(User)
        eq_(
            strategy_options._AttributeStrategyLoad.create(
                ll.path,
                User.name,
                ("strategy", True),
                "column",
                {},
                True,
            ).path,
            self._make_path_registry([User, "name"]),
        )

    def test_gen_path_invalid_from_col(self):
        User = self.classes.User

        result = Load(User)
        result.path = self._make_path_registry([User, "name"])
        assert_raises_message(
            sa.exc.ArgumentError,
            "Attribute 'name' of entity 'Mapper|User|users' does "
            "not refer to a mapped entity",
            result._clone_for_bind_strategy,
            (User.addresses,),
            None,
            "relationship",
        )

    def test_gen_path_attr_entity_invalid_raiseerr(self):
        User = self.classes.User
        Order = self.classes.Order

        result = Load(User)

        assert_raises_message(
            sa.exc.ArgumentError,
            "Attribute 'Order.items' does not link from element "
            "'Mapper|User|users'",
            result._clone_for_bind_strategy,
            (Order.items,),
            None,
            "relationship",
        )

    def test_gen_path_attr_entity_invalid_noraiseerr(self):
        User = self.classes.User
        Order = self.classes.Order

        ll = Load(User)

        eq_(
            strategy_options._AttributeStrategyLoad.create(
                ll.path,
                Order.items,
                ("strategy", True),
                "relationship",
                {},
                True,
                raiseerr=False,
            ),
            None,
        )

    def test_set_strat_ent(self):
        User = self.classes.User

        l1 = Load(User)
        l2 = l1.joinedload(User.addresses)

        s = fixture_session()
        q1 = s.query(User).options(l2)
        attr = q1._compile_context().attributes

        eq_(
            attr[("loader", self._make_path([User, "addresses"]))],
            l2.context[0],
        )

    def test_set_strat_col(self):
        User = self.classes.User

        l1 = Load(User)
        l2 = l1.defer(User.name)
        s = fixture_session()
        q1 = s.query(User).options(l2)
        attr = q1._compile_context().attributes

        eq_(attr[("loader", self._make_path([User, "name"]))], l2.context[0])

class OfTypePathingTest(PathTest, QueryTest):
    def _fixture(self):
        User, Address = self.classes.User, self.classes.Address
        Dingaling = self.classes.Dingaling
        address_table = self.tables.addresses

        class SubAddr(Address):
            pass

        self.mapper_registry.map_imperatively(
            SubAddr,
            inherits=Address,
            properties={
                "sub_attr": column_property(address_table.c.email_address),
                "dings": relationship(Dingaling, viewonly=True),
            },
        )

        return User, Address, SubAddr

    @emits_warning("This declarative base already contains a class")
    def test_oftype_only_col_attr_unbound(self):
        User, Address, SubAddr = self._fixture()

        l1 = defaultload(User.addresses.of_type(SubAddr)).defer(
            SubAddr.sub_attr
        )

        sess = fixture_session()
        q = sess.query(User)
        self._assert_path_result(
            l1,
            q,
            [(User, "addresses"), (User, "addresses", SubAddr, "sub_attr")],
        )

    @emits_warning("This declarative base already contains a class")
    def test_oftype_only_col_attr_bound(self):
        User, Address, SubAddr = self._fixture()

        l1 = (
            Load(User)
            .defaultload(User.addresses.of_type(SubAddr))
            .defer(SubAddr.sub_attr)
        )

        sess = fixture_session()
        q = sess.query(User)
        self._assert_path_result(
            l1,
            q,
            [(User, "addresses"), (User, "addresses", SubAddr, "sub_attr")],
        )

    @emits_warning("This declarative base already contains a class")
    def test_oftype_only_rel_attr_unbound(self):
        User, Address, SubAddr = self._fixture()

        l1 = defaultload(User.addresses.of_type(SubAddr)).joinedload(
            SubAddr.dings
        )

        sess = fixture_session()
        q = sess.query(User)
        self._assert_path_result(
            l1, q, [(User, "addresses"), (User, "addresses", SubAddr, "dings")]
        )

    @emits_warning("This declarative base already contains a class")
    def test_oftype_only_rel_attr_bound(self):
        User, Address, SubAddr = self._fixture()

        l1 = (
            Load(User)
            .defaultload(User.addresses.of_type(SubAddr))
            .joinedload(SubAddr.dings)
        )

        sess = fixture_session()
        q = sess.query(User)
        self._assert_path_result(
            l1, q, [(User, "addresses"), (User, "addresses", SubAddr, "dings")]
        )

class WithEntitiesTest(QueryTest, AssertsCompiledSQL):
    __dialect__ = "default"

    def test_options_legacy_with_entities_onelevel(self):
        """test issue #6253 (part of #6503)"""

        User = self.classes.User
        sess = fixture_session()

        q = (
            sess.query(User)
            .options(joinedload(User.addresses))
            .with_entities(User.id)
        )
        self.assert_compile(q, "SELECT users.id AS users_id FROM users")

    def test_options_with_only_cols_onelevel(self):
        """test issue #6253 (part of #6503)"""

        User = self.classes.User

        q = (
            select(User)
            .options(joinedload(User.addresses))
            .with_only_columns(User.id)
        )
        self.assert_compile(q, "SELECT users.id FROM users")

    def test_options_entities_replaced_with_equivs_one(self):
        User = self.classes.User
        Address = self.classes.Address

        q = (
            select(User, Address)
            .options(joinedload(User.addresses))
            .with_only_columns(User)
        )
        self.assert_compile(
            q,
            "SELECT users.id, users.name, addresses_1.id AS id_1, "
            "addresses_1.user_id, addresses_1.email_address FROM users "
            "LEFT OUTER JOIN addresses AS addresses_1 "
            "ON users.id = addresses_1.user_id ORDER BY addresses_1.id",
        )

    def test_options_entities_replaced_with_equivs_two(self):
        User = self.classes.User
        Address = self.classes.Address

        q = (
            select(User, Address)
            .options(joinedload(User.addresses), joinedload(Address.dingaling))
            .with_only_columns(User)
        )
        self.assert_compile(
            q,
            "SELECT users.id, users.name, addresses_1.id AS id_1, "
            "addresses_1.user_id, addresses_1.email_address FROM users "
            "LEFT OUTER JOIN addresses AS addresses_1 "
            "ON users.id = addresses_1.user_id ORDER BY addresses_1.id",
        )

    def test_options_entities_replaced_with_equivs_three(self):
        User = self.classes.User
        Address = self.classes.Address

        q = (
            select(User)
            .options(joinedload(User.addresses))
            .with_only_columns(User, Address)
            .options(joinedload(Address.dingaling))
            .join_from(User, Address)
        )
        self.assert_compile(
            q,
            "SELECT users.id, users.name, addresses.id AS id_1, "
            "addresses.user_id, addresses.email_address, "
            "addresses_1.id AS id_2, addresses_1.user_id AS user_id_1, "
            "addresses_1.email_address AS email_address_1, "
            "dingalings_1.id AS id_3, dingalings_1.address_id, "
            "dingalings_1.data "
            "FROM users JOIN addresses ON users.id = addresses.user_id "
            "LEFT OUTER JOIN addresses AS addresses_1 "
            "ON users.id = addresses_1.user_id "
            "LEFT OUTER JOIN dingalings AS dingalings_1 "
            "ON addresses.id = dingalings_1.address_id "
            "ORDER BY addresses_1.id",
        )

class OptionsTest(PathTest, QueryTest):
    def _option_fixture(self, *arg):
        # note contains_eager() passes chained=True to _from_keys,
        # which means an expression like contains_eager(a, b, c)
        # is expected to produce
        # contains_eager(a).contains_eager(b).contains_eager(c).  no other
        # loader option works this way right now; the rest all use
        # defaultload() for the "chain" elements
        return strategy_options._generate_from_keys(
            strategy_options.Load.contains_eager, arg, True, {}
        )

    @testing.combinations(
        lambda: joinedload("addresses"),
        lambda: defer("name"),
        lambda Address: joinedload("addresses").joinedload(Address.dingaling),
        lambda: joinedload("addresses"),
    )
    def test_error_for_string_names_unbound(self, test_case):
        User, Address = self.classes("User", "Address")

        with expect_raises_message(
            sa.exc.ArgumentError,
            "Strings are not accepted for attribute names in loader "
            "options; please use class-bound attributes directly.",
        ):
            testing.resolve_lambda(test_case, User=User, Address=Address)

    @testing.combinations(
        lambda User: Load(User).joinedload("addresses"),
        lambda User: Load(User).defer("name"),
        lambda User, Address: Load(User)
        .joinedload("addresses")
        .joinedload(Address.dingaling),
        lambda User: Load(User).joinedload("addresses"),
    )
    def test_error_for_string_names_bound(self, test_case):
        User, Address = self.classes("User", "Address")

        with expect_raises_message(
            sa.exc.ArgumentError,
            "Strings are not accepted for attribute names in loader "
            "options; please use class-bound attributes directly.",
        ):
            testing.resolve_lambda(test_case, User=User, Address=Address)

    def test_get_path_one_level_attribute(self):
        User = self.classes.User

        sess = fixture_session()
        q = sess.query(User)

        opt = self._option_fixture(User.addresses)
        self._assert_path_result(opt, q, [(User, "addresses")])

    def test_get_path_one_level_with_unrelated(self):
        Order = self.classes.Order
        User = self.classes.User

        sess = fixture_session()
        q = sess.query(Order)
        opt = self._option_fixture(User.addresses)

        with expect_raises_message(
            sa.exc.ArgumentError,
            r"Mapped class Mapper\[User\(users\)\] does not apply to any "
            "of the root entities in this query",
        ):
            self._assert_path_result(opt, q, [])

    def test_path_multilevel_attribute(self):
        Item, User, Order = (
            self.classes.Item,
            self.classes.User,
            self.classes.Order,
        )

        sess = fixture_session()
        q = sess.query(User)

        opt = self._option_fixture(User.orders, Order.items, Item.keywords)
        self._assert_path_result(
            opt,
            q,
            [
                (User, "orders"),
                (User, "orders", Order, "items"),
                (User, "orders", Order, "items", Item, "keywords"),
            ],
        )

    def test_with_current_matching_attribute(self):
        Item, User, Order = (
            self.classes.Item,
            self.classes.User,
            self.classes.Order,
        )

        sess = fixture_session()
        q = sess.query(Item)._with_current_path(
            self._make_path_registry([User, "orders", Order, "items"])
        )

        opt = self._option_fixture(User.orders, Order.items, Item.keywords)
        self._assert_path_result(opt, q, [(Item, "keywords")])

    def test_with_current_nonmatching_attribute(self):
        Item, User, Order = (
            self.classes.Item,
            self.classes.User,
            self.classes.Order,
        )

        sess = fixture_session()
        q = sess.query(Item)._with_current_path(
            self._make_path_registry([User, "orders", Order, "items"])
        )

        opt = self._option_fixture(Item.keywords)
        self._assert_path_result(opt, q, [])

        opt = self._option_fixture(Order.items, Item.keywords)
        self._assert_path_result(opt, q, [])

    def test_with_current_nonmatching_entity(self):
        Item, User, Order = (
            self.classes.Item,
            self.classes.User,
            self.classes.Order,
        )

        sess = fixture_session()
        q = sess.query(Item)._with_current_path(
            self._make_path_registry(
                [inspect(aliased(User)), "orders", Order, "items"]
            )
        )

        opt = self._option_fixture(User.orders)
        self._assert_path_result(opt, q, [])

        opt = self._option_fixture(User.orders, Order.items, Item.keywords)
        self._assert_path_result(opt, q, [])

        q = sess.query(Item)._with_current_path(
            self._make_path_registry([User, "orders", Order, "items"])
        )

        ac = aliased(User)

        opt = self._option_fixture(ac.orders)
        self._assert_path_result(opt, q, [])

        opt = self._option_fixture(ac.orders, Order.items, Item.keywords)
        self._assert_path_result(opt, q, [])

    def test_with_current_match_aliased_classes(self):
        Item, User, Order = (
            self.classes.Item,
            self.classes.User,
            self.classes.Order,
        )

        ac = aliased(User)
        sess = fixture_session()
        q = sess.query(Item)._with_current_path(
            self._make_path_registry([inspect(ac), "orders", Order, "items"])
        )

        opt = self._option_fixture(ac.orders, Order.items, Item.keywords)
        self._assert_path_result(opt, q, [(Item, "keywords")])

        opt = self._option_fixture(ac.orders, Order.items)
        self._assert_path_result(opt, q, [])

    @emits_warning("This declarative base already contains a class")
    def test_from_base_to_subclass_attr(self):
        Dingaling, Address = self.classes.Dingaling, self.classes.Address

        sess = fixture_session()

        class SubAddr(Address):
            pass

        self.mapper_registry.map_imperatively(
            SubAddr,
            inherits=Address,
            properties={"flub": relationship(Dingaling, viewonly=True)},
        )

        q = sess.query(Address)
        opt = self._option_fixture(SubAddr.flub)

        self._assert_path_result(opt, q, [(SubAddr, "flub")])

    @emits_warning("This declarative base already contains a class")
    def test_from_subclass_to_subclass_attr(self):
        Dingaling, Address = self.classes.Dingaling, self.classes.Address

        sess = fixture_session()

        class SubAddr(Address):
            pass

        self.mapper_registry.map_imperatively(
            SubAddr,
            inherits=Address,
            properties={"flub": relationship(Dingaling, viewonly=True)},
        )

        q = sess.query(SubAddr)
        opt = self._option_fixture(SubAddr.flub)

        self._assert_path_result(opt, q, [(SubAddr, "flub")])

    def test_from_base_to_base_attr_via_subclass(self):
        Dingaling, Address = self.classes.Dingaling, self.classes.Address

        sess = fixture_session()

        class SubAddr(Address):
            pass

        self.mapper_registry.map_imperatively(
            SubAddr,
            inherits=Address,
            properties={"flub": relationship(Dingaling, viewonly=True)},
        )

        q = sess.query(Address)
        opt = self._option_fixture(SubAddr.user)

        self._assert_path_result(
            opt, q, [(Address, inspect(Address).attrs.user)]
        )

    @emits_warning("This declarative base already contains a class")
    def test_of_type(self):
        User, Address = self.classes.User, self.classes.Address

        sess = fixture_session()

        class SubAddr(Address):
            pass

        self.mapper_registry.map_imperatively(SubAddr, inherits=Address)

        q = sess.query(User)
        opt = self._option_fixture(
            User.addresses.of_type(SubAddr), SubAddr.user
        )

        u_mapper = inspect(User)
        a_mapper = inspect(Address)
        self._assert_path_result(
            opt,
            q,
            [
                (u_mapper, u_mapper.attrs.addresses),
                (
                    u_mapper,
                    u_mapper.attrs.addresses,
                    a_mapper,
                    a_mapper.attrs.user,
                ),
            ],
        )

    @emits_warning("This declarative base already contains a class")
    def test_of_type_plus_level(self):
        Dingaling, User, Address = (
            self.classes.Dingaling,
            self.classes.User,
            self.classes.Address,
        )

        sess = fixture_session()

        class SubAddr(Address):
            pass

        self.mapper_registry.map_imperatively(
            SubAddr,
            inherits=Address,
            properties={"flub": relationship(Dingaling, viewonly=True)},
        )

        q = sess.query(User)
        opt = self._option_fixture(
            User.addresses.of_type(SubAddr), SubAddr.flub
        )

        u_mapper = inspect(User)
        sa_mapper = inspect(SubAddr)
        self._assert_path_result(
            opt,
            q,
            [
                (u_mapper, u_mapper.attrs.addresses),
                (
                    u_mapper,
                    u_mapper.attrs.addresses,
                    sa_mapper,
                    sa_mapper.attrs.flub,
                ),
            ],
        )

    def test_aliased_single(self):
        User = self.classes.User

        sess = fixture_session()
        ualias = aliased(User)
        q = sess.query(ualias)
        opt = self._option_fixture(ualias.addresses)
        self._assert_path_result(opt, q, [(inspect(ualias), "addresses")])

    def test_with_current_aliased_single(self):
        User, Address = self.classes.User, self.classes.Address

        sess = fixture_session()
        ualias = aliased(User)
        q = sess.query(Address)._with_current_path(
            self._make_path_registry([Address, "user"])
        )
        opt = self._option_fixture(
            Address.user.of_type(ualias), ualias.addresses
        )
        self._assert_path_result(opt, q, [(inspect(ualias), "addresses")])

    def test_with_current_aliased_single_nonmatching_option(self):
        User, Address = self.classes.User, self.classes.Address

        sess = fixture_session()
        ualias = aliased(User)
        q = sess.query(User)._with_current_path(
            self._make_path_registry([User, "addresses", Address, "user"])
        )
        opt = self._option_fixture(
            Address.user.of_type(ualias), ualias.addresses
        )
        self._assert_path_result(opt, q, [])

    def test_with_current_aliased_single_nonmatching_entity(self):
        User, Address = self.classes.User, self.classes.Address

        sess = fixture_session()
        ualias = aliased(User)
        q = sess.query(ualias)._with_current_path(
            # this was:
            # self._make_path_registry([Address, "user"])
            # .. which seems like an impossible "current_path"
            #
            # this one makes a little more sense
            self._make_path_registry([ualias, "addresses", Address, "user"])
        )
        opt = self._option_fixture(Address.user, User.addresses)
        self._assert_path_result(opt, q, [])

    def test_multi_entity_opt_on_second(self):
        Item = self.classes.Item
        Order = self.classes.Order
        opt = self._option_fixture(Order.items)
        sess = fixture_session()
        q = sess.query(Item, Order)
        self._assert_path_result(opt, q, [(Order, "items")])

    def test_path_exhausted(self):
        User = self.classes.User
        Item = self.classes.Item
        Order = self.classes.Order
        opt = self._option_fixture(User.orders)
        sess = fixture_session()
        q = sess.query(Item)._with_current_path(
            self._make_path_registry([User, "orders", Order, "items"])
        )
        self._assert_path_result(opt, q, [])

    def test_chained(self):
        User = self.classes.User
        Order = self.classes.Order
        sess = fixture_session()
        q = sess.query(User)
        opt = self._option_fixture(User.orders).joinedload(Order.items)
        self._assert_path_result(
            opt, q, [(User, "orders"), (User, "orders", Order, "items")]
        )

    def test_chained_plus_multi(self):
        User = self.classes.User
        Order = self.classes.Order
        Item = self.classes.Item
        sess = fixture_session()
        q = sess.query(User)
        opt = self._option_fixture(User.orders, Order.items).joinedload(
            Item.keywords
        )

        self._assert_path_result(
            opt,
            q,
            [
                (User, "orders"),
                (User, "orders", Order, "items"),
                (User, "orders", Order, "items", Item, "keywords"),
            ],
        )

class FromSubclassOptionsTest(PathTest, fixtures.DeclarativeMappedTest):
    # test for regression to #3963
    run_setup_mappers = "once"
    run_inserts = "once"
    run_deletes = None

    @classmethod
    def setup_mappers(cls):
        Base = cls.DeclarativeBasic

        class BaseCls(Base):
            __tablename__ = "basecls"
            id = Column(Integer, primary_key=True)

            type = Column(String(30))
            related_id = Column(ForeignKey("related.id"))
            related = relationship("Related")

        class SubClass(BaseCls):
            __tablename__ = "subcls"
            id = Column(ForeignKey("basecls.id"), primary_key=True)

        class Related(Base):
            __tablename__ = "related"
            id = Column(Integer, primary_key=True)

            sub_related_id = Column(ForeignKey("sub_related.id"))
            sub_related = relationship("SubRelated")

        class SubRelated(Base):
            __tablename__ = "sub_related"
            id = Column(Integer, primary_key=True)

    def test_with_current_nonmatching_entity_subclasses(self):
        BaseCls, SubClass, Related, SubRelated = self.classes(
            "BaseCls", "SubClass", "Related", "SubRelated"
        )
        sess = fixture_session()

        q = sess.query(Related)._with_current_path(
            self._make_path_registry([inspect(SubClass), "related"])
        )

        opt = subqueryload(SubClass.related).subqueryload(Related.sub_related)
        self._assert_path_result(opt, q, [(Related, "sub_related")])

class OptionsNoPropTest(_fixtures.FixtureTest):
    """test the error messages emitted when using property
    options in conjunction with column-only entities, or
    for not existing options

    """

    run_create_tables = False
    run_inserts = None
    run_deletes = None

    def test_option_with_mapper_PropCompatator(self):
        Item = self.classes.Item

        self._assert_option([Item], Item.keywords)

    def test_option_with_mapper_then_column_PropComparator(self):
        Item = self.classes.Item

        self._assert_option([Item, Item.id], Item.keywords)

    def test_option_with_column_then_mapper_PropComparator(self):
        Item = self.classes.Item

        self._assert_option([Item.id, Item], Item.keywords)

    def test_option_with_column_PropComparator(self):
        Item = self.classes.Item

        self._assert_eager_with_just_column_exception(
            Item.id,
            Item.keywords,
            r"Query has only expression-based entities; attribute loader "
            r"options for Mapper\[Item\(items\)\] can't be applied here.",
        )

    def test_option_against_nonexistent_PropComparator(self):
        Item = self.classes.Item
        Keyword = self.classes.Keyword
        self._assert_eager_with_entity_exception(
            [Keyword],
            (joinedload(Item.keywords),),
            r"Mapped class Mapper\[Item\(items\)\] does not apply to any of "
            "the root entities in this query, e.g. "
            r"Mapper\[Keyword\(keywords\)\]. "
            "Please specify the full path from one of "
            "the root entities to the target attribute. ",
        )

    def test_load_only_against_multi_entity_attr(self):
        User = self.classes.User
        Item = self.classes.Item
        self._assert_eager_with_entity_exception(
            [User, Item],
            lambda: (load_only(User.id, Item.id),),
            r"Can't apply wildcard \('\*'\) or load_only\(\) loader option "
            r"to multiple entities in the same option. Use separate options "
            "per entity.",
        )

    def test_col_option_against_relationship_attr(self):
        Item = self.classes.Item
        self._assert_loader_strategy_exception(
            [Item],
            lambda: (load_only(Item.keywords),),
            'Can\'t apply "column loader" strategy to property '
            '"Item.keywords", which is a "relationship"; this '
            'loader strategy is intended to be used with a "column property".',
        )

    def test_option_against_wrong_multi_entity_type_attr_one(self):
        Item = self.classes.Item
        Keyword = self.classes.Keyword
        self._assert_loader_strategy_exception(
            [Keyword, Item],
            lambda: (joinedload(Keyword.id).joinedload(Item.keywords),),
            'Can\'t apply "joined loader" strategy to property "Keyword.id", '
            'which is a "column property"; this loader strategy is intended '
            'to be used with a "relationship property".',
        )

    def test_option_against_wrong_multi_entity_type_attr_two(self):
        Item = self.classes.Item
        Keyword = self.classes.Keyword
        self._assert_loader_strategy_exception(
            [Keyword, Item],
            lambda: (joinedload(Keyword.keywords).joinedload(Item.keywords),),
            'Can\'t apply "joined loader" strategy to property '
            '"Keyword.keywords", which is a "mapped sql expression"; '
            "this loader "
            'strategy is intended to be used with a "relationship property".',
        )

    def test_option_against_wrong_multi_entity_type_attr_three(self):
        Item = self.classes.Item
        Keyword = self.classes.Keyword
        self._assert_eager_with_entity_exception(
            [Keyword.id, Item.id],
            lambda: (joinedload(Keyword.keywords),),
            r"Query has only expression-based entities; attribute loader "
            r"options for Mapper\[Keyword\(keywords\)\] can't be applied "
            "here.",
        )

    @testing.combinations(True, False, argnames="first_element")
    def test_wrong_type_in_option_cls(self, first_element):
        Item = self.classes.Item
        Keyword = self.classes.Keyword
        self._assert_eager_with_entity_exception(
            [Item],
            lambda: (joinedload(Keyword),)
            if first_element
            else (Load(Item).joinedload(Keyword),),
            "expected ORM mapped attribute for loader " "strategy argument",
        )

    @testing.combinations(
        (15,), (object(),), (type,), ({"foo": "bar"},), argnames="rando"
    )
    @testing.combinations(True, False, argnames="first_element")
    def test_wrong_type_in_option_any_random_type(self, rando, first_element):
        Item = self.classes.Item
        self._assert_eager_with_entity_exception(
            [Item],
            lambda: (joinedload(rando),)
            if first_element
            else (Load(Item).joinedload(rando)),
            "expected ORM mapped attribute for loader strategy argument",
        )

    @testing.combinations(True, False, argnames="first_element")
    def test_wrong_type_in_option_descriptor(self, first_element):
        OrderWProp = self.classes.OrderWProp

        self._assert_eager_with_entity_exception(
            [OrderWProp],
            lambda: (joinedload(OrderWProp.some_attr),)
            if first_element
            else (Load(OrderWProp).joinedload(OrderWProp.some_attr),),
            "expected ORM mapped attribute for loader strategy argument",
        )

    def test_non_contiguous_all_option(self):
        User = self.classes.User
        self._assert_eager_with_entity_exception(
            [User],
            lambda: (joinedload(User.addresses).joinedload(User.orders),),
            r'ORM mapped attribute "User.orders" does not link '
            r'from relationship "User.addresses"',
        )

    def test_non_contiguous_all_option_of_type(self):
        User = self.classes.User
        Order = self.classes.Order
        self._assert_eager_with_entity_exception(
            [User],
            lambda: (
                joinedload(User.addresses).joinedload(
                    User.orders.of_type(Order)
                ),
            ),
            r'ORM mapped attribute "User.orders" does not link '
            r'from relationship "User.addresses"',
        )

    @classmethod
    def setup_mappers(cls):
        users, User, addresses, Address, orders, Order = (
            cls.tables.users,
            cls.classes.User,
            cls.tables.addresses,
            cls.classes.Address,
            cls.tables.orders,
            cls.classes.Order,
        )
        cls.mapper_registry.map_imperatively(
            User,
            users,
            properties={
                "addresses": relationship(Address),
                "orders": relationship(Order),
            },
        )
        cls.mapper_registry.map_imperatively(Address, addresses)
        cls.mapper_registry.map_imperatively(Order, orders)
        keywords, items, item_keywords, Keyword, Item = (
            cls.tables.keywords,
            cls.tables.items,
            cls.tables.item_keywords,
            cls.classes.Keyword,
            cls.classes.Item,
        )
        cls.mapper_registry.map_imperatively(
            Keyword,
            keywords,
            properties={
                "keywords": column_property(keywords.c.name + "some keyword")
            },
        )
        cls.mapper_registry.map_imperatively(
            Item,
            items,
            properties=dict(
                keywords=relationship(Keyword, secondary=item_keywords)
            ),
        )

        class OrderWProp(cls.classes.Order):
            @property
            def some_attr(self):
                return "hi"

        cls.mapper_registry.map_imperatively(
            OrderWProp, None, inherits=cls.classes.Order
        )

    def _assert_option(self, entity_list, option):
        Item = self.classes.Item

        context = (
            fixture_session()
            .query(*entity_list)
            .options(joinedload(option))
            ._compile_state()
        )
        key = ("loader", (inspect(Item), inspect(Item).attrs.keywords))
        assert key in context.attributes

    def _assert_loader_strategy_exception(self, entity_list, options, message):
        sess = fixture_session()
        with expect_raises_message(orm_exc.LoaderStrategyException, message):
            # accommodate Load() objects that will raise
            # on construction
            if callable(options):
                options = options()

            # accommodate UnboundLoad objects that will raise
            # only when compile state is set up
            sess.query(*entity_list).options(*options)._compile_state()

    def _assert_eager_with_entity_exception(
        self, entity_list, options, message
    ):
        sess = fixture_session()
        with expect_raises_message(sa.exc.ArgumentError, message):
            # accommodate Load() objects that will raise
            # on construction
            if callable(options):
                options = options()

            # accommodate UnboundLoad objects that will raise
            # only when compile state is set up
            sess.query(*entity_list).options(*options)._compile_state()

    def _assert_eager_with_just_column_exception(
        self, column, eager_option, message
    ):
        assert_raises_message(
            sa.exc.ArgumentError,
            message,
            fixture_session()
            .query(column)
            .options(joinedload(eager_option))
            ._compile_state,
        )

class OptionsNoPropTestInh(_Polymorphic):
    def test_missing_attr_wpoly_subclasss(self):
        s = fixture_session()

        wp = with_polymorphic(Person, [Manager], flat=True)

        assert_raises_message(
            sa.exc.ArgumentError,
            r"Mapped class Mapper\[Manager\(managers\)\] does not apply to "
            "any of "
            r"the root entities in this query, e.g. "
            r"with_polymorphic\(Person, \[Manager\]\).",
            s.query(wp).options(load_only(Manager.status))._compile_state,
        )

    def test_missing_attr_of_type_subclass_one(self):
        s = fixture_session()

        e1 = with_polymorphic(Person, [Engineer])
        assert_raises_message(
            sa.exc.ArgumentError,
            r'ORM mapped attribute "Manager.manager_name" does not link from '
            r'relationship "Company.employees.'
            r'of_type\(with_polymorphic\(Person, \[Engineer\]\)\)".$',
            lambda: s.query(Company)
            .options(
                joinedload(Company.employees.of_type(e1)).load_only(
                    Manager.manager_name
                )
            )
            ._compile_state(),
        )

    def test_missing_attr_of_type_subclass_two(self):
        s = fixture_session()

        assert_raises_message(
            sa.exc.ArgumentError,
            r'ORM mapped attribute "Manager.manager_name" does not link from '
            r'relationship "Company.employees.'
            r'of_type\(Mapper\[Engineer\(engineers\)\]\)".$',
            lambda: s.query(Company)
            .options(
                joinedload(Company.employees.of_type(Engineer)).load_only(
                    Manager.manager_name
                )
            )
            ._compile_state(),
        )

    def test_missing_attr_of_type_subclass_name_matches(self):
        s = fixture_session()

        # the name "status" is present on Engineer also, make sure
        # that doesn't get mixed up here
        assert_raises_message(
            sa.exc.ArgumentError,
            r'ORM mapped attribute "Manager.status" does not link from '
            r'relationship "Company.employees.'
            r'of_type\(Mapper\[Engineer\(engineers\)\]\)".$',
            lambda: s.query(Company)
            .options(
                joinedload(Company.employees.of_type(Engineer)).load_only(
                    Manager.status
                )
            )
            ._compile_state(),
        )

    def test_missing_attr_of_type_wpoly_subclass(self):
        s = fixture_session()

        wp = with_polymorphic(Person, [Manager], flat=True)

        assert_raises_message(
            sa.exc.ArgumentError,
            r'ORM mapped attribute "Manager.manager_name" does not link from '
            r'relationship "Company.employees.'
            r'of_type\(with_polymorphic\(Person, \[Manager\]\)\)".$',
            lambda: s.query(Company)
            .options(
                joinedload(Company.employees.of_type(wp)).load_only(
                    Manager.manager_name
                )
            )
            ._compile_state(),
        )

    def test_missing_attr_is_missing_of_type_for_alias(self):
        s = fixture_session()

        pa = aliased(Person)

        assert_raises_message(
            sa.exc.ArgumentError,
            r'ORM mapped attribute "aliased\(Person\).name" does not link '
            r'from relationship "Company.employees".  Did you mean to use '
            r'"Company.employees.of_type\(aliased\(Person\)\)\"?',
            lambda: s.query(Company)
            .options(joinedload(Company.employees).load_only(pa.name))
            ._compile_state(),
        )

        q = s.query(Company).options(
            joinedload(Company.employees.of_type(pa)).load_only(pa.name)
        )
        orig_path = inspect(Company)._path_registry[
            Company.employees.property
        ][inspect(pa)][pa.name.property]
        key = ("loader", orig_path.natural_path)
        loader = q._compile_state().attributes[key]
        eq_(loader.path, orig_path)

class PickleTest(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table(
            "users",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("name", String(30), nullable=False),
        )
        Table(
            "addresses",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("user_id", None, ForeignKey("users.id")),
            Column("email_address", String(50), nullable=False),
        )

    @testing.fixture
    def user_address_fixture(self, registry):
        from sqlalchemy.testing.pickleable import User, Address

        registry.map_imperatively(
            User,
            self.tables.users,
            properties={"addresses": relationship(Address)},
        )
        registry.map_imperatively(Address, self.tables.addresses)

        return User, Address

    def test_slots(self, user_address_fixture):
        User, Address = user_address_fixture

        opt = joinedload(User.addresses)

        assert not hasattr(opt, "__dict__")
        assert not hasattr(opt.context[0], "__dict__")

    def test_pickle_relationship_loader(self, user_address_fixture):
        User, Address = user_address_fixture

        for i in range(3):
            opt = joinedload(User.addresses)

            q1 = fixture_session().query(User).options(opt)
            c1 = q1._compile_context()

            pickled = pickle.dumps(opt)

            opt2 = pickle.loads(pickled)

            q2 = fixture_session().query(User).options(opt2)
            c2 = q2._compile_context()

            eq_(c1.attributes, c2.attributes)

class LocalOptsTest(PathTest, QueryTest):
    @classmethod
    def setup_test_class(cls):
        def some_col_opt_only(self, key, opts):
            return self._set_column_strategy((key,), None, opts)

        strategy_options._AbstractLoad.some_col_opt_only = some_col_opt_only

        def some_col_opt_strategy(loadopt, key, opts):
            return loadopt._set_column_strategy(
                (key,), {"deferred": True, "instrument": True}, opts
            )

        strategy_options._AbstractLoad.some_col_opt_strategy = (
            some_col_opt_strategy
        )

    def _assert_attrs(self, opts, expected):
        User = self.classes.User

        s = fixture_session()
        q1 = s.query(User).options(*opts)
        attr = q1._compile_context().attributes

        key = (
            "loader",
            tuple(inspect(User)._path_registry[User.name.property]),
        )
        eq_(attr[key].local_opts, expected)

    def test_single_opt_only(self):
        User = self.classes.User

        opt = strategy_options.Load(User).some_col_opt_only(
            User.name, {"foo": "bar"}
        )
        self._assert_attrs([opt], {"foo": "bar"})

    def test_bound_multiple_opt_only(self):
        User = self.classes.User
        opts = [
            Load(User)
            .some_col_opt_only(User.name, {"foo": "bar"})
            .some_col_opt_only(User.name, {"bat": "hoho"})
        ]
        self._assert_attrs(opts, {"foo": "bar", "bat": "hoho"})

    def test_bound_strat_opt_recvs_from_optonly(self):
        User = self.classes.User
        opts = [
            Load(User)
            .some_col_opt_only(User.name, {"foo": "bar"})
            .some_col_opt_strategy(User.name, {"bat": "hoho"})
        ]
        self._assert_attrs(opts, {"foo": "bar", "bat": "hoho"})

    def test_bound_opt_only_adds_to_strat(self):
        User = self.classes.User
        opts = [
            Load(User)
            .some_col_opt_strategy(User.name, {"bat": "hoho"})
            .some_col_opt_only(User.name, {"foo": "bar"})
        ]
        self._assert_attrs(opts, {"foo": "bar", "bat": "hoho"})

class SubOptionsTest(PathTest, QueryTest):
    run_create_tables = False
    run_inserts = None
    run_deletes = None

    def _assert_opts(self, q, sub_opt, non_sub_opts):
        attr_a = {}

        q1 = q.options(sub_opt)._compile_context()
        q2 = q.options(*non_sub_opts)._compile_context()

        attr_a = {
            k: v
            for k, v in q1.attributes.items()
            if isinstance(k, tuple) and k[0] == "loader"
        }
        attr_b = {
            k: v
            for k, v in q2.attributes.items()
            if isinstance(k, tuple) and k[0] == "loader"
        }

        def strat_as_tuple(strat):
            return (
                strat.strategy,
                strat.local_opts,
                getattr(strat, "_of_type", None),
                strat.is_class_strategy,
                strat.is_opts_only,
            )

        eq_(
            {path: strat_as_tuple(load) for path, load in attr_a.items()},
            {path: strat_as_tuple(load) for path, load in attr_b.items()},
        )

    def test_one(self):
        User, Address, Order, Item, SubItem = self.classes(
            "User", "Address", "Order", "Item", "SubItem"
        )
        sub_opt = joinedload(User.orders).options(
            joinedload(Order.items).options(defer(Item.description)),
            defer(Order.description),
        )
        non_sub_opts = [
            joinedload(User.orders),
            defaultload(User.orders)
            .joinedload(Order.items)
            .defer(Item.description),
            defaultload(User.orders).defer(Order.description),
        ]

        sess = fixture_session()
        self._assert_opts(sess.query(User), sub_opt, non_sub_opts)

    def test_two(self):
        User, Address, Order, Item, SubItem = self.classes(
            "User", "Address", "Order", "Item", "SubItem"
        )

        sub_opt = defaultload(User.orders).options(
            joinedload(Order.items),
            defaultload(Order.items).options(subqueryload(Item.keywords)),
            defer(Order.description),
        )
        non_sub_opts = [
            defaultload(User.orders)
            .joinedload(Order.items)
            .subqueryload(Item.keywords),
            defaultload(User.orders).defer(Order.description),
        ]

        sess = fixture_session()
        self._assert_opts(sess.query(User), sub_opt, non_sub_opts)

    def test_three(self):
        User, Address, Order, Item, SubItem = self.classes(
            "User", "Address", "Order", "Item", "SubItem"
        )
        sub_opt = defaultload(User.orders).options(defer("*"))
        non_sub_opts = [defaultload(User.orders).defer("*")]
        sess = fixture_session()
        self._assert_opts(sess.query(User), sub_opt, non_sub_opts)

    def test_four(self):
        User, Address, Order, Item, SubItem, Keyword = self.classes(
            "User", "Address", "Order", "Item", "SubItem", "Keyword"
        )
        sub_opt = joinedload(User.orders).options(
            defer(Order.description),
            joinedload(Order.items).options(
                joinedload(Item.keywords).options(defer(Keyword.name)),
                defer(Item.description),
            ),
        )
        non_sub_opts = [
            joinedload(User.orders),
            defaultload(User.orders).defer(Order.description),
            defaultload(User.orders).joinedload(Order.items),
            defaultload(User.orders)
            .defaultload(Order.items)
            .joinedload(Item.keywords),
            defaultload(User.orders)
            .defaultload(Order.items)
            .defer(Item.description),
            defaultload(User.orders)
            .defaultload(Order.items)
            .defaultload(Item.keywords)
            .defer(Keyword.name),
        ]
        sess = fixture_session()
        self._assert_opts(sess.query(User), sub_opt, non_sub_opts)

    def test_five(self):
        User, Address, Order, Item, SubItem, Keyword = self.classes(
            "User", "Address", "Order", "Item", "SubItem", "Keyword"
        )
        sub_opt = joinedload(User.orders).options(load_only(Order.description))
        non_sub_opts = [
            joinedload(User.orders),
            defaultload(User.orders).load_only(Order.description),
        ]
        sess = fixture_session()
        self._assert_opts(sess.query(User), sub_opt, non_sub_opts)

    def test_invalid_one(self):
        User, Address, Order, Item, SubItem = self.classes(
            "User", "Address", "Order", "Item", "SubItem"
        )

        with expect_raises_message(
            sa.exc.ArgumentError,
            r'ORM mapped attribute "Item.keywords" does not link from '
            r'relationship "User.orders"',
        ):
            [
                joinedload(User.orders).joinedload(Item.keywords),
                defaultload(User.orders).joinedload(Order.items),
            ]
        with expect_raises_message(
            sa.exc.ArgumentError,
            r'Attribute "Item.keywords" does not link from '
            r'element "Mapper\[Order\(orders\)\]"',
        ):
            joinedload(User.orders).options(
                joinedload(Item.keywords), joinedload(Order.items)
            )

class MapperOptionsTest(_fixtures.FixtureTest):
    def test_synonym_options(self):
        Address, addresses, users, User = (
            self.classes.Address,
            self.tables.addresses,
            self.tables.users,
            self.classes.User,
        )

        self.mapper_registry.map_imperatively(
            User,
            users,
            properties=dict(
                addresses=relationship(
                    self.mapper_registry.map_imperatively(Address, addresses),
                    lazy="select",
                    order_by=addresses.c.id,
                ),
                adlist=synonym("addresses"),
            ),
        )

        def go():
            sess = fixture_session()
            u = (
                sess.query(User)
                .order_by(User.id)
                .options(sa.orm.joinedload(User.adlist))
                .filter_by(name="jack")
            ).one()
            eq_(u.adlist, [self.static.user_address_result[0].addresses[0]])

        self.assert_sql_count(testing.db, go, 1)

    def test_eager_options(self):
        """A lazy relationship can be upgraded to an eager relationship."""

        Address, addresses, users, User = (
            self.classes.Address,
            self.tables.addresses,
            self.tables.users,
            self.classes.User,
        )

        self.mapper_registry.map_imperatively(
            User,
            users,
            properties=dict(
                addresses=relationship(
                    self.mapper_registry.map_imperatively(Address, addresses),
                    order_by=addresses.c.id,
                )
            ),
        )

        sess = fixture_session()
        result = (
            sess.query(User)
            .order_by(User.id)
            .options(sa.orm.joinedload(User.addresses))
        ).all()

        def go():
            eq_(result, self.static.user_address_result)

        self.sql_count_(0, go)

    def test_eager_options_with_limit(self):
        Address, addresses, users, User = (
            self.classes.Address,
            self.tables.addresses,
            self.tables.users,
            self.classes.User,
        )

        self.mapper_registry.map_imperatively(
            User,
            users,
            properties=dict(
                addresses=relationship(
                    self.mapper_registry.map_imperatively(Address, addresses),
                    lazy="select",
                )
            ),
        )

        sess = fixture_session()
        u = (
            sess.query(User)
            .options(sa.orm.joinedload(User.addresses))
            .filter_by(id=8)
        ).one()

        def go():
            eq_(u.id, 8)
            eq_(len(u.addresses), 3)

        self.sql_count_(0, go)

        sess.expunge_all()

        u = sess.query(User).filter_by(id=8).one()
        eq_(u.id, 8)
        eq_(len(u.addresses), 3)

    def test_lazy_options_with_limit(self):
        Address, addresses, users, User = (
            self.classes.Address,
            self.tables.addresses,
            self.tables.users,
            self.classes.User,
        )

        self.mapper_registry.map_imperatively(
            User,
            users,
            properties=dict(
                addresses=relationship(
                    self.mapper_registry.map_imperatively(Address, addresses),
                    lazy="joined",
                )
            ),
        )

        sess = fixture_session()
        u = (
            sess.query(User)
            .options(sa.orm.lazyload(User.addresses))
            .filter_by(id=8)
        ).one()

        def go():
            eq_(u.id, 8)
            eq_(len(u.addresses), 3)

        self.sql_count_(1, go)

    def test_eager_degrade(self):
        """An eager relationship automatically degrades to a lazy relationship
        if eager columns are not available"""

        Address, addresses, users, User = (
            self.classes.Address,
            self.tables.addresses,
            self.tables.users,
            self.classes.User,
        )

        self.mapper_registry.map_imperatively(
            User,
            users,
            properties=dict(
                addresses=relationship(
                    self.mapper_registry.map_imperatively(Address, addresses),
                    lazy="joined",
                    order_by=addresses.c.id,
                )
            ),
        )

        sess = fixture_session()
        # first test straight eager load, 1 statement

        def go():
            result = sess.query(User).order_by(User.id).all()
            eq_(result, self.static.user_address_result)

        self.sql_count_(1, go)

        sess.expunge_all()

        # then select just from users.  run it into instances.
        # then assert the data, which will launch 3 more lazy loads
        # (previous users in session fell out of scope and were removed from
        # session's identity map)
        r = sess.connection().execute(users.select().order_by(users.c.id))

        ctx = sess.query(User)._compile_context()

        def go():
            result = loading.instances(r, ctx).scalars().unique()
            result = list(result)
            eq_(result, self.static.user_address_result)

        self.sql_count_(4, go)

    def test_eager_degrade_deep(self):
        (
            users,
            Keyword,
            items,
            order_items,
            orders,
            Item,
            User,
            Address,
            keywords,
            item_keywords,
            Order,
            addresses,
        ) = (
            self.tables.users,
            self.classes.Keyword,
            self.tables.items,
            self.tables.order_items,
            self.tables.orders,
            self.classes.Item,
            self.classes.User,
            self.classes.Address,
            self.tables.keywords,
            self.tables.item_keywords,
            self.classes.Order,
            self.tables.addresses,
        )

        # test with a deeper set of eager loads.  when we first load the three
        # users, they will have no addresses or orders.  the number of lazy
        # loads when traversing the whole thing will be three for the
        # addresses and three for the orders.
        self.mapper_registry.map_imperatively(Address, addresses)

        self.mapper_registry.map_imperatively(Keyword, keywords)

        self.mapper_registry.map_imperatively(
            Item,
            items,
            properties=dict(
                keywords=relationship(
                    Keyword,
                    secondary=item_keywords,
                    lazy="joined",
                    order_by=item_keywords.c.keyword_id,
                )
            ),
        )

        self.mapper_registry.map_imperatively(
            Order,
            orders,
            properties=dict(
                items=relationship(
                    Item,
                    secondary=order_items,
                    lazy="joined",
                    order_by=order_items.c.item_id,
                )
            ),
        )

        self.mapper_registry.map_imperatively(
            User,
            users,
            properties=dict(
                addresses=relationship(
                    Address, lazy="joined", order_by=addresses.c.id
                ),
                orders=relationship(
                    Order, lazy="joined", order_by=orders.c.id
                ),
            ),
        )

        sess = fixture_session()

        # first test straight eager load, 1 statement
        def go():
            result = sess.query(User).order_by(User.id).all()
            eq_(result, self.static.user_all_result)

        self.assert_sql_count(testing.db, go, 1)

        sess.expunge_all()

        # then select just from users.  run it into instances.
        # then assert the data, which will launch 6 more lazy loads
        r = sess.connection().execute(users.select())

        ctx = sess.query(User)._compile_context()

        def go():
            result = loading.instances(r, ctx).scalars().unique()
            result = list(result)
            eq_(result, self.static.user_all_result)

        self.assert_sql_count(testing.db, go, 6)

    def test_lazy_options(self):
        """An eager relationship can be upgraded to a lazy relationship."""

        Address, addresses, users, User = (
            self.classes.Address,
            self.tables.addresses,
            self.tables.users,
            self.classes.User,
        )

        self.mapper_registry.map_imperatively(
            User,
            users,
            properties=dict(
                addresses=relationship(
                    self.mapper_registry.map_imperatively(Address, addresses),
                    lazy="joined",
                )
            ),
        )

        sess = fixture_session()
        result = (
            sess.query(User)
            .order_by(User.id)
            .options(sa.orm.lazyload(User.addresses))
        ).all()

        def go():
            eq_(result, self.static.user_address_result)

        self.sql_count_(4, go)

    def test_option_propagate(self):
        users, items, order_items, Order, Item, User, orders = (
            self.tables.users,
            self.tables.items,
            self.tables.order_items,
            self.classes.Order,
            self.classes.Item,
            self.classes.User,
            self.tables.orders,
        )

        self.mapper_registry.map_imperatively(
            User, users, properties=dict(orders=relationship(Order))
        )
        self.mapper_registry.map_imperatively(
            Order,
            orders,
            properties=dict(items=relationship(Item, secondary=order_items)),
        )
        self.mapper_registry.map_imperatively(Item, items)

        sess = fixture_session()

        oalias = aliased(Order)

        # this one is *really weird*
        # here's what the test originally had.  note two different strategies
        # for Order.items
        #
        # opt1 = sa.orm.joinedload(User.orders, Order.items)
        # opt2 = sa.orm.contains_eager(User.orders, Order.items, alias=oalias)

        # here's how it would translate.  note that the second
        # contains_eager() for Order.items just got cancelled out,
        # I guess the joinedload() would somehow overrule the contains_eager
        #
        # opt1 = Load(User).defaultload(User.orders).joinedload(Order.items)
        # opt2 = Load(User).contains_eager(User.orders, alias=oalias)

        # setting up the options more specifically works however with
        # both the old way and the new way
        opt1 = sa.orm.joinedload(User.orders, Order.items)
        opt2 = sa.orm.contains_eager(User.orders, alias=oalias)

        u1 = (
            sess.query(User)
            .join(oalias, User.orders)
            .options(opt1, opt2)
            .first()
        )
        ustate = attributes.instance_state(u1)
        assert opt1 in ustate.load_options
        assert opt2 not in ustate.load_options

    @testing.combinations(
        (
            lambda User, Order: (
                joinedload(User.orders),
                contains_eager(User.orders),
            ),
            r"Loader strategies for ORM Path\[Mapper\[User\(users\)\] -> "
            r"User.orders -> Mapper\[Order\(orders\)\]\] conflict",
        ),
        (
            lambda User, Order: (
                joinedload(User.orders),
                joinedload(User.orders).joinedload(Order.items),
            ),
            None,
        ),
        (
            lambda User, Order: (
                joinedload(User.orders),
                joinedload(User.orders, innerjoin=True).joinedload(
                    Order.items
                ),
            ),
            r"Loader strategies for ORM Path\[Mapper\[User\(users\)\] -> "
            r"User.orders -> Mapper\[Order\(orders\)\]\] conflict",
        ),
        (
            lambda User: (defer(User.name), undefer(User.name)),
            r"Loader strategies for ORM Path\[Mapper\[User\(users\)\] -> "
            r"User.name\] conflict",
        ),
    )
    def test_conflicts(self, make_opt, errmsg):
        """introduce a new error for conflicting options in SQLAlchemy 2.0.

        This case seems to be fairly difficult to come up with randomly
        so let's see if we can refuse to guess for this case.

        """
        users, items, order_items, Order, Item, User, orders = (
            self.tables.users,
            self.tables.items,
            self.tables.order_items,
            self.classes.Order,
            self.classes.Item,
            self.classes.User,
            self.tables.orders,
        )

        self.mapper_registry.map_imperatively(
            User, users, properties=dict(orders=relationship(Order))
        )
        self.mapper_registry.map_imperatively(
            Order,
            orders,
            properties=dict(items=relationship(Item, secondary=order_items)),
        )
        self.mapper_registry.map_imperatively(Item, items)

        sess = fixture_session()

        opt = testing.resolve_lambda(
            make_opt, User=User, Order=Order, Item=Item
        )

        if errmsg:
            with expect_raises_message(sa.exc.InvalidRequestError, errmsg):
                sess.query(User).options(opt)._compile_context()
        else:
            sess.query(User).options(opt)._compile_context()


text:
package net.parallaxed.bluejam;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

import ec.util.MersenneTwisterFast;

import net.parallaxed.bluejam.evolution.IndividualSelector;
import net.parallaxed.bluejam.evolution.TreeBreeder;
import net.parallaxed.bluejam.exceptions.ErrorFeedback;
import net.parallaxed.bluejam.exceptions.IndividualAddException;
import net.parallaxed.bluejam.playback.Listener;
import net.parallaxed.bluejam.playback.Player;

/**
 * The evolve class carries out the main evolution cycles and
 * outputs "winning" musical candidates by notifying listeners. Instances
 * of this object are meant to be run in their own thread.
 *
 * As the generations are run, registered listeners will receive
 * references to NoteSequences. Feedback can be given on these
 * sequences by calling the feedback() method with a score
 * and a reference back to the NoteSequence that achieved that score.
 * In the case of positive feedback, the system can choose to
 * make that candidate and other high-scoring candidates from the
 * population "elite", which means they get passed in to
 * subsequent generations.
 *
 * This class implements the Player interface, a primitive events
 * pattern.
 *
 * @see Listener
 * @see Player
 *
 * @author Ciarán Rowe (csr2@kent.ac.uk)
 *
 */
public class Evolve implements Runnable, Player
{
	private static final MersenneTwisterFast _mt = net.parallaxed.bluejam.util.MersenneTwisterFast.getInstance();
	private HashMap<NoteSequence, Population> played = new HashMap<NoteSequence, Population>();
	private int playedMemorySize = 10;

	private ArrayList <Listener> listeners = new ArrayList<Listener>();
	private ArrayList <NoteSequence> elites = new ArrayList<NoteSequence>();
	private int maxElites = 10;

	private SequenceParameters  _sequenceParameters = null;
	private HeuristicCollection _heuristics = null;
	private Population population = null;

	public boolean running = true;
	private volatile boolean waiting = false;
	/**
	 * This is the maximum number of outputs we make before we
	 * wait on feedback from listeners.
	 * TODO Optimize for multiple listeners.
	 */
	private int listenBufferSize = 5;

	/**
	 * Keep our track of the properties we need to update in the next
	 * generation.
	 */
	private int _memberCount = 50;

	protected int generations = 25;
	/**
	 * The number of generations we're going to run.
	 */
	public int generations() { return generations; }
	/**
	 * Sets the number of generations. Limit: 0 < x < 100
	 * @param numberOfGenerations The number of generations to complete
	 */
	public void generations(int numberOfGenerations)
	{
		if (generations > 0 && generations < 200)
			generations = numberOfGenerations;
	}

	// Increased from 8 to halve selection pressure.
	private int _matingPoolSize = 15;

	/**
	 * Sets the size of the mating pool. Limit: 1 < x < 21
	 * @param matingPoolSize The new size of the pool
	 */
	public void matingPoolSize(int matingPoolSize) {
		if (matingPoolSize >= 2 && matingPoolSize <= 20)
			_matingPoolSize = matingPoolSize;
	}
	private int _maxBreedCycles = 10;

	private double _crossoverProbability = 0.9;

	/**
	 * Adds a listener to this player.
	 */
	public void addListener(Listener listener)
	{
		listeners.add(listener);
	}

	/**
	 * Accepts a positive or negative feedback value (usually
	 * -1 or 0 or 1), and turns on elitism for that NoteSequence
	 * making it appear in subsequent generations.
	 */
	public void feedback(int feedback, NoteSequence notes)
	{
		// Should be garbaged later.

		// Hold on to this one.
		if (feedback == 1) {
			System.out.println("Recieved positive feedback! Enabling elitism for "+notes.toString());
			if (elites.size() >= maxElites)
				elites.remove(0);
			if (!elites.contains(notes))
				elites.add(notes);
		}

		listenBufferSize++;

		/**
		 * Should restart the sleeping evolution thread running this
		 * object if there is one.
		 */
		synchronized (this) {
			waiting = false;
			notify();
		}
		played.remove(notes);
	}

	/**
	 * Pauses the evolution from outside this thread.
	 */
	public void togglePause() {
		if (waiting)
			synchronized (this) {
				waiting = false;
				System.out.print("\nPausing...");
				notify();
			}
		else
			waiting = true;
	}

	public PopulationParameters getPopulationParameters() {
		return population.getParameters();
	}

	/**
	 * Runs the main evolution thread.
	 */
	public void run() {
		if (population == null)
			throw new IllegalArgumentException("No population found");
		/*
		 * Initialize everything, then run evoluton cycles.
		 * Run evolution cycle.
		 */
		try {

			population.initialize();
			Class<?> selection = population.getParameters().getSelectionType().eval();
			Method _cons = selection.getMethod("getInstance", new Class[] {});
			IndividualSelector _indS =  (IndividualSelector) _cons.invoke(null, new Object[] {});

			// TODO Associate this with the genotype
			while(running && generations-- > 0) {
				//populations.add(population);
				if (waiting || listenBufferSize == 0)
				{
					try {
						synchronized (this) {
							waiting = true;
							System.out.println("Evolution waiting for listener...");
							wait();
							// Double check we've not been awakened prematurely from togglePause();
							if(listenBufferSize == 0) {
								System.out.println("Resumed.");
								continue;
							}
						}
					}
					catch (InterruptedException e) { ErrorFeedback.handle("Interrupted.", e); }
				}

				// TODO Make parameters generic.
				Population p = _indS.select(population, _matingPoolSize);

				// QUIRKY - Almost 2/3 of the time we select the fittest.
				Individual winner = (_mt.nextInt(22) < 14) ? p.getFittestIndividual() : p.getRandomIndividual();
				NoteSequence play = winner.getNoteSequence();

				// Remove one if we're over budget on retention.
				if (played.size() > playedMemorySize)
					played.remove(played.keySet().iterator().next());

				/*
				 * Record this, and play it to the listeners.
				 */
				played.put(play, population);
				for (Listener l : listeners)
					l.listen(play);

				// Decrement the listen buffer.
				listenBufferSize--;
				try {
					// Re-insert heuristic individuals before breeding.
					if (_heuristics != null)
						for (int i = 0; i < _heuristics.size(); i++) {
							Individual h = new Individual(p, _heuristics.get(i));
							h.initialize();
							p.addIndividual(h);
						}

					// Re-insert elites.
					if (elites.size() > 0)
						for (NoteSequence n : elites)
						{
							Individual elite = null;
							// This encourages the individual to evolve.
							if (n instanceof Heuristic)
								elite = new Individual(n,(Heuristic)n);
							else
								elite = new Individual(n,(Heuristic) null);
							p.addIndividual(elite);
						}
				}
				catch (IndividualAddException e) {
					ErrorFeedback.handle(e);
				}

				p.memberCount = _memberCount;
				TreeBreeder pool = new TreeBreeder(_maxBreedCycles,_crossoverProbability);
				pool.breed(p);

				population = p;
				System.gc();
			}
		}
		catch (Exception e) {
			ErrorFeedback.handle(e.getMessage(), e);
		}
		for (Listener l : listeners)
			l.listen(null);
	}

	public void setPopulationCount(int memberCount)
	{
		if (memberCount >= 25 &&  memberCount <= 250)
			_memberCount = memberCount;
	}
	/**
	 * Creates an instance of the Evolve class
	 * @see SequenceParameters
	 * @param sequenceParameters The sequenceParameters to use.
	 */
	public Evolve(SequenceParameters sequenceParameters, int populationCount) {
		this(sequenceParameters, populationCount, null);
	}

	/**
	 * Creates an instance of the Evolve class.
	 * @see SequenceParameters
	 * @see HeuristicCollection
	 * @param sequenceParameters The sequenceParameters to use.
	 * @param heuristics The HeuristicCollection to use.
	 */
	public Evolve(SequenceParameters sequenceParameters, int populationCount, HeuristicCollection heuristics)
	{
		if (sequenceParameters == null)
			throw new IllegalArgumentException("SequenceParameters must not be null.");
		_sequenceParameters = sequenceParameters;
		_heuristics = heuristics;
		population = new Population(_sequenceParameters,populationCount,_heuristics);
	}
}

redpajama_set_name: RedPajamaGithub


text:
package org.zaproxy.addon.network.internal;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;

/* Unit test for {@link TlsUtils}. */
class TlsUtilsUnitTest {

    @Test
    void shouldHaveSupportedTlsProtocols() {
        assertThat(TlsUtils.getSupportedTlsProtocols(), is(not(empty())));
    }

    @Test
    void shouldFilterUnsupportedTlsProtocols() {
        // Given
        List<String> protocols = Arrays.asList("Unknown A", TlsUtils.TLS_V1_2, "Unknown B", null);
        // When
        List<String> filteredProtocols = TlsUtils.filterUnsupportedTlsProtocols(protocols);
        // Then
        assertThat(filteredProtocols, contains(TlsUtils.TLS_V1_2));
    }

    @ParameterizedTest
    @NullAndEmptySource
    void shouldThrowIfNoTlsProtocols(List<String> protocols) {
        assertThrows(
                IllegalArgumentException.class,
                () -> TlsUtils.filterUnsupportedTlsProtocols(protocols));
    }

    @Test
    void shouldThrowIfNoTlsProtocolsAfterFilter() {
        // Given
        List<String> protocols = Arrays.asList("Unknown A");
        // When / Then
        assertThrows(
                IllegalArgumentException.class,
                () -> TlsUtils.filterUnsupportedTlsProtocols(protocols));
    }

    @Test
    void shouldThrowIfInvalidTlsConfiguration() {
        // Given
        List<String> protocols = Arrays.asList(TlsUtils.SSL_V2_HELLO);
        // When / Then
        assertThrows(
                IllegalArgumentException.class,
                () -> TlsUtils.filterUnsupportedTlsProtocols(protocols));
    }

    @Test
    void shouldHaveSupportedApplicationProtocols() {
        assertThat(TlsUtils.getSupportedApplicationProtocols(), is(not(empty())));
    }

    @Test
    void shouldFilterUnsupportedApplicationProtocols() {
        // Given
        List<String> protocols =
                Arrays.asList(
                        "Unknown A",
                        TlsUtils.APPLICATION_PROTOCOL_HTTP_1_1,
                        TlsUtils.APPLICATION_PROTOCOL_HTTP_2,
                        "Unknown B",
                        null);
        // When
        List<String> filteredApplicationProtocols =
                TlsUtils.filterUnsupportedApplicationProtocols(protocols);
        // Then
        assertThat(
                filteredApplicationProtocols,
                contains(
                        TlsUtils.APPLICATION_PROTOCOL_HTTP_1_1,
                        TlsUtils.APPLICATION_PROTOCOL_HTTP_2));
    }

    @ParameterizedTest
    @NullAndEmptySource
    void shouldThrowIfNoApplicationProtocols(List<String> protocols) {
        assertThrows(
                IllegalArgumentException.class,
                () -> TlsUtils.filterUnsupportedApplicationProtocols(protocols));
    }

    @Test
    void shouldThrowIfNoApplicationProtocolsAfterFilter() {
        // Given
        List<String> protocols = Arrays.asList("Unknown A");
        // When / Then
        assertThrows(
                IllegalArgumentException.class,
                () -> TlsUtils.filterUnsupportedApplicationProtocols(protocols));
    }
}


text:
'use strict';

/**
 * Module dependencies.
 */
var _ = require('lodash'),
	gruntFile = require('grunt').file,
	bowerFiles = require('main-bower-files'),
	path = require('path'),
	fs = require('fs');

var exists = require('path-exists').sync;

var minBowerFiles = function(type){
    return bowerFiles(type).map( function(path, index, arr) {
      var newPath = path.replace(/.([^.]+)$/g, '.min.$1');
      return exists( newPath ) ? newPath : path;
    });
};
/**
 * Load app configurations
 */
 var exports = _.extend(
		require('./env/all'),
		require('./env/' + process.env.NODE_ENV) || {}
	);

//Load keys from api_keys.js if file exists
if( fs.existsSync('./config/env/api_keys.js') ){
	module.exports = _.extend(
		exports,
		require('./env/api_keys')
	);
} else {
	module.exports = exports;
}

/**
 * Get files by glob patterns
 */
module.exports.getGlobbedFiles = function(globPatterns, removeRoot, addRoot) {

	var files = gruntFile.expand(globPatterns);
	if (removeRoot) {
		files = files.map(function(file) {
			if(addRoot) return file.replace(removeRoot, addRoot);
			return file.replace(removeRoot, '');
		});
	}

	return files;
};

module.exports.removeRootDir = function(files, removeRoot, addRoot) {
	return files.map(function(file) {
		if (addRoot) return file.replace(path.join(process.cwd(), removeRoot), addRoot);
		return file.replace(path.join(process.cwd(), removeRoot), '');
	});
};

/**
 * Get the app's bower dependencies
 */
module.exports.getBowerJSAssets = function() {
	return this.removeRootDir(minBowerFiles('**/**.js'), 'public/', 'static/');
};
module.exports.getBowerCSSAssets = function() {
	return this.removeRootDir(minBowerFiles('**/**.css'), 'public/', 'static/');
};
module.exports.getBowerOtherAssets = function() {
	return this.removeRootDir(minBowerFiles('**/!(*.js|*.css|*.less)'), 'public/', 'static/');
};

/**
 * Helper Function for getJavascriptAssets and getFormJavaScriptAssets
 */
module.exports._getAssets = function(includeTests, isFormJS){
	var unit_tests, js_assets;

	if(isFormJS) {
		js_assets = this.assets.form_js;
		unit_tests = this.assets.form_unit_tests;
	} else {
		js_assets = this.assets.js;
		unit_tests = this.assets.unit_tests;
	}

	var output = this.getGlobbedFiles(js_assets, 'public/', 'static/');

	// To include tests
	if (includeTests) {
		output = _.union(output, this.getGlobbedFiles(unit_tests));
	}

	return output;
};

/**
 * Get the modules JavaScript files
 */
module.exports.getJavaScriptAssets = function(includeTests) {
	return this._getAssets(includeTests, false);
};

/**
 * Get the modules Form JavaScript files
 */
module.exports.getFormJavaScriptAssets = function(includeTests) {
	return this._getAssets(includeTests, true);
};

/**
 * Get the modules CSS files
 */
module.exports.getCSSAssets = function() {
	var output = this.getGlobbedFiles(this.assets.css, 'public/', 'static/');
	return output;
};


text:
package model

import (
	"time"
)

//ReportSummary report summary
type ReportSummary struct {
	ID                  int       `json:"id" gorm:"AUTO_INCREMENT;primary_key;" form:"id"`
	ScriptID            int       `json:"script_id" form:"script_id"`
	ScriptSnapID        int       `json:"script_snap_id" form:"script_snap_id"`
	ExecuteID           string    `json:"execute_id" form:"execute_id"`
	Department          string    `json:"department" form:"department"`
	Project             string    `json:"project" form:"project"`
	APP                 string    `json:"app" form:"app"`
	TestName            string    `json:"test_name" form:"test_name" param:"test_name"`
	TestNameNick        string    `json:"test_name_nick" form:"test_name_nick"`
	JobName             string    `json:"job_name" form:"job_name"`
	Count               int       `json:"count"`
	QPS                 int       `json:"qps"`
	AvgTime             int       `json:"avg_time"`
	Min                 int       `json:"min"`
	Max                 int       `json:"max"`
	Error               int       `json:"error"`
	FailPercent         string    `json:"fail_percent"`
	NinetyTime          int       `json:"ninety_time"`
	NinetyFiveTime      int       `json:"ninety_five_time"`
	NinetyNineTime      int       `json:"ninety_nine_time"`
	NetIo               int       `json:"net_io"`
	ElapsdTime          int       `json:"elapsd_time"`
	TestStatus          int       `json:"test_status"`
	UserName            string    `json:"user_name" form:"user_name"`
	ResJtl              string    `json:"res_jtl"`
	JmeterLog           string    `json:"jmeter_log"`
	DockerSum           int       `json:"docker_sum"`
	Ctime               time.Time `json:"ctime"`
	Mtime               time.Time `json:"mtime"`
	Debug               int       `json:"debug"`
	Active              int       `json:"active" form:"active"`
	SceneID             int       `json:"scene_id" form:"scene_id"`
	Type                int       `json:"type" form:"type"` // 0.http单接口  1.grpc报告  2.场景报告  3.全链路
	LoadTime            int       `json:"load_time"`        //执行时间
	FiftyTime           int       `json:"fifty_time"`
	IsFusing            bool      `json:"is_fusing"`             //是否熔断
	FusingTestName      string    `json:"fusing_test_name"`      //被熔断接口
	SuccessCodeRate     int       `json:"success_code_rate"`     //熔断时接口的httpcode
	SuccessBusinessRate int       `json:"success_business_rate"` //熔断时接口的成功率
	FusingValue         int       `json:"fusing_value"`          //熔断阈值
	BusinessValue       int       `json:"business_value"`        //业务熔断阈值
	UseBusinessStop     bool      `json:"use_business_stop"`     //是否使用业务熔断
}

//QueryReportSuRequest query report summary request
type QueryReportSuRequest struct {
	ReportSummary
	//Script
	Pagination
	Executor  string `json:"executor" form:"executor"`
	SearchAll bool   `json:"search_all" form:"search_all"`
}

//QueryReportSuResponse query report summary response
type QueryReportSuResponse struct {
	ReportSummarys []*ReportLabels `json:"reports"`
	Pagination
}

//ReportLabels report labels
type ReportLabels struct {
	ReportSummary
	Labels []*LabelRelation `json:"labels"`
}

//TableName tablename
func (r ReportSummary) TableName() string {
	return "report_summary"
}


text:
package org.apache.beam.runners.flink;

import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Map;
import org.apache.beam.runners.core.SplittableParDo;
import org.apache.beam.runners.core.construction.PTransformMatchers;
import org.apache.beam.runners.core.construction.PTransformReplacements;
import org.apache.beam.runners.core.construction.ReplacementOutputs;
import org.apache.beam.runners.core.construction.SingleInputOutputOverrideFactory;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.runners.PTransformOverride;
import org.apache.beam.sdk.runners.PTransformOverrideFactory;
import org.apache.beam.sdk.runners.TransformHierarchy;
import org.apache.beam.sdk.transforms.AppliedPTransform;
import org.apache.beam.sdk.transforms.Combine;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.ParDo.MultiOutput;
import org.apache.beam.sdk.transforms.View;
import org.apache.beam.sdk.util.InstanceBuilder;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.PCollectionTuple;
import org.apache.beam.sdk.values.PValue;
import org.apache.beam.sdk.values.TupleTag;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This is a {@link FlinkPipelineTranslator} for streaming jobs. Its role is to translate
 * the user-provided {@link org.apache.beam.sdk.values.PCollection}-based job into a
 * {@link org.apache.flink.streaming.api.datastream.DataStream} one.
 *
 */
class FlinkStreamingPipelineTranslator extends FlinkPipelineTranslator {

  private static final Logger LOG = LoggerFactory.getLogger(FlinkStreamingPipelineTranslator.class);

  /** The necessary context in the case of a straming job. */
  private final FlinkStreamingTranslationContext streamingContext;

  private int depth = 0;

  private FlinkRunner flinkRunner;

  public FlinkStreamingPipelineTranslator(
      FlinkRunner flinkRunner,
      StreamExecutionEnvironment env,
      PipelineOptions options) {
    this.streamingContext = new FlinkStreamingTranslationContext(env, options);
    this.flinkRunner = flinkRunner;
  }

  @Override
  public void translate(Pipeline pipeline) {
    List<PTransformOverride> transformOverrides =
        ImmutableList.<PTransformOverride>builder()
            .add(
                PTransformOverride.of(
                    PTransformMatchers.splittableParDoMulti(),
                    new SplittableParDoOverrideFactory()))
            .add(
                PTransformOverride.of(
                    PTransformMatchers.classEqualTo(View.AsIterable.class),
                    new ReflectiveOneToOneOverrideFactory(
                        FlinkStreamingViewOverrides.StreamingViewAsIterable.class, flinkRunner)))
            .add(
                PTransformOverride.of(
                    PTransformMatchers.classEqualTo(View.AsList.class),
                    new ReflectiveOneToOneOverrideFactory(
                        FlinkStreamingViewOverrides.StreamingViewAsList.class, flinkRunner)))
            .add(
                PTransformOverride.of(
                    PTransformMatchers.classEqualTo(View.AsMap.class),
                    new ReflectiveOneToOneOverrideFactory(
                        FlinkStreamingViewOverrides.StreamingViewAsMap.class, flinkRunner)))
            .add(
                PTransformOverride.of(
                    PTransformMatchers.classEqualTo(View.AsMultimap.class),
                    new ReflectiveOneToOneOverrideFactory(
                        FlinkStreamingViewOverrides.StreamingViewAsMultimap.class, flinkRunner)))
            .add(
                PTransformOverride.of(
                    PTransformMatchers.classEqualTo(View.AsSingleton.class),
                    new ReflectiveOneToOneOverrideFactory(
                        FlinkStreamingViewOverrides.StreamingViewAsSingleton.class, flinkRunner)))
            // this has to be last since the ViewAsSingleton override
            // can expand to a Combine.GloballyAsSingletonView
            .add(
                PTransformOverride.of(
                    PTransformMatchers.classEqualTo(Combine.GloballyAsSingletonView.class),
                    new ReflectiveOneToOneOverrideFactory(
                        FlinkStreamingViewOverrides.StreamingCombineGloballyAsSingletonView.class,
                        flinkRunner)))
            .build();

    pipeline.replaceAll(transformOverrides);
    super.translate(pipeline);
  }

  // --------------------------------------------------------------------------------------------
  //  Pipeline Visitor Methods
  // --------------------------------------------------------------------------------------------

  @Override
  public CompositeBehavior enterCompositeTransform(TransformHierarchy.Node node) {
    LOG.info("{} enterCompositeTransform- {}", genSpaces(this.depth), node.getFullName());
    this.depth++;

    PTransform<?, ?> transform = node.getTransform();
    if (transform != null) {
      StreamTransformTranslator<?> translator =
          FlinkStreamingTransformTranslators.getTranslator(transform);

      if (translator != null && applyCanTranslate(transform, node, translator)) {
        applyStreamingTransform(transform, node, translator);
        LOG.info("{} translated- {}", genSpaces(this.depth), node.getFullName());
        return CompositeBehavior.DO_NOT_ENTER_TRANSFORM;
      }
    }
    return CompositeBehavior.ENTER_TRANSFORM;
  }

  @Override
  public void leaveCompositeTransform(TransformHierarchy.Node node) {
    this.depth--;
    LOG.info("{} leaveCompositeTransform- {}", genSpaces(this.depth), node.getFullName());
  }

  @Override
  public void visitPrimitiveTransform(TransformHierarchy.Node node) {
    LOG.info("{} visitPrimitiveTransform- {}", genSpaces(this.depth), node.getFullName());
    // get the transformation corresponding to hte node we are
    // currently visiting and translate it into its Flink alternative.

    PTransform<?, ?> transform = node.getTransform();
    StreamTransformTranslator<?> translator =
        FlinkStreamingTransformTranslators.getTranslator(transform);

    if (translator == null || !applyCanTranslate(transform, node, translator)) {
      LOG.info(node.getTransform().getClass().toString());
      throw new UnsupportedOperationException(
          "The transform " + transform + " is currently not supported.");
    }
    applyStreamingTransform(transform, node, translator);
  }

  @Override
  public void visitValue(PValue value, TransformHierarchy.Node producer) {
    // do nothing here
  }

  private <T extends PTransform<?, ?>> void applyStreamingTransform(
      PTransform<?, ?> transform,
      TransformHierarchy.Node node,
      StreamTransformTranslator<?> translator) {

    @SuppressWarnings("unchecked")
    T typedTransform = (T) transform;

    @SuppressWarnings("unchecked")
    StreamTransformTranslator<T> typedTranslator = (StreamTransformTranslator<T>) translator;

    // create the applied PTransform on the streamingContext
    streamingContext.setCurrentTransform(node.toAppliedPTransform());
    typedTranslator.translateNode(typedTransform, streamingContext);
  }

  private <T extends PTransform<?, ?>> boolean applyCanTranslate(
      PTransform<?, ?> transform,
      TransformHierarchy.Node node,
      StreamTransformTranslator<?> translator) {

    @SuppressWarnings("unchecked")
    T typedTransform = (T) transform;

    @SuppressWarnings("unchecked")
    StreamTransformTranslator<T> typedTranslator = (StreamTransformTranslator<T>) translator;

    streamingContext.setCurrentTransform(node.toAppliedPTransform());

    return typedTranslator.canTranslate(typedTransform, streamingContext);
  }

  /**
   * The interface that every Flink translator of a Beam operator should implement.
   * This interface is for streaming jobs. For examples of such translators see
   * {@link FlinkStreamingTransformTranslators}.
   */
  abstract static class StreamTransformTranslator<T extends PTransform> {

    /**
     * Translate the given transform.
     */
    abstract void translateNode(T transform, FlinkStreamingTranslationContext context);

    /**
     * Returns true iff this translator can translate the given transform.
     */
    boolean canTranslate(T transform, FlinkStreamingTranslationContext context) {
      return true;
    }
  }

  private static class ReflectiveOneToOneOverrideFactory<
          InputT, OutputT, TransformT extends PTransform<PCollection<InputT>, PCollection<OutputT>>>
      extends SingleInputOutputOverrideFactory<
          PCollection<InputT>, PCollection<OutputT>, TransformT> {
    private final Class<PTransform<PCollection<InputT>, PCollection<OutputT>>> replacement;
    private final FlinkRunner runner;

    private ReflectiveOneToOneOverrideFactory(
        Class<PTransform<PCollection<InputT>, PCollection<OutputT>>> replacement,
        FlinkRunner runner) {
      this.replacement = replacement;
      this.runner = runner;
    }

    @Override
    public PTransformReplacement<PCollection<InputT>, PCollection<OutputT>> getReplacementTransform(
        AppliedPTransform<PCollection<InputT>, PCollection<OutputT>, TransformT> transform) {
      return PTransformReplacement.of(
          PTransformReplacements.getSingletonMainInput(transform),
          InstanceBuilder.ofType(replacement)
              .withArg(FlinkRunner.class, runner)
              .withArg(
                  (Class<PTransform<PCollection<InputT>, PCollection<OutputT>>>)
                      transform.getTransform().getClass(),
                  transform.getTransform())
              .build());
    }
  }

  /**
   * A {@link PTransformOverrideFactory} that overrides a Splittable DoFn with {@link SplittableParDo}.
   */
  static class SplittableParDoOverrideFactory<InputT, OutputT>
      implements PTransformOverrideFactory<
          PCollection<InputT>, PCollectionTuple, MultiOutput<InputT, OutputT>> {
    @Override
    public PTransformReplacement<PCollection<InputT>, PCollectionTuple>
        getReplacementTransform(
            AppliedPTransform<
                    PCollection<InputT>, PCollectionTuple, MultiOutput<InputT, OutputT>>
                transform) {
      return PTransformReplacement.of(
          PTransformReplacements.getSingletonMainInput(transform),
          new SplittableParDo<>(transform.getTransform()));
    }

    @Override
    public Map<PValue, ReplacementOutput> mapOutputs(
        Map<TupleTag<?>, PValue> outputs, PCollectionTuple newOutput) {
      return ReplacementOutputs.tagged(outputs, newOutput);
    }
  }
}


text:
import logging

import CsHelper
from cs.CsFile import CsFile

class CsVrConfig(object):
    def __init__(self, config):
        self.config = config
        self.dbag = self.config.dbag_network_virtualrouter

    def process(self):
        logging.debug("Processing CsVrConfig file ==> %s" % self.dbag)

        for item in self.dbag:
            if item == "id":
                continue

            if item == "source_nat_list":
                self._configure_firewall(self.dbag[item])

    def _configure_firewall(self, sourcenatlist):
        firewall = self.config.get_fw()

        logging.debug("Processing source NAT list: %s" % sourcenatlist)
        for cidr in sourcenatlist.split(','):
            firewall.append(["filter", "", "-A SOURCE_NAT_LIST -o eth2 -s %s -j ACCEPT" % cidr])


text:
package fr.free.nrw.commons.navtab;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.bookmarks.BookmarkFragment;
import fr.free.nrw.commons.explore.ExploreFragment;
import org.wikipedia.model.EnumCode;
import org.wikipedia.model.EnumCodeMap;

public enum NavTabLoggedOut implements EnumCode {

  EXPLORE(R.string.navigation_item_explore, R.drawable.ic_globe) {
    @NonNull
    @Override
    public Fragment newInstance() {
      return ExploreFragment.newInstance();
    }
  },
  FAVORITES(R.string.favorites, R.drawable.ic_round_star_border_24px) {
    @NonNull
    @Override
    public Fragment newInstance() {
      return BookmarkFragment.newInstance();
    }
  },
  MORE(R.string.more, R.drawable.ic_menu_black_24dp) {
    @NonNull
    @Override
    public Fragment newInstance() {
      return null;
    }
  };

  private static final EnumCodeMap<NavTabLoggedOut> MAP = new EnumCodeMap<>(NavTabLoggedOut.class);

  @StringRes
  private final int text;
  @DrawableRes
  private final int icon;

  @NonNull
  public static NavTabLoggedOut of(int code) {
    return MAP.get(code);
  }

  public static int size() {
    return MAP.size();
  }

  @StringRes
  public int text() {
    return text;
  }

  @DrawableRes
  public int icon() {
    return icon;
  }

  @NonNull
  public abstract Fragment newInstance();

  @Override
  public int code() {
    // This enumeration is not marshalled so tying declaration order to presentation order is
    // convenient and consistent.
    return ordinal();
  }

  NavTabLoggedOut(@StringRes int text, @DrawableRes int icon) {
    this.text = text;
    this.icon = icon;
  }
}


text:
//=================================================================================================

//=================================================================================================

#ifndef _BLAZE_UTIL_CONSTRAINTS_COMPLEXFLOAT_H_
#define _BLAZE_UTIL_CONSTRAINTS_COMPLEXFLOAT_H_

//*************************************************************************************************
// Includes
//*************************************************************************************************

#include <blaze/util/typetraits/IsComplexFloat.h>

namespace blaze {

//=================================================================================================
//
//  MUST_BE_COMPLEX_FLOAT_TYPE CONSTRAINT
//
//=================================================================================================

//*************************************************************************************************
/*!\brief Constraint on the data type.
// \ingroup constraints
//
// This compile time constraint checks that the given data type \a T is a single precision
// complex data type (i.e. \a complex<float>, ignoring the cv-qualifiers). In case \a T is
// not of type \a complex<float> a compilation error is created.
*/
#define BLAZE_CONSTRAINT_MUST_BE_COMPLEX_FLOAT_TYPE(T) \
   static_assert( ::blaze::IsComplexFloat<T>::value, "Non-single precision complex type detected" )
//*************************************************************************************************

//=================================================================================================
//
//  MUST_NOT_BE_COMPLEX_TYPE CONSTRAINT
//
//=================================================================================================

//*************************************************************************************************
/*!\brief Constraint on the data type.
// \ingroup constraints
//
// This compile time constraint checks that the given data type \a T is not a single precision
// complex data type (i.e. \a complex<float>, ignoring the cv-qualifiers). In case \a T is of
// type \a complex<float> a compilation error is created.
*/
#define BLAZE_CONSTRAINT_MUST_NOT_BE_COMPLEX_FLOAT_TYPE(T) \
   static_assert( !::blaze::IsComplexFloat<T>::value, "Single precision complex type detected" )
//*************************************************************************************************

} // namespace blaze

#endif


text:
'use strict';
var meta = require('./meta');
var Cookie = require('./cookie');
var prepare = require('./prepare');
var parse = require('./parse');

var setup = function(type) {
  var storage;
  try {
    storage = window[type];
    try {
      /*eslint-disable*/
      var test = storage['foo'];
      /*eslint-enable*/
    } catch(e) {
      storage = undefined;
    }
  } catch(e) {
    storage = undefined;
  }
  if (!storage) {
    // console.warn('Vault: ' + type + ' is not suppored. I will attempt to use Cookies instead.');
    return Cookie;
  }
  return {
    type: type,
    get: function(key, default_value) {
      if (storage[key]) {
        var keyMeta = meta.checkKeyMeta(storage, key);
        if (keyMeta) {
          return default_value;
        }
        var value = parse(storage[key]);
        return (value && value.value) || value;
      }
      return default_value;
    },
    getAndRemove: function(key) {
      var value = this.get(key);
      this.remove(key);
      return value;
    },
    getList: function() {
      var list = [];
      for (var i in storage) {
        var item = {};
        item[i] = this.get(i);
        list.push(item);
      }
      return list;
    },
    set: function(key, value, config) {
      if (!key) {
        return console.warn('Vault: set was called with no key.', key);
      }
      try {
        if (type === 'sessionStorage' && config && config.expires) {
          delete config.expires;
        }
        meta.setKeyMeta(storage, key, config);
        return storage.setItem(key, prepare(value));
      } catch(e) {
        console.warn('Vault: I cannot write to localStoarge even though localStorage is supported. Perhaps you are using your browser in private mode? Here is the error: ', e);
      }
    },
    remove: function(key) {
      meta.clearKeyMeta(storage, key);
      return storage.removeItem(key);
    },
    clear: function() {
      return storage.clear();
    },
    list: function(raw) {
      var i, il = storage.length;
      if (il === 0) {
        console.log('0 items in ' + type);
        return undefined;
      }
      for (i in storage) {
        var value = raw ? parse(storage[i]) : this.get(i);
        console.log(i, '=', value);
      }
    }
  };
};
module.exports = {
  Local: setup('localStorage'),
  Session: setup('sessionStorage'),
  Cookie: Cookie
};