]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Test execution_options on Query object before compilation
authorRamonWill <ramonwilliams@hotmail.co.uk>
Thu, 13 Aug 2020 21:04:13 +0000 (17:04 -0400)
committersqla-tester <sqla-tester@sqlalchemy.org>
Thu, 13 Aug 2020 21:04:13 +0000 (17:04 -0400)
 A test that checks if the execution options are being set on the Query object before compilation

### Description
Since Issue #4670 has been open there have changes to the execution mechanics that resolved it. I have just created a test case that will enable the issue to be closed.

This pull request is:

- [ X] A documentation / typographical error fix
- Good to go, no issue or tests are needed
- [ ] A short code fix
- please include the issue number, and create an issue if none exists, which
  must include a complete example of the issue.  one line code fixes without an
  issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.   one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
  include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.

**Have a nice day!**
Fixes: #4670
Closes: #5446
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5446
Pull-request-sha: c98639ec94279760caacf9f288b180198511dfd3

Change-Id: I686dc7a4735000748c94bff5ba6dc7bd57b2d1c4

test/orm/test_events.py

index df48cfe63c5552ddeaec8cdddd0e4dafb668c75b..aff47d96efae99f622f817332cc77c52e9828778 100644 (file)
@@ -2768,6 +2768,25 @@ class QueryEventsTest(
             )
         )
 
+    def test_before_compile_execution_options(self):
+        User = self.classes.User
+
+        @event.listens_for(query.Query, "before_compile", retval=True)
+        def _before_compile(query):
+            return query.execution_options(my_option=True)
+
+        opts = {}
+
+        @event.listens_for(testing.db, "before_cursor_execute")
+        def _before_cursor_execute(
+            conn, cursor, statement, parameters, context, executemany
+        ):
+            opts.update(context.execution_options)
+
+        sess = create_session(bind=testing.db, autocommit=False)
+        sess.query(User).first()
+        eq_(opts["my_option"], True)
+
 
 class RefreshFlushInReturningTest(fixtures.MappedTest):
     """test [ticket:3427].