Skip to content

Commit

Permalink
added better unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad_baderkhan committed Apr 23, 2023
1 parent 0a0a0ff commit a6ee714
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 18 deletions.
1 change: 1 addition & 0 deletions v1/easygin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func To[T IRequest](inputFunc func(reqDTO T) *Response, bindFrom ...string) gin.
val, exists := ctx.Get(cKey)
if !exists {
fmt.Println("warning!! context key == " + cKey + " does not exist")
continue
}
err := copier.Copy(&dtoCastFrom, val)
if err != nil {
Expand Down
125 changes: 107 additions & 18 deletions v1/easygin/gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func MockInboundRequest(m *MockRequestInput) *gin.Context {
}

type dummyJSONOnly struct {
Name string `json:"name" form:"name" uri:"name"`
Name string `json:"name" form:"name" uri:"name"`
Other int `json:"other" form:"other" uri:"other" binding:"ne=10"`
}

func (d dummyJSONOnly) Validate() error {
Expand Down Expand Up @@ -178,6 +179,28 @@ func TestTo(t *testing.T) {
assert.Equal(t, `"baderkha"`, w.Body.String())
}

// no mode specify , failure gin validation , not custom // should error
{
w := httptest.NewRecorder()
ctx := GetTestGinContext(w)
gCtx := MockInboundRequest(&MockRequestInput{
Ctx: ctx,
HTTPMethod: http.MethodGet,
QueryParams: map[string][]string{
"name": {
"baderkha",
},
"other": {
"10",
},
},
})
ginFunc := To(handler)
ginFunc(gCtx)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.MatchRegex(t, w.Body.String(), "Error")
}

// query mode specify // ok , should output
{
w := httptest.NewRecorder()
Expand Down Expand Up @@ -220,6 +243,30 @@ func TestTo(t *testing.T) {
assert.Equal(t, `"baderkha"`, w.Body.String())
}

// no mode specify , failure gin validation , not custom // should error
{
w := httptest.NewRecorder()
ctx := GetTestGinContext(w)
gCtx := MockInboundRequest(&MockRequestInput{
Ctx: ctx,
HTTPMethod: http.MethodGet,
PathParams: []gin.Param{
{
Key: "name",
Value: "baderkha",
},
{
Key: "other",
Value: "10",
},
},
})
ginFunc := To(handler)
ginFunc(gCtx)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.MatchRegex(t, w.Body.String(), "Error")
}

// uri mode specify // ok , should output
{
w := httptest.NewRecorder()
Expand All @@ -244,24 +291,66 @@ func TestTo(t *testing.T) {
// test case 4 set / get struct binding
{

w := httptest.NewRecorder()
ctx := GetTestGinContext(w)
gCtx := MockInboundRequest(&MockRequestInput{
Ctx: ctx,
HTTPMethod: http.MethodGet,
QueryParams: make(url.Values),
KeyValueMiddlewareSet: map[string]any{
"some_mwr": struct {
Name string
}{
Name: "baderkha",
// regular struct , this should work and we should have the value passed to our dto handler
{
w := httptest.NewRecorder()
ctx := GetTestGinContext(w)
gCtx := MockInboundRequest(&MockRequestInput{
Ctx: ctx,
HTTPMethod: http.MethodGet,
QueryParams: make(url.Values),
KeyValueMiddlewareSet: map[string]any{
"some_mwr": struct {
Name string
}{
Name: "baderkha",
},
},
},
})
ginFunc := To(handler, BindContext("some_mwr"))
ginFunc(gCtx)
assert.Equal(t, 305, w.Code)
assert.Equal(t, `"baderkha"`, w.Body.String())
})
ginFunc := To(handler, BindContext("some_mwr"))
ginFunc(gCtx)
assert.Equal(t, 305, w.Code)
assert.Equal(t, `"baderkha"`, w.Body.String())
}

// regular struct key does not exist, this should work but the value will be empty
{
w := httptest.NewRecorder()
ctx := GetTestGinContext(w)
gCtx := MockInboundRequest(&MockRequestInput{
Ctx: ctx,
HTTPMethod: http.MethodGet,
QueryParams: make(url.Values),
KeyValueMiddlewareSet: map[string]any{
"some_mwr": struct {
Name string
}{
Name: "baderkha",
},
},
})
ginFunc := To(handler, BindContext("some_mwr2"))
ginFunc(gCtx)
assert.Equal(t, http.StatusBadRequest, w.Code)
}

// map should not bind and should throw an error
{
w := httptest.NewRecorder()
ctx := GetTestGinContext(w)
gCtx := MockInboundRequest(&MockRequestInput{
Ctx: ctx,
HTTPMethod: http.MethodGet,
QueryParams: make(url.Values),
KeyValueMiddlewareSet: map[string]any{
"some_mwr": nil,
},
})
ginFunc := To(handler, BindContext("some_mwr"))
ginFunc(gCtx)
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.NotEqual(t, `"baderkha"`, w.Body.String())
}

}

Expand Down

0 comments on commit a6ee714

Please sign in to comment.