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

Annotation to load SQL before a test @Sql #851

Merged
merged 26 commits into from
Oct 23, 2023
Merged

Annotation to load SQL before a test @Sql #851

merged 26 commits into from
Oct 23, 2023

Conversation

timyates
Copy link
Contributor

@timyates timyates commented Sep 18, 2023

Started with Spock support

Closes #832

@timyates timyates self-assigned this Sep 18, 2023
@timyates timyates changed the title Add Spock support Annotation to load SQL before a test @Sql Sep 18, 2023
@timyates timyates added the type: enhancement New feature or request label Sep 18, 2023
@timyates timyates marked this pull request as ready for review September 18, 2023 16:01
@alvarosanchez
Copy link
Member

Would it be too crazy to allow this annotation in other than tests? (at my own risk). I can imagine using it for some of my demos on the Application class

@dstepanov
Copy link
Contributor

If you already working on this code, maybe you can take a look and rewrite the code:

            final List<Property> ps = AnnotationUtils.findRepeatableAnnotations(testClass, Property.class);

To read the annotations from the bean definition, allowing for meta annotations.

@timyates
Copy link
Contributor Author

Would it be too crazy to allow this annotation in other than tests? (at my own risk). I can imagine using it for some of my demos on the Application class

If we want to do that, we need to decide now as this means the annotation would need a different place to live.

And a different mechanism to invoke it

@micronaut-projects/core-developers do people have thoughts on this?

@sdelamo
Copy link
Contributor

sdelamo commented Sep 19, 2023

Would it be too crazy to allow this annotation in other than tests? (at my own risk). I can imagine using it for some of my demos on the Application class

If we want to do that, we need to decide now as this means the annotation would need a different place to live.

And a different mechanism to invoke it

@micronaut-projects/core-developers do people have thoughts on this?

I think we could keep @Sql test-only.

However, We could create a similar feature to load data on startup, such as the data.sql and schema.sql in Spring.. I think being able to load automatically data.sql and schema.sql files in src/main/resources would fulfill what @alvarosanchez is describing.

@alvarosanchez
Copy link
Member

@sdelamo that would work perfectly, yes

@melix
Copy link
Contributor

melix commented Sep 19, 2023

Out of curiosity, what is the the benefit of doing this vs using a StartupEvent listener to perform the injection?

@sdelamo
Copy link
Contributor

sdelamo commented Sep 19, 2023

Out of curiosity, what is the the benefit of doing this vs using a StartupEvent listener to perform the injection?

I assume by injection you mean loading of sql file into the database. What do you mean by "this"? Using @Sql or using data.sql ? The benefit is you don't have to write almost any code. No class with a startup listener. I a guide, I am porting from Spring Boot I had to https://github.com/micronaut-projects/micronaut-guides/pull/1338/files#diff-c930a4c88bedee28367f71ecac1d44d25bebae55251aca4a8247379fde75c384 and this in my test:

    @Inject
    Connection connection;

    @Inject
    ResourceLoader resourceLoader;


    @BeforeEach
    void setUp() throws IOException, SQLException {
        SqlUtils.load(connection, resourceLoader, "sql/seed-data.sql");
    }

@timyates
Copy link
Contributor Author

To read the annotations from the bean definition, allowing for meta annotations.

We need the properties before we have an application context, which means before we have a specDefinition, so not sure how we can switch to read the Properties from the definition

@timyates
Copy link
Contributor Author

I think I have a way of supporting R2DBC...

Working on it

@timyates
Copy link
Contributor Author

I need to investigate the implicit MicronautTest transactions...

I think I need to move where we do the Sql injection...

@timyates
Copy link
Contributor Author

@dstepanov could you take another look? It supports R2dbc now...

Not sure if we need method level annotation... We can set method level transactions in MicronautTest, so I guess we do...

@timyates timyates requested review from sdelamo and removed request for alvarosanchez September 21, 2023 13:36
Copy link
Contributor

@sdelamo sdelamo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the nullability annotations to SqlHandler

@timyates
Copy link
Contributor Author

I added the nullability annotations to SqlHandler

Thanks! Sorry I missed them in the rewrite 😞

sdelamo and others added 2 commits October 13, 2023 11:04
* add test-suite-at-sql-jpa module

* add tests

* change hikari

* fix id

* rollback SQL

* add insert

* Add rollback to the annotation

* Switch to allow phases

* Docs

---------

Co-authored-by: Tim Yates <tim.yates@gmail.com>
@sonarcloud
Copy link

sonarcloud bot commented Oct 20, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

63.2% 63.2% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@sdelamo sdelamo merged commit 00ed0f8 into master Oct 23, 2023
13 of 14 checks passed
@sdelamo sdelamo deleted the sql-annotation branch October 23, 2023 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Annotation to load SQL before a test @Sql
5 participants