diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-01-10 17:47:38 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-01-10 17:47:38 -0500 |
| commit | b301f009e18246db9277a4b9d7e3a1bf01a92ae9 (patch) | |
| tree | 8715eeccab4ba3188d0da3ff51a7a4200e7fe736 /test/orm/test_options.py | |
| parent | d03477d4b761132f44c48417be09d622ad9adfd3 (diff) | |
| download | sqlalchemy-b301f009e18246db9277a4b9d7e3a1bf01a92ae9.tar.gz | |
- refactor a bit the loader options system to make it a bit more
intelligible, given the fixes for ref #3623. unfortunately the system
is still quite weird even though it was rewritten to be... less weird
Diffstat (limited to 'test/orm/test_options.py')
| -rw-r--r-- | test/orm/test_options.py | 128 |
1 files changed, 125 insertions, 3 deletions
diff --git a/test/orm/test_options.py b/test/orm/test_options.py index e1e26c62c..e7b750cf4 100644 --- a/test/orm/test_options.py +++ b/test/orm/test_options.py @@ -3,11 +3,14 @@ from sqlalchemy.orm import attributes, mapper, relationship, backref, \ configure_mappers, create_session, synonym, Session, class_mapper, \ aliased, column_property, joinedload_all, joinedload, Query,\ util as orm_util, Load, defer +from sqlalchemy.orm.query import QueryContext +from sqlalchemy.orm import strategy_options import sqlalchemy as sa from sqlalchemy import testing -from sqlalchemy.testing.assertions import eq_, assert_raises, assert_raises_message +from sqlalchemy.testing.assertions import eq_, assert_raises_message from test.orm import _fixtures + class QueryTest(_fixtures.FixtureTest): run_setup_mappers = 'once' run_inserts = 'once' @@ -17,6 +20,7 @@ class QueryTest(_fixtures.FixtureTest): def setup_mappers(cls): cls._setup_stock_mapping() + class PathTest(object): def _make_path(self, path): r = [] @@ -160,11 +164,11 @@ class LoadTest(PathTest, QueryTest): ) + + class OptionsTest(PathTest, QueryTest): def _option_fixture(self, *arg): - from sqlalchemy.orm import strategy_options - return strategy_options._UnboundLoad._from_keys( strategy_options._UnboundLoad.joinedload, arg, True, {}) @@ -768,3 +772,121 @@ class OptionsNoPropTest(_fixtures.FixtureTest): create_session().query(column).options, joinedload(eager_option)) + +class LocalOptsTest(PathTest, QueryTest): + @classmethod + def setup_class(cls): + super(LocalOptsTest, cls).setup_class() + + @strategy_options.loader_option() + def some_col_opt_only(loadopt, key, opts): + return loadopt.set_column_strategy( + (key, ), + None, + opts, + opts_only=True + ) + + @strategy_options.loader_option() + def some_col_opt_strategy(loadopt, key, opts): + return loadopt.set_column_strategy( + (key, ), + {"deferred": True, "instrument": True}, + opts + ) + + cls.some_col_opt_only = some_col_opt_only + cls.some_col_opt_strategy = some_col_opt_strategy + + def _assert_attrs(self, opts, expected): + User = self.classes.User + + query = create_session().query(User) + attr = {} + + for opt in opts: + if isinstance(opt, strategy_options._UnboundLoad): + for tb in opt._to_bind: + tb._bind_loader(query, attr, False) + else: + attr.update(opt.context) + + key = ( + 'loader', + tuple(inspect(User)._path_registry[User.name.property])) + eq_( + attr[key].local_opts, + expected + ) + + def test_single_opt_only(self): + opt = strategy_options._UnboundLoad().some_col_opt_only( + "name", {"foo": "bar"} + ) + self._assert_attrs([opt], {"foo": "bar"}) + + def test_unbound_multiple_opt_only(self): + opts = [ + strategy_options._UnboundLoad().some_col_opt_only( + "name", {"foo": "bar"} + ), + strategy_options._UnboundLoad().some_col_opt_only( + "name", {"bat": "hoho"} + ) + ] + self._assert_attrs(opts, {"foo": "bar", "bat": "hoho"}) + + def test_bound_multiple_opt_only(self): + User = self.classes.User + opts = [ + Load(User).some_col_opt_only( + "name", {"foo": "bar"} + ).some_col_opt_only( + "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( + "name", {"foo": "bar"} + ).some_col_opt_strategy( + "name", {"bat": "hoho"} + ) + ] + self._assert_attrs(opts, {"foo": "bar", "bat": "hoho"}) + + def test_unbound_strat_opt_recvs_from_optonly(self): + opts = [ + strategy_options._UnboundLoad().some_col_opt_only( + "name", {"foo": "bar"} + ), + strategy_options._UnboundLoad().some_col_opt_strategy( + "name", {"bat": "hoho"} + ) + ] + self._assert_attrs(opts, {"foo": "bar", "bat": "hoho"}) + + def test_unbound_opt_only_adds_to_strat(self): + opts = [ + strategy_options._UnboundLoad().some_col_opt_strategy( + "name", {"bat": "hoho"} + ), + strategy_options._UnboundLoad().some_col_opt_only( + "name", {"foo": "bar"} + ), + ] + 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( + "name", {"bat": "hoho"} + ).some_col_opt_only( + "name", {"foo": "bar"} + ), + ] + self._assert_attrs(opts, {"foo": "bar", "bat": "hoho"}) |
