Testing the Repository with SQLite

Learn how to write test cases to test the repository with SQLite.

We wrote tests for the pomodoro package using a helper function getRepo() to obtain the repository. At that time, only the inMemory repository was available. Now that we’ve added the sqlite3 repository, we’ll provide an alternative version of this function that returns the new repository. We can control when to use each by applying build tags.

Updating the sqlite3_test.go file

We update the sqlite3_test.go file in our editor to write the specific function for this repository implementation. We add the build tag +build !inmemory to use this file unless we provide the tag inmemory, in which case it’ll include the file inmemory_test.go instead. We already added the equivalent build tag to that file. We skip a line and add the package definition:

Get hands-on with 1200+ tech skills courses.