quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.73k stars 2.67k forks source link

Introduce DbTestClient which can be used to reset DB before the next test #28188

Closed sberyozkin closed 2 years ago

sberyozkin commented 2 years ago

Description

I've started securing quarkus-quickstarts/hibernate-orm-panache-quickstart in devmode, with quarkus-oidc, and by adding @RolesAllowed to various endpoint methods. The tests delete one of the entities, so running them first as admin, and then as user does not work, because by the time the user is checked, one of the entities has gone. In DevUI, there is a nice option to reset DB, clicking it reruns and SQL script used in the demo.

It would be good, in tests, to have something like

DbTestClient dbClient = new DbTestClient();
// before each test: dbClient.reset()

@Test 
public void testAsAdmin() {
}
@Test 
public void testAsUser() {
} 

Both tests will pass in any order and with each of them having side-effects. And the reset DB option would be available not only in Dev UI but also in tests

It would be similar to KeycloakTestClient which is initialized by DevServices for Keycloak with the added of Keycloak.

Implementation ideas

No response

quarkus-bot[bot] commented 2 years ago

/cc @Sanne, @gsmet, @yrodiere

yrodiere commented 2 years ago

Hey @sberyozkin, I think this duplicates #14240? #15198 seems related as well in some way.

Also, there was an attempt to solve this in the past, in #15827 , but it wasn't merged for some reason.

sberyozkin commented 2 years ago

Hey @yrodiere Sorry, I haven't analyzed what has been done before in this area, and looks like, in #15827, it was assumed it was too complex to do right, with several alternatives considered. I've no problems with closing this issue, but perhaps I can keep it open for a bit to have the discussion refreshed ? I saw in #15827 some concerns expressed, but it works well from DevUI, I believe it is of primary interest to tests depending on PostgreSQL and other containers launched by DB Dev Services so perhaps it can make sense to mirror this option in the tests...

yrodiere commented 2 years ago

No problem with restarting the discussion, but let's not duplicate it? I think it would be better to post your API suggestion to #14240, and to link any PR to that.

And yes, I think most agree that this feature has value; the disagreement was about execution. I think your solution of a (injected?) client that controls the reset probably solves most issues we had.

sberyozkin commented 2 years ago

@yrodiere Sure, will do. Stuart had a nice test system enhancement, so I'll update #14240 and propose to fix it similarly to how it was done for KeycloakTestClient