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

Refactor TestGetPeopleBySearch To Use A Real Postgres DB For The Test #1716

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading