Best Mock code snippet using gomock.Add
account_test.go
Source:account_test.go
...379 recorder := httptest.NewRecorder()380 url := "/accounts"381 request, err := http.NewRequest(http.MethodGet, url, nil)382 require.NoError(t, err)383 // Add query parameters to request URL384 q := request.URL.Query()385 q.Add("page_id", fmt.Sprintf("%d", tc.query.pageID))386 q.Add("page_size", fmt.Sprintf("%d", tc.query.pageSize))387 request.URL.RawQuery = q.Encode()388 tc.setupAuth(t, request, server.tokenMaker)389 server.router.ServeHTTP(recorder, request)390 tc.checkResponse(recorder)391 })392 }393}394func randomAccount(owner string) db.Account {395 return db.Account{396 ID: util.RandomInt(1, 1000),397 Owner: owner,398 Balance: util.RandomMoney(),399 Currency: util.RandomCurrency(),400 Citizenship: util.RandomCitizenship(),...
transfer_test.go
Source:transfer_test.go
1package api2import (3 "bytes"4 "database/sql"5 "encoding/json"6 "net/http"7 "net/http/httptest"8 "testing"9 "time"10 mockdb "github.com/amallick86/psp/db/mock"11 db "github.com/amallick86/psp/db/sqlc"12 "github.com/amallick86/psp/token"13 "github.com/amallick86/psp/util"14 "github.com/gin-gonic/gin"15 "github.com/golang/mock/gomock"16 "github.com/stretchr/testify/require"17)18func TestTransferAPI(t *testing.T) {19 amount := int64(10)20 user1, _ := randomUser(t)21 user2, _ := randomUser(t)22 user3, _ := randomUser(t)23 account1 := randomAccount(user1.Username)24 account2 := randomAccount(user2.Username)25 account3 := randomAccount(user3.Username)26 account1.Currency = util.USD27 account2.Currency = util.USD28 account3.Currency = util.EUR29 testCases := []struct {30 name string31 body gin.H32 setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker)33 buildStubs func(store *mockdb.MockStore)34 checkResponse func(recoder *httptest.ResponseRecorder)35 }{36 {37 name: "OK",38 body: gin.H{39 "from_account_id": account1.ID,40 "to_account_id": account2.ID,41 "amount": amount,42 "currency": util.USD,43 },44 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {45 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute)46 },47 buildStubs: func(store *mockdb.MockStore) {48 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account1.ID)).Times(1).Return(account1, nil)49 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account2.ID)).Times(1).Return(account2, nil)50 arg := db.TransferTxParams{51 FromAccountID: account1.ID,52 ToAccountID: account2.ID,53 Amount: amount,54 }55 store.EXPECT().TransferTx(gomock.Any(), gomock.Eq(arg)).Times(1)56 },57 checkResponse: func(recorder *httptest.ResponseRecorder) {58 require.Equal(t, http.StatusOK, recorder.Code)59 },60 },61 {62 name: "UnauthorizedUser",63 body: gin.H{64 "from_account_id": account1.ID,65 "to_account_id": account2.ID,66 "amount": amount,67 "currency": util.USD,68 },69 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {70 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user2.Username, time.Minute)71 },72 buildStubs: func(store *mockdb.MockStore) {73 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account1.ID)).Times(1).Return(account1, nil)74 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account2.ID)).Times(0)75 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)76 },77 checkResponse: func(recorder *httptest.ResponseRecorder) {78 require.Equal(t, http.StatusUnauthorized, recorder.Code)79 },80 },81 {82 name: "NoAuthorization",83 body: gin.H{84 "from_account_id": account1.ID,85 "to_account_id": account2.ID,86 "amount": amount,87 "currency": util.USD,88 },89 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {90 },91 buildStubs: func(store *mockdb.MockStore) {92 store.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Times(0)93 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)94 },95 checkResponse: func(recorder *httptest.ResponseRecorder) {96 require.Equal(t, http.StatusUnauthorized, recorder.Code)97 },98 },99 {100 name: "FromAccountNotFound",101 body: gin.H{102 "from_account_id": account1.ID,103 "to_account_id": account2.ID,104 "amount": amount,105 "currency": util.USD,106 },107 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {108 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute)109 },110 buildStubs: func(store *mockdb.MockStore) {111 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account1.ID)).Times(1).Return(db.Account{}, sql.ErrNoRows)112 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account2.ID)).Times(0)113 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)114 },115 checkResponse: func(recorder *httptest.ResponseRecorder) {116 require.Equal(t, http.StatusNotFound, recorder.Code)117 },118 },119 {120 name: "ToAccountNotFound",121 body: gin.H{122 "from_account_id": account1.ID,123 "to_account_id": account2.ID,124 "amount": amount,125 "currency": util.USD,126 },127 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {128 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute)129 },130 buildStubs: func(store *mockdb.MockStore) {131 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account1.ID)).Times(1).Return(account1, nil)132 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account2.ID)).Times(1).Return(db.Account{}, sql.ErrNoRows)133 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)134 },135 checkResponse: func(recorder *httptest.ResponseRecorder) {136 require.Equal(t, http.StatusNotFound, recorder.Code)137 },138 },139 {140 name: "FromAccountCurrencyMismatch",141 body: gin.H{142 "from_account_id": account3.ID,143 "to_account_id": account2.ID,144 "amount": amount,145 "currency": util.USD,146 },147 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {148 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user3.Username, time.Minute)149 },150 buildStubs: func(store *mockdb.MockStore) {151 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account3.ID)).Times(1).Return(account3, nil)152 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account2.ID)).Times(0)153 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)154 },155 checkResponse: func(recorder *httptest.ResponseRecorder) {156 require.Equal(t, http.StatusBadRequest, recorder.Code)157 },158 },159 {160 name: "ToAccountCurrencyMismatch",161 body: gin.H{162 "from_account_id": account1.ID,163 "to_account_id": account3.ID,164 "amount": amount,165 "currency": util.USD,166 },167 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {168 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute)169 },170 buildStubs: func(store *mockdb.MockStore) {171 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account1.ID)).Times(1).Return(account1, nil)172 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account3.ID)).Times(1).Return(account3, nil)173 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)174 },175 checkResponse: func(recorder *httptest.ResponseRecorder) {176 require.Equal(t, http.StatusBadRequest, recorder.Code)177 },178 },179 {180 name: "InvalidCurrency",181 body: gin.H{182 "from_account_id": account1.ID,183 "to_account_id": account2.ID,184 "amount": amount,185 "currency": "XYZ",186 },187 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {188 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute)189 },190 buildStubs: func(store *mockdb.MockStore) {191 store.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Times(0)192 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)193 },194 checkResponse: func(recorder *httptest.ResponseRecorder) {195 require.Equal(t, http.StatusBadRequest, recorder.Code)196 },197 },198 {199 name: "NegativeAmount",200 body: gin.H{201 "from_account_id": account1.ID,202 "to_account_id": account2.ID,203 "amount": -amount,204 "currency": util.USD,205 },206 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {207 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute)208 },209 buildStubs: func(store *mockdb.MockStore) {210 store.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Times(0)211 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)212 },213 checkResponse: func(recorder *httptest.ResponseRecorder) {214 require.Equal(t, http.StatusBadRequest, recorder.Code)215 },216 },217 {218 name: "GetAccountError",219 body: gin.H{220 "from_account_id": account1.ID,221 "to_account_id": account2.ID,222 "amount": amount,223 "currency": util.USD,224 },225 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {226 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute)227 },228 buildStubs: func(store *mockdb.MockStore) {229 store.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Times(1).Return(db.Account{}, sql.ErrConnDone)230 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)231 },232 checkResponse: func(recorder *httptest.ResponseRecorder) {233 require.Equal(t, http.StatusInternalServerError, recorder.Code)234 },235 },236 {237 name: "TransferTxError",238 body: gin.H{239 "from_account_id": account1.ID,240 "to_account_id": account2.ID,241 "amount": amount,242 "currency": util.USD,243 },244 setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {245 addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute)246 },247 buildStubs: func(store *mockdb.MockStore) {248 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account1.ID)).Times(1).Return(account1, nil)249 store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account2.ID)).Times(1).Return(account2, nil)250 store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(1).Return(db.TransferTxResult{}, sql.ErrTxDone)251 },252 checkResponse: func(recorder *httptest.ResponseRecorder) {253 require.Equal(t, http.StatusInternalServerError, recorder.Code)254 },255 },256 }257 for i := range testCases {258 tc := testCases[i]259 t.Run(tc.name, func(t *testing.T) {260 ctrl := gomock.NewController(t)261 defer ctrl.Finish()262 store := mockdb.NewMockStore(ctrl)263 tc.buildStubs(store)264 server := newTestServer(t, store)265 recorder := httptest.NewRecorder()266 // Marshal body data to JSON267 data, err := json.Marshal(tc.body)268 require.NoError(t, err)269 url := "/transfers"270 request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))271 require.NoError(t, err)272 tc.setupAuth(t, request, server.tokenMaker)273 server.router.ServeHTTP(recorder, request)274 tc.checkResponse(recorder)275 })276 }277}...
controller_handle_group_entry_test.go
Source:controller_handle_group_entry_test.go
1/*2 * Copyright 2020 Kaiserpfalz EDV-Service, Roland T. Lichti.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package installedfeature_test17import (18 "errors"19 "github.com/golang/mock/gomock"20 . "github.com/klenkes74/k8s-installed-features-catalogue/api/v1alpha1"21 . "github.com/onsi/ginkgo"22 . "github.com/onsi/gomega"23 k8sclient "sigs.k8s.io/controller-runtime/pkg/client"24 // +kubebuilder:scaffold:imports25)26var _ = Describe("InstalledFeature controller handling featuregroups", func() {27 Context("Handle Library Groups", func() {28 It("should add the status entry on the IFTG when the IFTG has no features yet", func() {29 ift := createIFT(name, namespace, version, provider, description, uri, true, false)30 setGroupToIFT(ift, group, namespace)31 iftg := createIFTG(group, namespace, provider, description, uri, true, false)32 client.EXPECT().LoadInstalledFeature(gomock.Any(), iftLookupKey).Return(ift, nil)33 client.EXPECT().LoadInstalledFeatureGroup(gomock.Any(), iftgLookupKey).Return(iftg, nil)34 client.EXPECT().GetInstalledFeatureGroupPatchBase(gomock.Any()).Return(k8sclient.MergeFrom(iftg))35 client.EXPECT().PatchInstalledFeatureGroupStatus(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)36 client.EXPECT().GetInstalledFeaturePatchBase(gomock.Any()).Return(k8sclient.MergeFrom(ift))37 client.EXPECT().PatchInstalledFeatureStatus(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)38 result, err := sut.Reconcile(iftReconcileRequest)39 Expect(result).Should(Equal(successResult))40 Expect(err).ToNot(HaveOccurred())41 })42 It("should add the status entry on the IFTG when the IFTG has already features", func() {43 ift := createIFT(name, namespace, version, provider, description, uri, true, false)44 setGroupToIFT(ift, group, namespace)45 iftg := createIFTG(group, namespace, provider, description, uri, true, false)46 iftg.Status.Features = make([]InstalledFeatureGroupListedFeature, 1)47 iftg.Status.Features[0] = InstalledFeatureGroupListedFeature{48 Namespace: namespace,49 Name: "other-feature",50 }51 client.EXPECT().LoadInstalledFeature(gomock.Any(), iftLookupKey).Return(ift, nil)52 client.EXPECT().LoadInstalledFeatureGroup(gomock.Any(), iftgLookupKey).Return(iftg, nil)53 client.EXPECT().GetInstalledFeatureGroupPatchBase(gomock.Any()).Return(k8sclient.MergeFrom(iftg))54 client.EXPECT().PatchInstalledFeatureGroupStatus(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)55 client.EXPECT().GetInstalledFeaturePatchBase(gomock.Any()).Return(k8sclient.MergeFrom(ift))56 client.EXPECT().PatchInstalledFeatureStatus(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)57 result, err := sut.Reconcile(iftReconcileRequest)58 Expect(result).Should(Equal(successResult))59 Expect(err).ToNot(HaveOccurred())60 })61 It("should not add the feature to the status entry on the IFTG when the IFTG already lists this feature", func() {62 ift := createIFT(name, namespace, version, provider, description, uri, true, false)63 setGroupToIFT(ift, group, namespace)64 iftg := createIFTG(group, namespace, provider, description, uri, true, false)65 iftg.Status.Features = make([]InstalledFeatureGroupListedFeature, 1)66 iftg.Status.Features[0] = InstalledFeatureGroupListedFeature{67 Namespace: namespace,68 Name: name,69 }70 client.EXPECT().LoadInstalledFeature(gomock.Any(), iftLookupKey).Return(ift, nil)71 client.EXPECT().LoadInstalledFeatureGroup(gomock.Any(), iftgLookupKey).Return(iftg, nil)72 client.EXPECT().GetInstalledFeatureGroupPatchBase(gomock.Any()).Return(k8sclient.MergeFrom(iftg))73 client.EXPECT().GetInstalledFeaturePatchBase(gomock.Any()).Return(k8sclient.MergeFrom(ift))74 client.EXPECT().PatchInstalledFeatureStatus(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)75 result, err := sut.Reconcile(iftReconcileRequest)76 Expect(result).Should(Equal(successResult))77 Expect(err).ToNot(HaveOccurred())78 })79 It("should remove the status entry on the IFTG when IFT is deleted and is listed in IFTG", func() {80 ift := createIFT(name, namespace, version, provider, description, uri, false, true)81 setGroupToIFT(ift, group, namespace)82 iftg := createIFTG(group, namespace, provider, description, uri, true, false)83 iftg.Status.Features = make([]InstalledFeatureGroupListedFeature, 1)84 iftg.Status.Features[0] = InstalledFeatureGroupListedFeature{85 Namespace: namespace,86 Name: name,87 }88 client.EXPECT().LoadInstalledFeature(gomock.Any(), iftLookupKey).Return(ift, nil)89 client.EXPECT().LoadInstalledFeatureGroup(gomock.Any(), iftgLookupKey).Return(iftg, nil)90 client.EXPECT().GetInstalledFeatureGroupPatchBase(gomock.Any()).Return(k8sclient.MergeFrom(iftg))91 client.EXPECT().PatchInstalledFeatureGroupStatus(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)92 client.EXPECT().GetInstalledFeaturePatchBase(gomock.Any()).Return(k8sclient.MergeFrom(ift))93 client.EXPECT().PatchInstalledFeatureStatus(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)94 result, err := sut.Reconcile(iftReconcileRequest)95 Expect(result).Should(Equal(successResult))96 Expect(err).ToNot(HaveOccurred())97 })98 It("should requeue the request when IFTG can't be loaded", func() {99 ift := createIFT(name, namespace, version, provider, description, uri, false, true)100 setGroupToIFT(ift, group, namespace)101 client.EXPECT().LoadInstalledFeature(gomock.Any(), iftLookupKey).Return(ift, nil)102 client.EXPECT().LoadInstalledFeatureGroup(gomock.Any(), iftgLookupKey).Return(nil, errors.New("can not load IFTG"))103 result, err := sut.Reconcile(iftReconcileRequest)104 Expect(result).Should(Equal(errorResult))105 Expect(err).Should(HaveOccurred())106 })107 })108})...
Add
Using AI Code Generation
1import (2func TestAdd(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := NewMockAdder(ctrl)6 mock.EXPECT().Add(3, 4).Return(7)7 fmt.Println(mock.Add(3, 4))8}9import (10type MockAdder struct {11}12type MockAdderMockRecorder struct {13}14func NewMockAdder(ctrl *gomock.Controller) *MockAdder {15 mock := &MockAdder{ctrl: ctrl}16 mock.recorder = &MockAdderMockRecorder{mock}17}18func (m *MockAdder) EXPECT() *MockAdderMockRecorder {19}20func (m *MockAdder) Add(arg0, arg1 int) int {21 m.ctrl.T.Helper()22 ret := m.ctrl.Call(m, "Add", arg0, arg1)23 ret0, _ := ret[0].(int)24}25func (mr *MockAdderMockRecorder) Add(arg0, arg1 interface{}) *gomock.Call {26 mr.mock.ctrl.T.Helper()27 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Add", reflect.TypeOf((*MockAdder)(nil).Add), arg0, arg1)28}29func (m *MockAdder) Add(arg0, arg1 int) int {30 fmt.Println("Add method called")31}32import
Add
Using AI Code Generation
1func Add(a, b int) int {2}3func Add(a, b int) int {4}5func Add(a, b int) int {6}7func Add(a, b int) int {8}9func Add(a, b int) int {10}11func Add(a, b int) int {12}13func Add(a, b int) int {14}15func Add(a, b int) int {16}17func Add(a, b int) int {18}19func Add(a, b int) int {20}21func Add(a, b int) int {22}23func Add(a, b int) int {24}25func Add(a, b int) int {26}27func Add(a, b int) int {28}29func Add(a, b int) int {30}
Add
Using AI Code Generation
1func Add(a int, b int) int {2}3func Add(a int, b int) int {4}5func Add(a int, b int) int {6}7func Add(a int, b int) int {8}9func Add(a int, b int) int {10}11func Add(a int, b int) int {12}13func Add(a int, b int) int {14}15func Add(a int, b int) int {16}17func Add(a int, b int) int {18}19func Add(a int, b int) int {20}21func Add(a int, b int) int {22}23func Add(a int, b int) int {24}25func Add(a int, b int) int {26}27func Add(a int, b int) int {28}29func Add(a int, b int)
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!!