question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Database not cleared automatically between tests

See original GitHub issue

I am struggling with Rider for the fact that database is never cleared and its state is shared across different test classes, creating some major issues. And I am wondering if I am not using it correctly or is an issue. I am following the example of Quarkus. The main issue I am finding is tests failing because the data set cannot initialized, because some other test inserted data that prevents some table from being fully deleted.

  1. For example, let’s have 3 tables: A, B & C, where B & C have a FK to A’s id. A’s data in initialized with a yml data set using @DataSet.
  2. In one test on it’s own class I insert a registry in C, and don’t delete it. Be it an accident or just because the test failed for some other reason and the manual deletion was not executed.
  3. Then, any tests that uses A’s dataset fails to start with a message like this
should_delete_all_B  Time elapsed: 0.074 s  <<< ERROR!
com.github.database.rider.core.exception.DataBaseSeedingException: Could not initialize dataset: a.yml
Caused by: org.h2.jdbc.JdbcSQLException: 
Referential integrity constraint violation: "FKJYATY0XA5P7DMGQP41FYB0SNE: PUBLIC.C FOREIGN KEY(A_ID) REFERENCES PUBLIC.A(ID) (1011)"; SQL statement:
delete from A [23503-197]

You see this forces to go through all tests trying to find where this data is inserted. It could be a failing test, or just a new test that does not clear it’s data.

Seeing this comment https://github.com/database-rider/database-rider/issues/68#issuecomment-359396449, I think we agree data should be reseted on every test, however that’s not the case.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rmpestanocommented, Jan 22, 2020

Hi @abelsromero, with this PR you can specify cleanBefore in a @BeforeEach callback which would result in cleaning db before every test, eg:

    @BeforeEach
    @DataSet(cleanBefore = true)
    public void cleanBeforeEach() {

    }

I know you would like a way to override defaults but this can help your usecase.

0reactions
rmpestanocommented, Sep 14, 2019

Note that the annotation will be used most of the time because datasets may change between tests

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django's TestCase doesn't seem to clean up database ...
If TestCase.tearDownClass() is not called via super() then the database is not reset in between test cases and you will get the dreaded ......
Read more >
The argument against clearing the database between tests
Tearing down data between tests or schemas between test runs is not free of (computational) charge. The time taken to clean the database...
Read more >
Database Testing Complete Guide (Why, What, and How to ...
I hope this tutorial will help you to focus on why database testing is important and also provide all the details of what...
Read more >
How to clean up DB record created during test run
If your application has a Delete option, then just write a little script/keyword test to go back and delete the records you've created....
Read more >
Testing tools | Django documentation
A TestCase , on the other hand, does not truncate tables after a test. Instead, it encloses the test code in a database...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found