techschool / simplebank

Backend master class: build a simple bank service in Go
MIT License
5.33k stars 961 forks source link

Have an issue with list params not matching #90

Closed CodeNinjaUG closed 1 year ago

CodeNinjaUG commented 1 year ago

   //creating random user. ///test function 
    user,_:= randomUser(t)
    n := 5
    //creating a slice of index n
    airlines := make([]db.Airline, n)
    for i:=0; i<n; i++ {
         airlines[i] = RandomAirline(t)
    }
    //query
    type Query struct {
        pageID int
        pageSize int
    }
    test_cases := []struct{
         name string
         query  Query
         setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker)
         buildStubs func(store *mockdb.MockStore) 
         checkResponse func(recorder *httptest.ResponseRecorder)
        }{
            {
               name : "OK",
               query: Query {
                   pageID: 1,
                   pageSize: n,
               },
               setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
                   addAuthorization(t, request, tokenMaker, authorizationBearerKey, user.Email, time.Minute)
               },
               buildStubs: func(store *mockdb.MockStore) {
                    arg := db.ListAirlinesParams{
                          Limit: int32(n),
                          Offset: 0,
                    }
                    store.EXPECT().ListAirlines(gomock.Any(), gomock.Eq(arg)).Times(1).Return(airlines, nil)
               },
               checkResponse: func(recorder *httptest.ResponseRecorder) {
                    require.Equal(t, http.StatusOK, recorder.Code)
               },
            },
        }

///live functionality

type listAirlinesRequest struct {
     pageID int32 `form:"page_id" binding:"required,min=1"`
     pageSize int32`form:"page_size" binding:"required,min=5,max=10"`
}

func (server *Server) GetAirlines(ctx *gin.Context){
        var req listAirlinesRequest
        if err := ctx.ShouldBindQuery(&req); err != nil{ 
                  ctx.JSON(http.StatusBadRequest, errorResponse(err))
                  return
        }
        arg := db.ListAirlinesParams{
                  Limit: req.pageSize,
                  Offset: (req.pageID - 1) * req.pageSize,
        }
        airlines, err := server.store.ListAirlines(ctx, arg)
        if err != nil {
            ctx.JSON(http.StatusInternalServerError, errorResponse(err))
            return
        }
        ctx.JSON(http.StatusOK, airlines)

  /////error
  --- FAIL: TestListAirlines (0.06s)
    --- FAIL: TestListAirlines/OK (0.00s)
        /Applications/XAMPP/xamppfiles/htdocs/SPS-Web/api/airline.go:92: Unexpected call to *mockdb.MockStore.ListAirlines([0xc000124600 {0 0}]) at /Applications/XAMPP/xamppfiles/htdocs/SPS-Web/api/airline.go:92 because: 
            expected call at /Applications/XAMPP/xamppfiles/htdocs/SPS-Web/api/airline_test.go:142 doesn't match the argument at index 1.
            Got: {0 0} (db.ListAirlinesParams)
            Want: is equal to {5 0} (db.ListAirlinesParams)
        /Applications/XAMPP/xamppfiles/htdocs/SPS-Web/api/controller.go:269: missing call(s) to *mockdb.MockStore.ListAirlines(is anything, is equal to {5 0} (db.ListAirlinesParams)) /Applications/XAMPP/xamppfiles/htdocs/SPS-Web/api/airline_test.go:142
        /Applications/XAMPP/xamppfiles/htdocs/SPS-Web/api/controller.go:269: aborting test due to missing call(s)
FAIL
FAIL    github.com/thinkIt-africa/SPS-Web/api   1.250s. ```