one way to write a test when you have to test a database is always to bring up an in-memory database to test it or a real database with fake data I encourage you to write tests in this way not unit tests because in unit tests all your testing is the fake or dummy that you write here is an example of what I mean:
import (
"testing"
"database/sql"
_ "github.com/mattn/go-sqlite3" // import the sqlite driver
)
func TestMyFunction(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:") // use an in-memory sqlite database for testing
if err != nil {
t.Fatal(err)
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback() // rollback the transaction at the end of the test
// Execute your code under test within the transaction
// ...
err = tx.Commit() // commit the transaction if there were no errors
if err != nil {
t.Fatal(err)
}
// Check the results of your code under test
// ...
}