diff --git a/handlers/people_test.go b/handlers/people_test.go index fb5b70097..d62ba6c81 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -275,34 +275,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{} @@ -311,7 +341,6 @@ 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 @@ -319,7 +348,6 @@ func TestGetPeopleBySearch(t *testing.T) { assert.NoError(t, err) assert.Equal(t, http.StatusOK, rr.Code) assert.EqualValues(t, expectedPeople, returnedPeople) - mockDb.AssertExpectations(t) }) }