Skip to content

Commit

Permalink
Merge pull request #1714 from AbdulWahab3181/Refactor-TestGetPersonBy…
Browse files Browse the repository at this point in the history
…Id-UT

Refactor TestGetPersonById To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty authored Jun 20, 2024
2 parents 2069cb3 + f972b61 commit 20e68a6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
go.mongodb.org/mongo-driver v1.7.5 // indirect
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect
golang.org/x/oauth2 v0.15.0
golang.org/x/tools/cmd/cover v0.1.0-deprecated // indirect
google.golang.org/api v0.153.0
gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
gorm.io/driver/postgres v1.5.4
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3033,12 +3033,15 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
golang.org/x/tools v0.5.1-0.20230111220935-a7f7db3f17fc/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg=
golang.org/x/tools v0.10.0/go.mod h1:UJwyiVBsOA2uwvK/e5OY3GTpDUJriEd+/YlqAwLPmyM=
golang.org/x/tools/cmd/cover v0.1.0-deprecated h1:Rwy+mWYz6loAF+LnG1jHG/JWMHRMMC2/1XX3Ejkx9lA=
golang.org/x/tools/cmd/cover v0.1.0-deprecated/go.mod h1:hMDiIvlpN1NoVgmjLjUJE9tMHyxHjFX7RuQ+rW12mSA=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
29 changes: 19 additions & 10 deletions handlers/people_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
mocks "github.com/stakwork/sphinx-tribes/mocks"
Expand Down Expand Up @@ -181,33 +182,43 @@ func TestCreateOrEditPerson(t *testing.T) {
}

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

pHandler := NewPeopleHandler(db.TestDB)

t.Run("successful retrieval", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(pHandler.GetPersonById)
person := db.Person{
ID: 1,
Uuid: "test-uuid",
OwnerPubKey: "owner-pub-key",
OwnerAlias: "owner",
ID: 100,
Uuid: "perosn_1_uuid",
OwnerAlias: "person",
UniqueName: "person",
OwnerPubKey: "person_1_pubkey",
PriceToMeet: 0,
Description: "this is test user 1",
Tags: pq.StringArray{},
Extras: db.PropertyMap{},
GithubIssues: db.PropertyMap{},
}

rctx := chi.NewRouteContext()
rctx.URLParams.Add("id", strconv.Itoa(int(person.ID)))
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/person", nil)
assert.NoError(t, err)

mockDb.On("GetPerson", mock.Anything).Return(person).Once()
db.TestDB.CreateOrEditPerson(person)
fetchedPerson := db.TestDB.GetPerson(person.ID)

handler.ServeHTTP(rr, req)

var returnedPerson db.Person
err = json.Unmarshal(rr.Body.Bytes(), &returnedPerson)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.EqualValues(t, person, returnedPerson)
mockDb.AssertExpectations(t)
assert.EqualValues(t, person, fetchedPerson)
})

t.Run("person not found", func(t *testing.T) {
Expand All @@ -219,11 +230,9 @@ func TestGetPersonById(t *testing.T) {
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/person", nil)
assert.NoError(t, err)

mockDb.On("GetPerson", mock.Anything).Return(db.Person{}).Once()
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
mockDb.AssertExpectations(t)
})
}

Expand Down

0 comments on commit 20e68a6

Please sign in to comment.