Best Mock code snippet using mock_pkg.EXPECT
request_handler_test.go
Source:request_handler_test.go
...35 mockController := gomock.NewController(t)36 defer mockController.Finish()37 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)38 dummyToken := "ohlebeautoken"39 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{40 TokenSpec: types.TokenSpec{41 Token: dummyToken,42 ExpectedRateLimit: 1000,43 },44 RemainingCalls: 1000,45 }, nil)46 request, err := githubUpstream.buildRequest(githubUpstream.withResponseHeaders(rateLimitHeader, "1000"))47 require.NoError(t, err)48 response, body, ok := assertHandleRequestSuccessful(t, NewRequestHandler(githubUpstream.urlStruct(), tokenPool), request, http.StatusOK)49 require.True(t, ok)50 assert.Equal(t, "pong", body)51 assertAuthHeaderMatchesToken(t, dummyToken, response.Header.Get(receivedAuthHeader))52 })53 t.Run("it updates the token pool if the token's rate limit is different than expected", func(t *testing.T) {54 mockController := gomock.NewController(t)55 defer mockController.Finish()56 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)57 dummyToken := "ohlebeautoken"58 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{59 TokenSpec: types.TokenSpec{60 Token: dummyToken,61 ExpectedRateLimit: 100,62 },63 RemainingCalls: 100,64 }, nil)65 tokenPool.EXPECT().UpdateTokenRateLimit(dummyToken, 1000)66 request, err := githubUpstream.buildRequest(githubUpstream.withResponseHeaders(rateLimitHeader, "1000"))67 require.NoError(t, err)68 response, body, ok := assertHandleRequestSuccessful(t, NewRequestHandler(githubUpstream.urlStruct(), tokenPool), request, http.StatusOK)69 require.True(t, ok)70 assert.Equal(t, "pong", body)71 assertAuthHeaderMatchesToken(t, dummyToken, response.Header.Get(receivedAuthHeader))72 })73 t.Run("it periodically updates the token pool about the token's usage, based on the response headers", func(t *testing.T) {74 mockController := gomock.NewController(t)75 defer mockController.Finish()76 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)77 dummyToken := "ohlebeautoken"78 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{79 TokenSpec: types.TokenSpec{80 Token: dummyToken,81 ExpectedRateLimit: 1000,82 },83 RemainingCalls: 1000,84 }, nil)85 remainingCallsStart := 100086 remainingCallsStop := 79087 for remainingCallsThreshold := remainingCallsStart - remainingCallsReportingInterval; remainingCallsThreshold > remainingCallsStop; remainingCallsThreshold -= remainingCallsReportingInterval {88 tokenPool.EXPECT().UpdateTokenUsage(dummyToken, remainingCallsThreshold)89 }90 handler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)91 for remainingCalls := remainingCallsStart; remainingCalls > remainingCallsStop; remainingCalls -= 10 {92 request, err := githubUpstream.buildRequest(githubUpstream.withResponseHeaders(remainingCallsHeader, strconv.Itoa(remainingCalls)))93 require.NoError(t, err)94 response, body, ok := assertHandleRequestSuccessful(t, handler, request, http.StatusOK)95 require.True(t, ok)96 assert.Equal(t, "pong", body)97 assertAuthHeaderMatchesToken(t, dummyToken, response.Header.Get(receivedAuthHeader))98 }99 })100 t.Run("if the response headers do not say how many calls are remaining, it does its best effort to keep track of it", func(t *testing.T) {101 mockController := gomock.NewController(t)102 defer mockController.Finish()103 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)104 dummyToken := "ohlebeautoken"105 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{106 TokenSpec: types.TokenSpec{107 Token: dummyToken,108 ExpectedRateLimit: 111,109 },110 RemainingCalls: 111,111 }, nil)112 tokenPool.EXPECT().UpdateTokenUsage(dummyToken, 61)113 tokenPool.EXPECT().UpdateTokenUsage(dummyToken, 11)114 tokenPool.EXPECT().UpdateTokenUsage(dummyToken, 0)115 handler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)116 for i := 0; i < 111; i++ {117 request, err := githubUpstream.buildRequest()118 require.NoError(t, err)119 response, body, ok := assertHandleRequestSuccessful(t, handler, request, http.StatusOK)120 require.True(t, ok)121 assert.Equal(t, "pong", body)122 assertAuthHeaderMatchesToken(t, dummyToken, response.Header.Get(receivedAuthHeader))123 }124 })125 t.Run("when the token has been exhausted based on the response headers, it requests a new one from the backend", func(t *testing.T) {126 mockController := gomock.NewController(t)127 defer mockController.Finish()128 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)129 dummyToken := "ohlebeautoken"130 nextToken := "encoreplusbeau"131 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{132 TokenSpec: types.TokenSpec{133 Token: dummyToken,134 ExpectedRateLimit: 10,135 },136 RemainingCalls: 10,137 }, nil)138 tokenPool.EXPECT().UpdateTokenUsage(dummyToken, 0)139 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{140 TokenSpec: types.TokenSpec{141 Token: nextToken,142 ExpectedRateLimit: 10,143 },144 RemainingCalls: 10,145 }, nil)146 handler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)147 for remainingCalls := 10; remainingCalls > -1; remainingCalls -= 1 {148 request, err := githubUpstream.buildRequest(githubUpstream.withResponseHeaders(remainingCallsHeader, strconv.Itoa(remainingCalls)))149 require.NoError(t, err)150 response, body, ok := assertHandleRequestSuccessful(t, handler, request, http.StatusOK)151 require.True(t, ok)152 assert.Equal(t, "pong", body)153 assertAuthHeaderMatchesToken(t, dummyToken, response.Header.Get(receivedAuthHeader))154 }155 request, err := githubUpstream.buildRequest(githubUpstream.withResponseHeaders(remainingCallsHeader, "10"))156 require.NoError(t, err)157 response, body, ok := assertHandleRequestSuccessful(t, handler, request, http.StatusOK)158 require.True(t, ok)159 assert.Equal(t, "pong", body)160 assertAuthHeaderMatchesToken(t, nextToken, response.Header.Get(receivedAuthHeader))161 })162 t.Run("if the response headers do not say how many calls are remaining, it does its best effort to keep track of it and requests a new one when exhausted", func(t *testing.T) {163 mockController := gomock.NewController(t)164 defer mockController.Finish()165 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)166 dummyToken := "ohlebeautoken"167 nextToken := "encoreplusbeau"168 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{169 TokenSpec: types.TokenSpec{170 Token: dummyToken,171 ExpectedRateLimit: 10,172 },173 RemainingCalls: 10,174 }, nil)175 tokenPool.EXPECT().UpdateTokenUsage(dummyToken, 0)176 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{177 TokenSpec: types.TokenSpec{178 Token: nextToken,179 ExpectedRateLimit: 10,180 },181 RemainingCalls: 10,182 }, nil)183 handler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)184 for remainingCalls := 10; remainingCalls > 0; remainingCalls -= 1 {185 request, err := githubUpstream.buildRequest()186 require.NoError(t, err)187 response, body, ok := assertHandleRequestSuccessful(t, handler, request, http.StatusOK)188 require.True(t, ok)189 assert.Equal(t, "pong", body)190 assertAuthHeaderMatchesToken(t, dummyToken, response.Header.Get(receivedAuthHeader))191 }192 request, err := githubUpstream.buildRequest()193 require.NoError(t, err)194 response, body, ok := assertHandleRequestSuccessful(t, handler, request, http.StatusOK)195 require.True(t, ok)196 assert.Equal(t, "pong", body)197 assertAuthHeaderMatchesToken(t, nextToken, response.Header.Get(receivedAuthHeader))198 })199 t.Run("if a token gets unexpectedly exhausted, it retries with another token", func(t *testing.T) {200 mockController := gomock.NewController(t)201 defer mockController.Finish()202 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)203 dummyToken := "ohlebeautoken"204 nextToken := "encoreplusbeau"205 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{206 TokenSpec: types.TokenSpec{207 Token: dummyToken,208 ExpectedRateLimit: 1000,209 },210 RemainingCalls: 1000,211 }, nil)212 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{213 TokenSpec: types.TokenSpec{214 Token: nextToken,215 ExpectedRateLimit: 1000,216 },217 RemainingCalls: 1000,218 }, nil)219 githubUpstream.resetRequestsCount()220 githubUpstream.addResponse(http.StatusForbidden, `{"message": "API rate limit exceeded"}`)221 request, err := githubUpstream.buildRequest()222 require.NoError(t, err)223 response, body, ok := assertHandleRequestSuccessful(t, NewRequestHandler(githubUpstream.urlStruct(), tokenPool), request, http.StatusOK)224 require.True(t, ok)225 assert.Equal(t, "pong", body)226 assertAuthHeaderMatchesToken(t, nextToken, response.Header.Get(receivedAuthHeader))227 assert.Equal(t, uint32(2), githubUpstream.requestsCount)228 })229 t.Run("it doesn't retry failed requests that did not fail because of throttling, and does not request new tokens", func(t *testing.T) {230 mockController := gomock.NewController(t)231 defer mockController.Finish()232 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)233 dummyToken := "ohlebeautoken"234 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{235 TokenSpec: types.TokenSpec{236 Token: dummyToken,237 ExpectedRateLimit: 1000,238 },239 RemainingCalls: 1000,240 }, nil)241 githubUpstream.resetRequestsCount()242 requestHandler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)243 // wrong status code244 request, err := githubUpstream.buildRequest(githubUpstream.withResponseStatusCode(http.StatusUnauthorized))245 require.NoError(t, err)246 _, body, ok := assertHandleRequestSuccessful(t, requestHandler, request, http.StatusUnauthorized)247 require.True(t, ok)248 assert.Equal(t, "pong", body)249 assert.Equal(t, uint32(1), githubUpstream.requestsCount)250 // wrong body: not a JSON251 responseBody := "dummy error"252 request, err = githubUpstream.buildRequest(githubUpstream.withResponseStatusCode(http.StatusForbidden),253 githubUpstream.withResponseBody(responseBody))254 require.NoError(t, err)255 _, body, ok = assertHandleRequestSuccessful(t, requestHandler, request, http.StatusForbidden)256 require.True(t, ok)257 assert.Equal(t, responseBody, body)258 assert.Equal(t, uint32(2), githubUpstream.requestsCount)259 // wrong body: not the expected message260 responseBody = `{"message": "dunno"}`261 request, err = githubUpstream.buildRequest(githubUpstream.withResponseStatusCode(http.StatusForbidden),262 githubUpstream.withResponseBody(responseBody))263 require.NoError(t, err)264 _, body, ok = assertHandleRequestSuccessful(t, requestHandler, request, http.StatusForbidden)265 require.True(t, ok)266 assert.Equal(t, responseBody, body)267 assert.Equal(t, uint32(3), githubUpstream.requestsCount)268 })269 t.Run("it does not do anything to requests going to other hosts than Github's API", func(t *testing.T) {270 mockController := gomock.NewController(t)271 defer mockController.Finish()272 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)273 request, err := otherUpstream.buildRequest()274 require.NoError(t, err)275 otherUpstream.resetRequestsCount()276 response, body, ok := assertHandleRequestSuccessful(t, NewRequestHandler(githubUpstream.urlStruct(), tokenPool), request, http.StatusOK)277 require.True(t, ok)278 assert.Equal(t, "pong", body)279 assert.Equal(t, "", response.Header.Get(receivedAuthHeader))280 assert.Equal(t, uint32(1), otherUpstream.requestsCount)281 })282 t.Run("for requests not going to Github, it correctly copies the request and response's bodies back and forth", func(t *testing.T) {283 mockController := gomock.NewController(t)284 defer mockController.Finish()285 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)286 reqBody := "t'as de beaux yeux tu sais"287 request, err := otherUpstream.buildRequest(otherUpstream.withRequestPath("/echo"),288 otherUpstream.withRequestBody(reqBody))289 require.NoError(t, err)290 otherUpstream.resetRequestsCount()291 response, respBody, ok := assertHandleRequestSuccessful(t, NewRequestHandler(githubUpstream.urlStruct(), tokenPool), request, http.StatusOK)292 require.True(t, ok)293 assert.Equal(t, reqBody, respBody)294 assert.Equal(t, "", response.Header.Get(receivedAuthHeader))295 assert.Equal(t, uint32(1), otherUpstream.requestsCount)296 })297 for _, poolErrorTestCase := range []struct {298 name string299 poolError error300 expectedHandlerErrorMessage string301 }{302 {303 name: "if the pool doesn't have any tokens, it bubbles up the error gracefully",304 expectedHandlerErrorMessage: "no available API token",305 },306 {307 name: "if the pool errors out, it bubbles up the error gracefully",308 poolError: errors.New("dummy pool error"),309 expectedHandlerErrorMessage: "dummy pool error",310 },311 } {312 t.Run(poolErrorTestCase.name, func(t *testing.T) {313 mockController := gomock.NewController(t)314 defer mockController.Finish()315 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)316 tokenPool.EXPECT().CheckOutToken().Return(nil, poolErrorTestCase.poolError)317 request, err := githubUpstream.buildRequest()318 require.NoError(t, err)319 response, err := NewRequestHandler(githubUpstream.urlStruct(), tokenPool).ProxyRequest(request)320 if assert.Error(t, err) {321 assert.Contains(t, err.Error(), poolErrorTestCase.expectedHandlerErrorMessage)322 }323 assert.Nil(t, response)324 })325 }326 t.Run("it is thread-safe", func(t *testing.T) {327 mockController := gomock.NewController(t)328 defer mockController.Finish()329 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)330 dummyToken := "ohlebeautoken"331 nextToken := "encoreplusbeau"332 bothTokens := []string{dummyToken, nextToken}333 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{334 TokenSpec: types.TokenSpec{335 Token: dummyToken,336 ExpectedRateLimit: 250,337 },338 RemainingCalls: 250,339 }, nil)340 for remaining := 200; remaining >= 0; remaining -= remainingCallsReportingInterval {341 tokenPool.EXPECT().UpdateTokenUsage(dummyToken, remaining)342 }343 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{344 TokenSpec: types.TokenSpec{345 Token: nextToken,346 ExpectedRateLimit: 250,347 },348 RemainingCalls: 250,349 }, nil)350 for remaining := 200; remaining >= remainingCallsReportingInterval; remaining -= remainingCallsReportingInterval {351 tokenPool.EXPECT().UpdateTokenUsage(nextToken, remaining)352 }353 nRequests := 490354 requests := make([]*http.Request, nRequests)355 for i := 0; i < nRequests; i++ {356 request, err := githubUpstream.buildRequest(githubUpstream.withResponseHeaders(rateLimitHeader, "250"))357 require.NoError(t, err)358 requests[i] = request359 }360 handler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)361 nThreads := 10362 var wg sync.WaitGroup363 perThread := nRequests / nThreads364 countsPerToken := make(map[string]int)365 var countsMutex sync.Mutex366 for threadID := 0; threadID < nThreads; threadID++ {367 wg.Add(1)368 go func(startIndex int) {369 defer wg.Done()370 for i := startIndex; i < startIndex+perThread; i++ {371 response, body, ok := assertHandleRequestSuccessful(t, handler, requests[i], http.StatusOK)372 require.True(t, ok)373 assert.Equal(t, "pong", body)374 token, err := extractTokenFromAuthHeader(response.Header.Get(receivedAuthHeader))375 if assert.NoError(t, err) && assert.Contains(t, bothTokens, token) {376 countsMutex.Lock()377 countsPerToken[token]++378 countsMutex.Unlock()379 }380 }381 }(threadID * perThread)382 }383 wg.Wait()384 assert.True(t, countsPerToken[dummyToken] >= 250)385 assert.True(t, countsPerToken[dummyToken] < 260)386 assert.Equal(t, nRequests, countsPerToken[dummyToken]+countsPerToken[nextToken])387 })388 t.Run("it stops using tokens when too close to their reset timestamp, based on the response headers", func(t *testing.T) {389 mockController := gomock.NewController(t)390 defer mockController.Finish()391 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)392 dummyToken := "ohlebeautoken"393 nextToken := "encoreplusbeau"394 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{395 TokenSpec: types.TokenSpec{396 Token: dummyToken,397 ExpectedRateLimit: 1000,398 },399 RemainingCalls: 1000,400 }, nil)401 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{402 TokenSpec: types.TokenSpec{403 Token: nextToken,404 ExpectedRateLimit: 10,405 },406 RemainingCalls: 10,407 }, nil)408 handler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)409 // a fist request, that says it resets in 30 secs410 resetIn30Secs := internal.TimeNowUnix() + 30411 request, err := githubUpstream.buildRequest(githubUpstream.withResponseHeaders(resetTimestampHeader, strconv.FormatInt(resetIn30Secs, 10)))412 require.NoError(t, err)413 response, body, ok := assertHandleRequestSuccessful(t, handler, request, http.StatusOK)414 require.True(t, ok)415 assert.Equal(t, "pong", body)416 assertAuthHeaderMatchesToken(t, dummyToken, response.Header.Get(receivedAuthHeader))417 // let's make another request, that should make the handler request a new token from the pool418 request, err = githubUpstream.buildRequest()419 require.NoError(t, err)420 response, body, ok = assertHandleRequestSuccessful(t, handler, request, http.StatusOK)421 require.True(t, ok)422 assert.Equal(t, "pong", body)423 assertAuthHeaderMatchesToken(t, nextToken, response.Header.Get(receivedAuthHeader))424 })425 t.Run("if the response headers don't include a reset timestamp, it keeps track of"+426 " when the token was checked out, and stops using it when too close to the reset", func(t *testing.T) {427 mockController := gomock.NewController(t)428 defer mockController.Finish()429 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)430 dummyToken := "ohlebeautoken"431 nextToken := "encoreplusbeau"432 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{433 TokenSpec: types.TokenSpec{434 Token: dummyToken,435 ExpectedRateLimit: 1000,436 },437 RemainingCalls: 1000,438 }, nil)439 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{440 TokenSpec: types.TokenSpec{441 Token: nextToken,442 ExpectedRateLimit: 10,443 },444 RemainingCalls: 10,445 }, nil)446 handler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)447 defer internal.WithTimeMock(t, 1000, 4540, 4540)()448 // first request449 request, err := githubUpstream.buildRequest()450 require.NoError(t, err)451 response, body, ok := assertHandleRequestSuccessful(t, handler, request, http.StatusOK)452 require.True(t, ok)453 assert.Equal(t, "pong", body)454 assertAuthHeaderMatchesToken(t, dummyToken, response.Header.Get(receivedAuthHeader))455 // second request is 59 minutes later, shouldn't use the same token456 request, err = githubUpstream.buildRequest()457 require.NoError(t, err)458 response, body, ok = assertHandleRequestSuccessful(t, handler, request, http.StatusOK)459 require.True(t, ok)460 assert.Equal(t, "pong", body)461 assertAuthHeaderMatchesToken(t, nextToken, response.Header.Get(receivedAuthHeader))462 })463}464func TestRequestHandler_HandleGithubAPIRequest(t *testing.T) {465 githubUpstream := newUpstreamServer(t)466 githubUpstream.start()467 defer githubUpstream.stop()468 t.Run("it adds tokens to Github API requests", func(t *testing.T) {469 mockController := gomock.NewController(t)470 defer mockController.Finish()471 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)472 handler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)473 proxy := &testHTTPServer{474 t: t,475 handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {476 handler.HandleGithubAPIRequest(writer, request)477 }),478 }479 proxy.start()480 defer proxy.stop()481 dummyToken := "ohlebeautoken"482 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{483 TokenSpec: types.TokenSpec{484 Token: dummyToken,485 ExpectedRateLimit: 1000,486 },487 RemainingCalls: 1000,488 }, nil)489 reqBody := "hey you"490 request, err := proxy.buildRequest(proxy.withRequestPath("/echo"),491 proxy.withRequestBody(reqBody))492 require.NoError(t, err)493 response, err := http.DefaultClient.Do(request)494 require.NoError(t, err)495 respBody, ok := assertResponse(t, response, http.StatusOK)496 require.True(t, ok)497 assert.Equal(t, reqBody, respBody)498 assertAuthHeaderMatchesToken(t, dummyToken, response.Header.Get(receivedAuthHeader))499 })500 t.Run("if ProxyRequest returns an error, it translates that to a 500 error", func(t *testing.T) {501 mockController := gomock.NewController(t)502 defer mockController.Finish()503 tokenPool := mock_token_pools.NewMockTokenPoolStorageBackend(mockController)504 handler := NewRequestHandler(githubUpstream.urlStruct(), tokenPool)505 handler.Transport = &failingTransport{}506 proxy := &testHTTPServer{507 t: t,508 handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {509 handler.HandleGithubAPIRequest(writer, request)510 }),511 }512 proxy.start()513 defer proxy.stop()514 dummyToken := "ohlebeautoken"515 tokenPool.EXPECT().CheckOutToken().Return(&types.Token{516 TokenSpec: types.TokenSpec{517 Token: dummyToken,518 ExpectedRateLimit: 1000,519 },520 RemainingCalls: 1000,521 }, nil)522 request, err := proxy.buildRequest()523 require.NoError(t, err)524 response, err := http.DefaultClient.Do(request)525 require.NoError(t, err)526 body, ok := assertResponse(t, response, http.StatusInternalServerError)527 require.True(t, ok)528 assert.Equal(t, proxyErrorResponseBody, body)529 })...
api_test.go
Source:api_test.go
...27 {28 name: "OK",29 inputBody: `{"id":348, "sum": 2700}`,30 mockUserBehavior: func(s *mock_service.MockUser) {31 s.EXPECT().AddFunds(348, float32(2700)).Return(nil)32 },33 expectedStatusCode: http.StatusOK,34 expectedRequestBody: "",35 },36 {37 name: "Invalid Body",38 inputBody: `{"id":348}`,39 mockUserBehavior: func(s *mock_service.MockUser) {},40 expectedStatusCode: http.StatusBadRequest,41 expectedRequestBody: `{"message":"invalid body."}`,42 },43 {44 name: "Negative Sum",45 inputBody: `{"id":34, "sum": -10}`,46 mockUserBehavior: func(s *mock_service.MockUser) {47 s.EXPECT().AddFunds(34, float32(-10)).Return(&service.NegativeSum{})48 },49 expectedStatusCode: http.StatusBadRequest,50 expectedRequestBody: `{"message":"sum can't be negative or 0."}`,51 },52 {53 name: "Internal Server Error",54 inputBody: `{"id":14589, "sum": 10}`,55 mockUserBehavior: func(s *mock_service.MockUser) {56 s.EXPECT().AddFunds(14589, float32(10)).Return(&service.InternalServerError{})57 },58 expectedStatusCode: http.StatusInternalServerError,59 expectedRequestBody: `{"message":"internal server error."}`,60 },61 }62 t.Parallel()63 for _, testCase := range testData {64 t.Run(testCase.name, func(t *testing.T) {65 // init deps66 c := gomock.NewController(t)67 defer c.Finish()68 servi := mock_service.NewMockUser(c)69 testCase.mockUserBehavior(servi)70 services := &service.Service{User: servi}71 handler := NewHandler(services)72 // test server73 r := gin.New()74 r.POST("/api/v1/add_funds", handler.addFundsHandler)75 // test request76 w := httptest.NewRecorder()77 req := httptest.NewRequest("POST", "/api/v1/add_funds", bytes.NewBufferString(testCase.inputBody))78 // perform request79 r.ServeHTTP(w, req)80 // assert81 assert.Equal(t, testCase.expectedStatusCode, w.Code)82 assert.Equal(t, testCase.expectedRequestBody, w.Body.String())83 })84 }85}86func TestHandler_writeOffFundsHandler(t *testing.T) {87 testData := []testSkillet{88 {89 name: "OK",90 inputBody: `{"id":348, "sum": 2700}`,91 mockUserBehavior: func(s *mock_service.MockUser) {92 s.EXPECT().WriteOffFunds(348, float32(2700)).Return(nil)93 },94 expectedStatusCode: http.StatusOK,95 expectedRequestBody: "",96 },97 {98 name: "Invalid Body",99 inputBody: `{"id":348}`,100 mockUserBehavior: func(s *mock_service.MockUser) {},101 expectedStatusCode: http.StatusBadRequest,102 expectedRequestBody: `{"message":"invalid body."}`,103 },104 {105 name: "Negative Sum",106 inputBody: `{"id":34, "sum": -10}`,107 mockUserBehavior: func(s *mock_service.MockUser) {108 s.EXPECT().WriteOffFunds(34, float32(-10)).Return(&service.NegativeSum{})109 },110 expectedStatusCode: http.StatusBadRequest,111 expectedRequestBody: `{"message":"sum can't be negative or 0."}`,112 },113 {114 name: "User Not Found",115 inputBody: `{"id":91, "sum": 10}`,116 mockUserBehavior: func(s *mock_service.MockUser) {117 s.EXPECT().WriteOffFunds(91, float32(10)).Return(&service.UserNotFound{Id: 91})118 },119 expectedStatusCode: http.StatusNotFound,120 expectedRequestBody: `{"message":"user 91 does not exist."}`,121 },122 {123 name: "Insufficient Funds",124 inputBody: `{"id":23, "sum": 10}`,125 mockUserBehavior: func(s *mock_service.MockUser) {126 s.EXPECT().WriteOffFunds(23, float32(10)).Return(&service.InsufficientFunds{Id: 23})127 },128 expectedStatusCode: http.StatusPreconditionFailed,129 expectedRequestBody: `{"message":"user 23 has insufficient funds."}`,130 },131 {132 name: "Internal Server Error",133 inputBody: `{"id":14589, "sum": 10}`,134 mockUserBehavior: func(s *mock_service.MockUser) {135 s.EXPECT().WriteOffFunds(14589, float32(10)).Return(&service.InternalServerError{})136 },137 expectedStatusCode: http.StatusInternalServerError,138 expectedRequestBody: `{"message":"internal server error."}`,139 },140 }141 t.Parallel()142 for _, testCase := range testData {143 t.Run(testCase.name, func(t *testing.T) {144 // init deps145 c := gomock.NewController(t)146 defer c.Finish()147 servi := mock_service.NewMockUser(c)148 testCase.mockUserBehavior(servi)149 services := &service.Service{User: servi}150 handler := NewHandler(services)151 // test server152 r := gin.New()153 r.POST("/api/v1/write_off_funds", handler.writeOffFundsHandler)154 // test request155 w := httptest.NewRecorder()156 req := httptest.NewRequest("POST", "/api/v1/write_off_funds", bytes.NewBufferString(testCase.inputBody))157 // perform request158 r.ServeHTTP(w, req)159 // assert160 assert.Equal(t, testCase.expectedStatusCode, w.Code)161 assert.Equal(t, testCase.expectedRequestBody, w.Body.String())162 })163 }164}165func TestHandler_fundsTransferHandler(t *testing.T) {166 testData := []testSkillet{167 {168 name: "OK",169 inputBody: `{"sender_id":348, "receiver_id": 4389, "sum": 2700}`,170 mockUserBehavior: func(s *mock_service.MockUser) {171 s.EXPECT().FundsTransfer(348, 4389, float32(2700)).Return(nil)172 },173 expectedStatusCode: http.StatusOK,174 expectedRequestBody: "",175 },176 {177 name: "Invalid Body",178 inputBody: `{"sender_id":348, "receiver_id": 4389}`,179 mockUserBehavior: func(s *mock_service.MockUser) {},180 expectedStatusCode: http.StatusBadRequest,181 expectedRequestBody: `{"message":"invalid body."}`,182 },183 {184 name: "Negative Sum",185 inputBody: `{"sender_id":34, "receiver_id": 89, "sum": -10}`,186 mockUserBehavior: func(s *mock_service.MockUser) {187 s.EXPECT().FundsTransfer(34, 89, float32(-10)).Return(&service.NegativeSum{})188 },189 expectedStatusCode: http.StatusBadRequest,190 expectedRequestBody: `{"message":"sum can't be negative or 0."}`,191 },192 {193 name: "Equal Sender And Receiver",194 inputBody: `{"sender_id":34, "receiver_id": 34, "sum": 1000}`,195 mockUserBehavior: func(s *mock_service.MockUser) {196 s.EXPECT().FundsTransfer(34, 34, float32(1000)).Return(&service.SameId{})197 },198 expectedStatusCode: http.StatusBadRequest,199 expectedRequestBody: `{"message":"user cannot send money to himself."}`,200 },201 {202 name: "User Not Found",203 inputBody: `{"sender_id":91, "receiver_id": 12, "sum": 599}`,204 mockUserBehavior: func(s *mock_service.MockUser) {205 s.EXPECT().FundsTransfer(91, 12, float32(599)).Return(&service.UserNotFound{Id: 91})206 },207 expectedStatusCode: http.StatusNotFound,208 expectedRequestBody: `{"message":"user 91 does not exist."}`,209 },210 {211 name: "Insufficient Funds",212 inputBody: `{"sender_id":23, "receiver_id": 24, "sum": 1000}`,213 mockUserBehavior: func(s *mock_service.MockUser) {214 s.EXPECT().FundsTransfer(23, 24, float32(1000)).Return(&service.InsufficientFunds{Id: 23})215 },216 expectedStatusCode: http.StatusPreconditionFailed,217 expectedRequestBody: `{"message":"user 23 has insufficient funds."}`,218 },219 {220 name: "Internal Server Error",221 inputBody: `{"sender_id":14589, "receiver_id": 4389, "sum": 3500}`,222 mockUserBehavior: func(s *mock_service.MockUser) {223 s.EXPECT().FundsTransfer(14589, 4389, float32(3500)).Return(&service.InternalServerError{})224 },225 expectedStatusCode: http.StatusInternalServerError,226 expectedRequestBody: `{"message":"internal server error."}`,227 },228 }229 t.Parallel()230 for _, testCase := range testData {231 t.Run(testCase.name, func(t *testing.T) {232 // init deps233 c := gomock.NewController(t)234 defer c.Finish()235 servi := mock_service.NewMockUser(c)236 testCase.mockUserBehavior(servi)237 services := &service.Service{User: servi}238 handler := NewHandler(services)239 // test server240 r := gin.New()241 r.POST("/api/v1/funds_transfer", handler.fundsTransferHandler)242 // test request243 w := httptest.NewRecorder()244 req := httptest.NewRequest("POST", "/api/v1/funds_transfer", bytes.NewBufferString(testCase.inputBody))245 // perform request246 r.ServeHTTP(w, req)247 // assert248 assert.Equal(t, testCase.expectedStatusCode, w.Code)249 assert.Equal(t, testCase.expectedRequestBody, w.Body.String())250 })251 }252}253func TestHandler_getBalance(t *testing.T) {254 testData := []testSkillet{255 {256 name: "OK",257 inputBody: `{"id":348}`,258 mockUserBehavior: func(s *mock_service.MockUser) {259 s.EXPECT().GetBalance(348).Return(float32(100), nil)260 },261 mockCalculatorBehavior: func(s *mock_pkg.MockCurrencyCalculator) {},262 expectedStatusCode: http.StatusOK,263 expectedRequestBody: `{"balance":100}`,264 },265 {266 name: "Invalid Body",267 inputBody: `{}`,268 mockUserBehavior: func(s *mock_service.MockUser) {},269 mockCalculatorBehavior: func(s *mock_pkg.MockCurrencyCalculator) {},270 expectedStatusCode: http.StatusBadRequest,271 expectedRequestBody: `{"message":"invalid body."}`,272 },273 {274 name: "OK With Query Param",275 inputBody: `{"id":34}`,276 inputQueryParams: "?currency=USD",277 mockUserBehavior: func(s *mock_service.MockUser) {278 s.EXPECT().GetBalance(34).Return(float32(100), nil)279 },280 mockCalculatorBehavior: func(s *mock_pkg.MockCurrencyCalculator) {281 s.EXPECT().ConvertRubTo("USD", float32(100)).Return(1.3, nil)282 },283 expectedStatusCode: http.StatusOK,284 expectedRequestBody: `{"balance":1.3}`,285 },286 {287 name: "Invalid Query Param",288 inputBody: `{"id":34}`,289 inputQueryParams: "?currency=XRP",290 mockUserBehavior: func(s *mock_service.MockUser) {291 s.EXPECT().GetBalance(34).Return(float32(100), nil)292 },293 mockCalculatorBehavior: func(s *mock_pkg.MockCurrencyCalculator) {294 s.EXPECT().ConvertRubTo("XRP", float32(100)).Return(0.0, &service.WrongParam{Param: "currency"})295 },296 expectedStatusCode: http.StatusPreconditionFailed,297 expectedRequestBody: `{"message":"wrong currency param."}`,298 },299 {300 name: "Internal Server Error",301 inputBody: `{"id":14589}`,302 mockUserBehavior: func(s *mock_service.MockUser) {303 s.EXPECT().GetBalance(14589).Return(float32(0), &service.InternalServerError{})304 },305 mockCalculatorBehavior: func(s *mock_pkg.MockCurrencyCalculator) {},306 expectedStatusCode: http.StatusInternalServerError,307 expectedRequestBody: `{"message":"internal server error."}`,308 },309 {310 name: "User Not Found",311 inputBody: `{"id":91}`,312 mockUserBehavior: func(s *mock_service.MockUser) {313 s.EXPECT().GetBalance(91).Return(float32(0), &service.UserNotFound{Id: 91})314 },315 mockCalculatorBehavior: func(s *mock_pkg.MockCurrencyCalculator) {},316 expectedStatusCode: http.StatusNotFound,317 expectedRequestBody: `{"message":"user 91 does not exist."}`,318 },319 }320 t.Parallel()321 for _, testCase := range testData {322 t.Run(testCase.name, func(t *testing.T) {323 // init deps324 c := gomock.NewController(t)325 defer c.Finish()326 userService := mock_service.NewMockUser(c)327 testCase.mockUserBehavior(userService)...
publish_test.go
Source:publish_test.go
...31 } {32 mocker := gomock.NewController(t)33 defer mocker.Finish()34 db := mock_pkg.NewMockDB(mocker)35 db.EXPECT().ExecContext(gomock.Any(), "INSERT INTO events (topic,payload) VALUES($1,$2)", data.Topic, data.Payload).Return(nil, testcase.connErr)36 err := pkg.StoreEvent(context.Background(), db, data)37 if (testcase.outputErr == nil && err != nil) ||38 (testcase.outputErr != nil && err == nil) ||39 (err != nil && testcase.outputErr.Error() != err.Error()) {40 t.Errorf("Test %s, err is %v, should be %v", testcase.tName, err, testcase.outputErr)41 }42 }43}44func TestKafkaPublishFromSQL(t *testing.T) {45 id := 246 topic := "topic1"47 payload := []byte(`{"foo":"bar"}`)48 for _, testcase := range []struct {49 tName string50 txErr error51 queryErr error52 deleteErr error53 kafkaErr error54 commitErr error55 outputErr error56 }{57 {58 tName: "fail start transaction",59 txErr: errors.New("fail"),60 outputErr: errors.New("fail"),61 },62 {63 tName: "fail query",64 queryErr: errors.New("fail"),65 outputErr: errors.New("fail"),66 },67 {68 tName: "fail delete",69 deleteErr: errors.New("fail"),70 outputErr: errors.New("fail"),71 },72 {73 tName: "fail kafka",74 kafkaErr: errors.New("fail"),75 outputErr: errors.New("fail"),76 },77 {78 tName: "fail commit",79 commitErr: errors.New("fail"),80 outputErr: errors.New("fail"),81 },82 {83 tName: "success",84 },85 {86 tName: "batch failed to query",87 },88 } {89 t.Run(testcase.tName, func(t *testing.T) {90 ctrl := gomock.NewController(t)91 defer ctrl.Finish()92 kafka := mock_sarama.NewMockSyncProducer(ctrl)93 db, mock, err := sqlmock.New()94 if err != nil {95 panic(err)96 }97 defer db.Close()98 if testcase.txErr != nil {99 mock.ExpectBegin().WillReturnError(testcase.txErr)100 } else {101 mock.ExpectBegin()102 if testcase.queryErr != nil {103 mock.ExpectQuery("SELECT id, topic, payload FROM events LIMIT 1 FOR UPDATE SKIP LOCKED").WillReturnError(testcase.queryErr)104 mock.ExpectRollback()105 } else {106 mock.ExpectQuery("SELECT id, topic, payload FROM events LIMIT 1 FOR UPDATE SKIP LOCKED").WillReturnRows(mock.NewRows([]string{"id", "topic", "payload"}).AddRow(id, topic, payload))107 if testcase.deleteErr != nil {108 mock.ExpectExec("DELETE FROM events where id = ?").WillReturnError(testcase.deleteErr)109 mock.ExpectRollback()110 } else {111 mock.ExpectExec("DELETE FROM events where id = ?").WillReturnResult(sqlmock.NewResult(1, 1))112 if testcase.kafkaErr != nil {113 kafka.EXPECT().SendMessage(gomock.Any()).Return(int32(1), int64(2), testcase.kafkaErr)114 mock.ExpectRollback()115 } else {116 kafka.EXPECT().SendMessage(gomock.Any()).Return(int32(1), int64(2), nil)117 if testcase.commitErr != nil {118 mock.ExpectCommit().WillReturnError(testcase.commitErr)119 } else {120 mock.ExpectCommit()121 }122 }123 }124 }125 }126 consumer := pkg.KafkaPublishFromSQL{127 DB: db,128 Kafka: kafka,129 Batch: 1,130 WorkerNum: 1,131 }132 err = consumer.Process()133 if (testcase.outputErr == nil && err != nil) ||134 (testcase.outputErr != nil && err == nil) ||135 (err != nil && testcase.outputErr.Error() != err.Error()) {136 t.Errorf("Test %s, err is %v, should be %v", testcase.tName, err, testcase.outputErr)137 }138 if err := mock.ExpectationsWereMet(); err != nil {139 panic(err)140 }141 })142 }143}144func TestKafkaPublishFromSQLBatch(t *testing.T) {145 id := 2146 topic := "topic1"147 payload := []byte(`{"foo":"bar"}`)148 for _, testcase := range []struct {149 tName string150 emptyRes bool151 txErr error152 queryErr error153 deleteErr error154 kafkaErr error155 commitErr error156 outputErr error157 }{158 {159 tName: "fail start transaction",160 txErr: errors.New("fail"),161 outputErr: errors.New("fail"),162 },163 {164 tName: "fail query",165 queryErr: errors.New("fail"),166 outputErr: errors.New("fail"),167 },168 {169 tName: "empty result",170 emptyRes: true,171 outputErr: sql.ErrNoRows,172 },173 {174 tName: "fail scan",175 outputErr: errors.New(`sql: Scan error on column index 0, name "id": converting driver.Value type string ("invalid id") to a int: invalid syntax`),176 },177 {178 tName: "fail delete",179 deleteErr: errors.New("fail"),180 outputErr: errors.New("fail"),181 },182 {183 tName: "fail kafka",184 kafkaErr: errors.New("fail"),185 outputErr: errors.New("fail"),186 },187 {188 tName: "fail commit",189 commitErr: errors.New("fail"),190 outputErr: errors.New("fail"),191 },192 {193 tName: "success",194 },195 } {196 t.Run(testcase.tName, func(t *testing.T) {197 ctrl := gomock.NewController(t)198 defer ctrl.Finish()199 kafka := mock_sarama.NewMockSyncProducer(ctrl)200 db, mock, err := sqlmock.New()201 if err != nil {202 panic(err)203 }204 defer db.Close()205 if testcase.txErr != nil {206 mock.ExpectBegin().WillReturnError(testcase.txErr)207 } else {208 mock.ExpectBegin()209 if testcase.queryErr != nil {210 mock.ExpectQuery("SELECT id, topic, payload FROM events LIMIT 2 FOR UPDATE SKIP LOCKED").WillReturnError(testcase.queryErr)211 mock.ExpectRollback()212 } else {213 if testcase.tName == "fail scan" {214 mock.ExpectQuery("SELECT id, topic, payload FROM events LIMIT 2 FOR UPDATE SKIP LOCKED").WillReturnRows(mock.NewRows([]string{"id", "topic", "payload"}).AddRow("invalid id", topic, payload))215 } else if testcase.emptyRes {216 mock.ExpectQuery("SELECT id, topic, payload FROM events LIMIT 2 FOR UPDATE SKIP LOCKED").WillReturnRows(mock.NewRows([]string{"id", "topic", "payload"}))217 mock.ExpectRollback()218 } else {219 mock.ExpectQuery("SELECT id, topic, payload FROM events LIMIT 2 FOR UPDATE SKIP LOCKED").WillReturnRows(mock.NewRows([]string{"id", "topic", "payload"}).AddRow(id, topic, payload))220 if testcase.deleteErr != nil {221 mock.ExpectExec("DELETE FROM events where id IN \\('2'\\)").WillReturnError(testcase.deleteErr)222 mock.ExpectRollback()223 } else {224 mock.ExpectExec("DELETE FROM events where id IN \\('2'\\)").WillReturnResult(sqlmock.NewResult(1, 1))225 if testcase.kafkaErr != nil {226 kafka.EXPECT().SendMessages(gomock.Any()).Return(testcase.kafkaErr)227 mock.ExpectRollback()228 } else {229 kafka.EXPECT().SendMessages(gomock.Any()).Return(nil)230 if testcase.commitErr != nil {231 mock.ExpectCommit().WillReturnError(testcase.commitErr)232 } else {233 mock.ExpectCommit()234 }235 }236 }237 }238 }239 }240 consumer := pkg.KafkaPublishFromSQL{241 DB: db,242 Kafka: kafka,243 Batch: 2,...
EXPECT
Using AI Code Generation
1func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {2 return &mock_pkgMockRecorder{mock}3}4func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {5 return &mock_pkgMockRecorder{mock}6}7func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {8 return &mock_pkgMockRecorder{mock}9}10func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {11 return &mock_pkgMockRecorder{mock}12}13func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {14 return &mock_pkgMockRecorder{mock}15}16func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {17 return &mock_pkgMockRecorder{mock}18}19func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {20 return &mock_pkgMockRecorder{mock}21}22func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {23 return &mock_pkgMockRecorder{mock}24}25func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {26 return &mock_pkgMockRecorder{mock}27}28func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {29 return &mock_pkgMockRecorder{mock}30}31func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {32 return &mock_pkgMockRecorder{mock}33}34func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {35 return &mock_pkgMockRecorder{mock}36}
EXPECT
Using AI Code Generation
1func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {2 return &mock_pkgMockRecorder{mock}3}4func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {5 return &mock_pkgMockRecorder{mock}6}7func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {8 return &mock_pkgMockRecorder{mock}9}10func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {11 return &mock_pkgMockRecorder{mock}12}13func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {14 return &mock_pkgMockRecorder{mock}15}16func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {17 return &mock_pkgMockRecorder{mock}18}19func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {20 return &mock_pkgMockRecorder{mock}21}22func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {23 return &mock_pkgMockRecorder{mock}24}25func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {26 return &mock_pkgMockRecorder{mock}27}28func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {29 return &mock_pkgMockRecorder{mock}30}31func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {32 return &mock_pkgMockRecorder{mock}33}34func (m *mock_pkg) EXPECT() *mock_pkgMockRecorder {35 return &mock_pkgMockRecorder{mock}36}
EXPECT
Using AI Code Generation
1mock_pkg.EXPECT().Method1().Return(1, nil)2mock_pkg.EXPECT().Method2().Return(2, nil)3mock_pkg.On("Method3").Return(3, nil)4mock_pkg.On("Method4").Return(4, nil)5mock_pkg.EXPECT().Method1().Return(1, nil)6mock_pkg.EXPECT().Method2().Return(2, nil)7mock_pkg.On("Method3").Return(3, nil)8mock_pkg.On("Method4").Return(4, nil)9mock_pkg.EXPECT().Method1().Return(1, nil)10mock_pkg.EXPECT().Method2().Return(2, nil)11mock_pkg.On("Method3").Return(3, nil)12mock_pkg.On("Method4").Return(4, nil)13mock_pkg.EXPECT().Method1().Return(1, nil)14mock_pkg.EXPECT().Method2().Return(2, nil)15mock_pkg.On("Method3").Return(3, nil)16mock_pkg.On("Method4").Return(4, nil)17mock_pkg.EXPECT().Method1().Return(1, nil)18mock_pkg.EXPECT().Method2().Return(2, nil)19mock_pkg.On("Method3").Return(3, nil)20mock_pkg.On("Method4").Return(4, nil)21mock_pkg.EXPECT().Method1().Return(1, nil)22mock_pkg.EXPECT().Method2().Return(2, nil)23mock_pkg.On("Method3").Return(3, nil)24mock_pkg.On("Method4").Return(4, nil)
EXPECT
Using AI Code Generation
1func main() {2 mock_pkg := new(MockPkg)3 mock_pkg.On("EXPECT", "MethodA", "arg1", "arg2").Return("return_value")4 result := mock_pkg.MethodA("arg1", "arg2")5 fmt.Println(result)6}7func main() {8 mock_pkg := new(MockPkg)9 mock_pkg.On("EXPECT", "MethodA", "arg1", "arg2").Return("return_value")10 result := mock_pkg.MethodA("arg1", "arg2")11 fmt.Println(result)12}13type A struct {14}15type B struct {16}17func (a *A) GetCs() []*C {18}19type Set struct {20 data map[interface{}]bool21}22func (s *Set) Add(elem interface{}) {23}24func (s *Set) Contains(elem interface{}) bool {25}26func (s *Set) Remove(elem interface{}) {27 delete(s.data, elem)28}29func (s *Set) Len() int {30 return len(s.data)31}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!