stakwork/sphinx-tribes

Refactor TestMetricsBountiesProviders To Use A Real Postgres DB For The Test

Closed this issue · 4 comments

Context

Currently, we test the TestMetricsBountiesProviders with a mock database.

Design

We want to refactor the tests to use a real Postgres DB spun up on docker, follow the instructions in the Unit Testing section of README.md to run spin up a database in docker, and change the test_config.go configuration.

After spinning up the Postgres DB container:

  • Add this line of code on the first line and second of the test function TestMetricsBountiesProviders to connect to the Test DB
    teardownSuite := SetupSuite(t)
    defer teardownSuite(t)

The test will throw a dereference error if not connected to a DB.

  • Remove all mock database functions in the TestMetricsBountiesProviders test cases e.g. mockDb.On("GetBountiesByDateRangeCount", dateRange, req).Return(int64(100)).Once().
  • Change the database parameter in mh := NewMetricHandler(mockDb) to mh := NewMetricHandler(db.TestDb)
  • Delete all bounties from the TestDb
  • Create two new persons using this db function db.TestDB.CreateOrEditPerson(person) e.g
person := db.Person{
    Uuid:         "your_uuid",
    OwnerPubKey:  "your_pubkey",
    OwnerAlias:   "your_owner",
    UniqueName:   "your_user",
    Description:  "Your user",
    Tags:         pq.StringArray{},
    Extras:       db.PropertyMap{},
    GithubIssues: db.PropertyMap{},
}
db.TestDB.CreateOrEditPerson(person)
  • Create 4 bounties using this db function db.TestDb.CreateOrEditBounty(), the bounties OwnerID should be the public keys of the persons created 2 each e.g
now := time.Now()
bounty  :=  db.NewBounty{
	Type:          "coding",
	Title:         "Bounty With ID",
	Description:   "Bounty ID Description",
	WorkspaceUuid: "",
	Assignee:      "",
	OwnerID:       person.OwnerPubKey,
	Show:          true,
        Created: &now,
        Paid: true
}
db.TestDb.CreateOrEditBounty(bounty)
bounty2  :=  db.NewBounty{
	Type:          "coding",
	Title:         "Bounty With ID",
	Description:   "Bounty ID Description",
	WorkspaceUuid: "",
	Assignee:      "",
	OwnerID:       person.OwnerPubKey,
	Show:          true,
        Created: &now,
}
db.TestDb.CreateOrEditBounty(bounty2)

The first bounty and the last bounties should be 30 days apart, use Golang Date manipulation to achieve this.

  • Change the validJson "start_date" and "end_date" values to be the Created date of the first bounty and the Created date of the last bounty e.g.
	validJson := []byte(`{"start_date":  bounty.Created, "end_date":  bounty4.Created}`)
  • Get all bounty providers using the db.TestDB.GetBountiesProviders(request, r) function
  • Assert that the API call response matches the value returned from the DB.
  • Assert that the Providers returned are equal to the persons created

Assertions

  • Assert that the API call returned value matches the bounties fetched from the DB.

Acceptance Criteria

  • Do not delete any of the existing test cases on the TestMetricsBountiesProviders
  • All test cases after the TestMetricsBountiesProviders refactor passes
  • The Refactoring of TestMetricsBountiesProviders should not break existing test flows.
  • I have rebased and tested locally before submitting my PR
  • I can submit a PR within 1 day of taking the bounty

Here is an example Real DB Test

@elraphty assign me next one

@elraphty please this time assign me

@MahtabBukhari

@elraphty Assign me next one?