From db7d45c52085a9e0d84231c27e0549e2a468f53e Mon Sep 17 00:00:00 2001 From: MuhammadUmer44 Date: Thu, 20 Jun 2024 21:04:13 +0500 Subject: [PATCH] Refactored TestGetPersonByPuKey UT --- handlers/people_test.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/handlers/people_test.go b/handlers/people_test.go index 6fb59861f..f043c40be 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -20,33 +20,39 @@ import ( ) func TestGetPersonByPuKey(t *testing.T) { - mockDb := mocks.NewDatabase(t) - pHandler := NewPeopleHandler(mockDb) + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + + pHandler := NewPeopleHandler(db.TestDB) t.Run("should return person if present in db", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(pHandler.GetPersonByPubkey) person := db.Person{ - ID: 1, - Uuid: uuid.New().String(), - OwnerPubKey: "person-pub-key", - OwnerAlias: "owner", - UniqueName: "test_user", - Description: "test user", + ID: 104, + Uuid: "person_104_uuid", + OwnerPubKey: "person_104_pubkey", + OwnerAlias: "owner", + UniqueName: "test_user", + Description: "test user", + Tags: pq.StringArray{}, + Extras: db.PropertyMap{}, + GithubIssues: db.PropertyMap{}, } + db.TestDB.CreateOrEditPerson(person) + rctx := chi.NewRouteContext() - rctx.URLParams.Add("pubkey", "person-pub-key") - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/person/person-pub-key", nil) + rctx.URLParams.Add("pubkey", "person_104_pubkey") + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/person/person_104_pubkey", nil) if err != nil { t.Fatal(err) } - mockDb.On("GetPersonByPubkey", "person-pub-key").Return(person).Once() + handler.ServeHTTP(rr, req) var returnedPerson db.Person _ = json.Unmarshal(rr.Body.Bytes(), &returnedPerson) assert.Equal(t, http.StatusOK, rr.Code) assert.EqualValues(t, person, returnedPerson) - mockDb.AssertExpectations(t) }) }