database-rider icon indicating copy to clipboard operation
database-rider copied to clipboard

how to remove inserted record in test?

Open codeweb opened this issue 7 years ago • 12 comments

I would now how to remove inserted record in a test environment:

@Test
@DataSet(transactional=true)
@ExpectedDataSet(value = "foo.csv", ignoreCols = "id")
public void runGenericTest() throws Exception {

Using CleanAfter/Before will remove records also in other tables. Using the following code:

@After
public void clean_all() {

it will remove records before ExpectedDataSet action.

codeweb avatar Jan 22 '18 10:01 codeweb

Hi, currently there is no way to clean only inserted records after test execution. Also note that seedStrategy is applied only on tables declared in @DataSet.

Having said that, maybe we could have a new seedStratagy e.g CLEAN_INSERT_CLEAN which would clean records on tables declared in @DataSet after test execution.

rmpestano avatar Jan 22 '18 10:01 rmpestano

Is it possible to set tables on which CleanAfter/Before works?

codeweb avatar Jan 22 '18 10:01 codeweb

Well, it uses the tableOrdering attribute in a first moment (see here) but after that it clears other tables also, see here.

Don't know how we could define that we won't clear other tables in a way to not confuse the api. Example adding another attribute to @DataSet annotation like "skipAutomaticCleanup" may lead to confusion.

So unless you have another idea I still think adding another seedingStragy is the recommended way.

rmpestano avatar Jan 22 '18 10:01 rmpestano

I agree and it's very interesting. Moreover I'm asking myself who can want delete all tables in database and why. Maybe "skipAutomaticCleanup" can help also in this situation.

I will close this issue.

codeweb avatar Jan 22 '18 11:01 codeweb

Also another idea is to configure that behaviour (the "skipAutomaticCleanup") in @DBUnit annotation or in dbunit.yml see configuration section here, but that will apply to all tests (in case of dbunit.yml) or in all tests of test class when using @DBunit annotation, something like:

@RunWith(JUnit4.class)
@DBUnit(autoCleanUp = false)
public class CrudIt {

    @Test
    @DataSet(transactional = true, cleanAfter = true, tableOrdering = "foo,bar") //will clean only 'foo' and 'bar' tables 
    @ExpectedDataSet(value = "foo.csv", ignoreCols = "id")
    public void aTest() throws Exception {
    }

    @Test
    @DBunit(autoCleanUp = true)
    @DataSet(transactional = true, cleanAfter = true) //will clean all tables because method level annotation takes precedence
    @ExpectedDataSet(value = "foo.csv", ignoreCols = "id")
    public void anotherTest() throws Exception {
    }
}

What do you think?

rmpestano avatar Jan 22 '18 11:01 rmpestano

"Moreover I'm asking myself who can want delete all tables in database and why" well usually the test database is used only for tests and an easy(and recommended) way to guarantee desired state is to clear everything before test execution.

rmpestano avatar Jan 22 '18 11:01 rmpestano

Not "everything" but only interested tables... What do you think?

codeweb avatar Jan 22 '18 11:01 codeweb

Yes, clean_insert strategy do that, clear tables declared in @Dataset("mydataset.yml") before test execution

rmpestano avatar Jan 22 '18 12:01 rmpestano

And what if dataset is not specified (I use only ExpectedDataSet)?

codeweb avatar Jan 22 '18 13:01 codeweb

I think the best way to handle your case is to implement what I've described here: https://github.com/database-rider/database-rider/issues/68#issuecomment-359391609

rmpestano avatar Jan 22 '18 13:01 rmpestano

Ok, thanks. I hope someone will do it.

codeweb avatar Jan 22 '18 13:01 codeweb

Let's keep it opened until there.

Thank you.

rmpestano avatar Jan 22 '18 13:01 rmpestano