Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Mock Database With Real Postgres Database For Integration / Unit Testing #1687

Closed
4 tasks
elraphty opened this issue Jun 11, 2024 · 0 comments
Closed
4 tasks
Assignees
Labels

Comments

@elraphty
Copy link
Contributor

elraphty commented Jun 11, 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

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

  1. Create a database programmatically, and drop the database after all the tests are processed.
  2. 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.

REFERENCES

Acceptance Criteria

  • I've tested on Chrome
  • I've created a test that...
  • I have rebased and tested locally before submitting my PR
  • I can submit a PR within 4 days of taking the bounty

Here is an example backend test

@elraphty elraphty self-assigned this Jun 11, 2024
@elraphty 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: ✅ Done
Development

No branches or pull requests

1 participant