Unit Testing on SQL scripts

We have a postgreSQL data source.

Just like we write JUnit tests for Java files.We want to implement unit testing for SQL queries(unit tests on JOINS , DATATYPE..etc) we write related to Stored Procedures on our DB.

  • We want to run these unit tests on a in-memory DB so that it will feasible to run the tests without calling the DB and can run locally

Is the above idea feasible? Any other suggestions/ideas are welcome.

Searched in Internet got some testing tools like pgTAP.
But it establishes a DB connection which fails to meet our requirements.

  • This does not look like a valid case for unit tests – they should not use any in-memory DB. Use a proper integration test for this, and run this on a proper database. This will ensure that everything works in a real scenario, and you won’t face any incompatibilties between that “in-memory DB” and PostgreSQL

    – 

  • I suppose you also want to run your Java tests without having Java installed too?

    – 

Unit testing isn’t really feasible here. The point is, that the queries are executed inside the DB process. You’d have to isolate those parts and verify that the expected data is written to the storage.

Instead, integration testing or functional testing is a better approach. Run the parts of your code with a test DB server. Verify that the data that comes out resembles the data that went in. For such a test DB server, using a Docker container is probably the quickest way to get it running.

Leave a Comment