Skip to content

Commit

Permalink
Refactor TestGetPeopleBySearch To Use A Real Postgres DB For The Test
Browse files Browse the repository at this point in the history
  • Loading branch information
MahtabBukhari committed Jun 20, 2024
1 parent 20e68a6 commit abbfadb
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions handlers/people_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,34 +269,64 @@ func TestDeletePerson(t *testing.T) {
}

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

person := db.Person{
ID: 102,
Uuid: "perosn_102_uuid",
OwnerAlias: "person102",
UniqueName: "person102",
OwnerPubKey: "person_102_pubkey",
PriceToMeet: 0,
Description: "This is test user 102",
Tags: pq.StringArray{},
Extras: db.PropertyMap{},
GithubIssues: db.PropertyMap{},
}
person2 := db.Person{
ID: 103,
Uuid: "perosn_103_uuid",
OwnerAlias: "person103",
UniqueName: "person103",
OwnerPubKey: "person_103_pubkey",
PriceToMeet: 0,
Description: "This is test user 103",
Tags: pq.StringArray{},
Extras: db.PropertyMap{},
GithubIssues: db.PropertyMap{},
}
db.TestDB.CreateOrEditPerson(person)
db.TestDB.CreateOrEditPerson(person2)

t.Run("should return users that match the search text", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(pHandler.GetPeopleBySearch)
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, "/search?search=John", nil)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/search?search="+person.OwnerAlias, nil)
assert.NoError(t, err)

mockDb.On("GetPeopleBySearch", mock.Anything).Return(expectedPeople)
fetchedPerson := db.TestDB.GetPerson(person.ID)
fetchedPerson2 := db.TestDB.GetPerson(person2.ID)

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, person, fetchedPerson)
assert.EqualValues(t, person2, fetchedPerson2)
assert.EqualValues(t, expectedPeople, returnedPeople)
mockDb.AssertExpectations(t)
})

t.Run("should return an empty search result when no user matches the search text", func(t *testing.T) {
mockDb.ExpectedCalls = nil
rr := httptest.NewRecorder()
handler := http.HandlerFunc(pHandler.GetPeopleBySearch)
expectedPeople := []db.Person{}
Expand All @@ -305,15 +335,13 @@ func TestGetPeopleBySearch(t *testing.T) {
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/search?search=user not matched", nil)
assert.NoError(t, err)

mockDb.On("GetPeopleBySearch", mock.Anything).Return(expectedPeople)
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, expectedPeople, returnedPeople)
mockDb.AssertExpectations(t)
})
}

Expand Down

0 comments on commit abbfadb

Please sign in to comment.