Skip to content

Commit

Permalink
Merge pull request #1728 from aliraza556/Refactor-TestGetListedPeople-UT
Browse files Browse the repository at this point in the history
Refactor TestGetListedPeople To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty authored Jun 23, 2024
2 parents 1f27f1c + 64bc7fd commit dfd7266
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 23 deletions.
4 changes: 4 additions & 0 deletions db/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ func InitTestDB() {
}
}
}

func CleanDB() {
TestDB.db.Exec("DELETE FROM people")
}
101 changes: 78 additions & 23 deletions handlers/people_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,78 +415,133 @@ func TestGetPeopleBySearch(t *testing.T) {
}

func TestGetListedPeople(t *testing.T) {
mockDb := mocks.NewDatabase(t)
pHandler := NewPeopleHandler(mockDb)
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

pHandler := NewPeopleHandler(db.TestDB)

db.CleanDB()

person := db.Person{
ID: 101,
Uuid: "person_101_uuid",
OwnerAlias: "person101",
UniqueName: "person101",
OwnerPubKey: "person_101_pubkey",
PriceToMeet: 0,
Description: "this is test user 1",
Unlisted: true,
Tags: pq.StringArray{},
GithubIssues: db.PropertyMap{},
Extras: db.PropertyMap{"coding_languages": "Typescript"},
}
person2 := db.Person{
ID: 102,
Uuid: "person_102_uuid",
OwnerAlias: "person102",
UniqueName: "person102",
OwnerPubKey: "person_102_pubkey",
PriceToMeet: 0,
Description: "This is test user 2",
Unlisted: false,
Tags: pq.StringArray{},
GithubIssues: db.PropertyMap{},
Extras: db.PropertyMap{"coding_languages": "Golang"},
}
person3 := db.Person{
ID: 103,
Uuid: "person_103_uuid",
OwnerAlias: "person103",
UniqueName: "person103",
OwnerPubKey: "person_103_pubkey",
PriceToMeet: 0,
Description: "This is test user 3",
Unlisted: false,
Tags: pq.StringArray{},
GithubIssues: db.PropertyMap{},
Extras: db.PropertyMap{"coding_languages": "Lightning"},
}

db.TestDB.CreateOrEditPerson(person)
db.TestDB.CreateOrEditPerson(person2)
db.TestDB.CreateOrEditPerson(person3)

fetchedPerson := db.TestDB.GetPerson(person2.ID)
fetchedPerson2 := db.TestDB.GetPerson(person3.ID)

t.Run("should return all listed users", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(pHandler.GetListedPeople)
expectedPeople := []db.Person{
{ID: 1, Uuid: "uuid1", OwnerPubKey: "pubkey1", OwnerAlias: "John Doe"},
{ID: 2, Uuid: "uuid2", OwnerPubKey: "pubkey2", OwnerAlias: "John Smith"},
}

rctx := chi.NewRouteContext()
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/?page=1&limit=10", nil)
assert.NoError(t, err)

mockDb.On("GetListedPeople", mock.Anything).Return(expectedPeople)
expectedPeople := []db.Person{
fetchedPerson,
fetchedPerson2,
}

handler.ServeHTTP(rr, req)

var returnedPeople []db.Person
err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.EqualValues(t, person2, fetchedPerson)
assert.EqualValues(t, person3, fetchedPerson2)
assert.EqualValues(t, expectedPeople, returnedPeople)
mockDb.AssertExpectations(t)
})

t.Run("should return only users that match a search text when a search is added to the URL query", func(t *testing.T) {
mockDb.ExpectedCalls = nil
rr := httptest.NewRecorder()
handler := http.HandlerFunc(pHandler.GetListedPeople)
expectedPeople := []db.Person{
{ID: 1, Uuid: "uuid1", OwnerPubKey: "pubkey1", OwnerAlias: "John Doe"},
{ID: 2, Uuid: "uuid2", OwnerPubKey: "pubkey2", OwnerAlias: "John Smith"},
}

rctx := chi.NewRouteContext()
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/?page=1&limit=10&search=John", nil)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/?page=1&limit=10&search="+person2.OwnerAlias, nil)
assert.NoError(t, err)

mockDb.On("GetListedPeople", mock.Anything).Return(expectedPeople)
expectedPeople := []db.Person{
fetchedPerson,
}

handler.ServeHTTP(rr, req)

var returnedPeople []db.Person
err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.EqualValues(t, person2, fetchedPerson)
assert.EqualValues(t, expectedPeople, returnedPeople)
mockDb.AssertExpectations(t)
})

t.Run("should return only users that match a skill set when languages are passed to the URL query", func(t *testing.T) {
mockDb.ExpectedCalls = nil
rr := httptest.NewRecorder()
handler := http.HandlerFunc(pHandler.GetListedPeople)
expectedPeople := []db.Person{
{ID: 1, Uuid: "uuid1", OwnerPubKey: "pubkey1", OwnerAlias: "John Doe"},
}

rctx := chi.NewRouteContext()
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/?page=1&limit=10&languages=typescript", nil)
req, err := http.NewRequestWithContext(
context.WithValue(context.Background(), chi.RouteCtxKey, rctx),
http.MethodGet,
"page=1&limit=10&languages="+person2.Extras["coding_languages"].(string),
nil,
)
assert.NoError(t, err)

mockDb.On("GetListedPeople", mock.Anything).Return(expectedPeople)
expectedPeople := []db.Person{
fetchedPerson,
}

handler.ServeHTTP(rr, req)

var returnedPeople []db.Person
err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.EqualValues(t, person2, fetchedPerson)
assert.EqualValues(t, expectedPeople, returnedPeople)
mockDb.AssertExpectations(t)
})

}

func TestGetPersonByUuid(t *testing.T) {
Expand Down

0 comments on commit dfd7266

Please sign in to comment.