You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, we use Mock Database for our Business Logic tests using github.com/stretchr/testify/mock, the issue with using the mock DB is the Mock will always return a response if the right input is provided, and cannot assert that the SQL query is working as it should.
e.g
func TestBountyMetrics(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
// Mock DB
mockDb := mocks.NewDatabase(t)
mh := NewMetricHandler(mockDb)
t.Run("should fetch bounties count within specified date range", func(t *testing.T) {
db.RedisError = errors.New("redis not initialized")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(mh.MetricsBountiesCount)
dateRange := db.PaymentDateRange{
StartDate: "1111",
EndDate: "2222",
}
body, _ := json.Marshal(dateRange)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/boutnies/count", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
// Using mock DB function
mockDb.On("GetBountiesByDateRangeCount", dateRange, req).Return(int64(100)).Once()
handler.ServeHTTP(rr, req)
var res int64
_ = json.Unmarshal(rr.Body.Bytes(), &res)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, res, int64(100))
})
}
Design
We want to create a Postgres database and then test the database functions to assert that the SQL queries work as they should.
We can achieve this using these two approaches
Create a database programmatically, and drop the database after all the tests are processed.
Using Docker Compose to spin up a Postgres container, the tests will connect to the Postgres DB, and then after the test, we will drop the Postgres container
Programmatic Postgres Database Creation
Pros
It's easier to run on Local, running the go test command go test ./.... spins up the database before running the test.
Cons
Uncertainty on how long it will take for the Postgres DB to spin up for now, which may lead to test timing out.
Requires more experience and understanding of the library to maintain the code for future purposes
Docker Compose Database Creation
Pros
The Database will be up and running before the Go tests start, which will reduce the likelihood of tests failing due to DB connection timeout
Easier to pick up and edit In the future buy anyone who understands docker and docker-compose
Cons
One has to run the docker-compose-up command before running the Golang, and docker-compose-down after the Golang test, running this process on GitHub actions will make the commands easier since it's a constant.
elraphty
changed the title
Replace Mock Database With Live Postgres Database For Integration / Unit Testing
Replace Mock Database With Real Postgres Database For Integration / Unit Testing
Jun 13, 2024
Context
Currently, we use Mock Database for our Business Logic tests using github.com/stretchr/testify/mock, the issue with using the mock DB is the Mock will always return a response if the right input is provided, and cannot assert that the SQL query is working as it should.
e.g
Design
We want to create a Postgres database and then test the database functions to assert that the SQL queries work as they should.
We can achieve this using these two approaches
Programmatic Postgres Database Creation
Pros
go test ./....
spins up the database before running the test.Cons
Docker Compose Database Creation
Pros
Cons
REFERENCES
Acceptance Criteria
Here is an example backend test
The text was updated successfully, but these errors were encountered: