Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make unit tests on local and on build server using postgres?

I'm tyring to make unit tests for my node application. I'm using a postgresql database for the development and SQLite for the tests. However Sqlite does not understand some features of postgresql such as to_tsvectorand sometimes I got a problem of SQLITE databse locked. So I think for a server to test the application on local and on build sever. Is it a good solution to do that? I found some alternatives that mention to use docker container testing with docker.

So what are the suitable solution to run postgres test on local and server build without getting problem of databse lock?

like image 581
Slim Avatar asked Dec 05 '25 05:12

Slim


1 Answers

I would avoid using the database in the unit tests as they are now dependant on an external system:

Part of being a unit test is the implication that things outside the code under test are mocked or stubbed out. Unit tests shouldn't have dependencies on outside systems. They test internal consistency as opposed to proving that they play nicely with some outside system.

Basically, mock any calls to the database so you don't need to use one.

However, If you really must use a Postgres database you should use the official image in a compose file and initialize it with your schema. You can then connect to that with your tests in a known state, etc.

As for the Database lock, it may disappear after using Postgres instead of SQLite and you may want to check if you have any concurrency in your tests.

like image 141
syncdk Avatar answered Dec 07 '25 02:12

syncdk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!