Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: implement integration testing #124

Merged
merged 42 commits into from
Aug 23, 2024

Conversation

DeagleGross
Copy link
Collaborator

So far the only testing available is "eye-testing":

  1. write user code input
  2. generate intercepted code out of it
  3. check if generated code makes sense.

It would be great to have some integration tests using generated bits against some database to ensure code works. I created Dapper.AOT.Test.Integration project as a launcher of tests and Dapper.AOT.Test.Integration.Executables project as user code (similar to input we had before).

Tests work in this way:

  • Use a type from Executables (I will further call it "user code") and parse the code as text
  • Create compilation out of it which includes minimum amount of assembly references to make it work
  • Run DapperInterceptorGenerator on that compilation and get new compilation instance with both user code and generated bits
  • Run compilation.Emit() to get a built-in assembly in the code
  • Via reflection access the user code type specified on the first step and activate an instance of the class
  • Execute the code passing pre-created IDbConnection.

To make sure that generated interception code is actually being invoked, I also adjusted DapperInterceptorGenerator to include such a code line:

global::Dapper.AOT.Test.Integration.Executables.Recording.InterceptorRecorderResolver.Resolve().Record();

Since Dapper.AOT.Test.Integration.Executables assembly is anyway referenced to the compilation built during a test execution, we have access to all types there. InterceptorRecorderResolver is a container for IInterceptorRecorder -> test code can register any implementation of recorder to collect all necessary information it can. The implementation I have provided does the following:

  • get the StackTrace and takes only last 15 frames out of it
  • finds invocation of intercepted code (generated bits)
  • finds invocation of user code
  • ensures that user code is invoked before intercepted code (which is logical)

In such a manner we can be sure that generated code is actually executed.

Closes #122

@DeagleGross DeagleGross added the enhancement New feature or request label Aug 1, 2024
@DeagleGross DeagleGross requested a review from mgravell August 1, 2024 23:52
@DeagleGross DeagleGross self-assigned this Aug 1, 2024
@mgravell mgravell merged commit a9328b1 into DapperLib:main Aug 23, 2024
2 checks passed
@DeagleGross DeagleGross deleted the dmkorolev/integration-tests branch August 23, 2024 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement integration testing using generated bits
2 participants