Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Record and replay SQL queries #1221

Unanswered
matthiasschaub asked this question in Q&A
Discussion options

I am experimenting with ways to record and replay SQL queries send by asyncpg (similar to VCR.py).

I do not know the internals of asyncpg well and therefore would appreciate feedback and pointers.

To record and replay, following steps have to be done (roughly):

  1. Record the SQL query and the results
  2. Next time the same SQL query is executed, do not interact with the database at all (e.g. no connection and no execution). Instead, replay the recorded results.

Step 1

After reading the source code of asyncpg a bit, I decided to wrap the Connection._execute function like this:

(As context: Below record function is intended to be used as decorator around a test function.)

def record(func):
 @wraps(func)
 async def wrapper(*args, **kwargs):
 execture_original = asyncpg.connection.Connection._execute
 @wraps(execture_original)
 async def execute_wrapper(self, *execute_args, **execute_kwargs):
 # TODO: Record (serialize to disk) execute parameters and result
 return await execture_original(self, *execute_args, **execute_kwargs)
 asyncpg.connection.Connection._execute = execute_wrapper
 return await func(*args, **kwargs)
 return wrapper

Do you find this sensible? Any scenario where this approach would not work?

Step 2

Here I mainly have the following questions:

Which is the best way to mock every possible interaction with the database and create a asyncpg record object from the recorded results?


Any thoughts about any of the points above are highly appreciated. Thanks!

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /