Best Got code snippet using got.Once
policy_test.go
Source:policy_test.go
...23func TestPolicy_SetDeletesPctAllowed(t *testing.T) {24 numberSet := float32(1)25 numberChange := float32(2)26 type fields struct {27 maxMergeAtOnceExplicit *uint28 maxMergeAtOnce *uint29 floorSegment *Size30 maxMergedSegment *Size31 reclaimDeletesWeight *float3232 expungeDeletesAllowed *float3233 segmentsPerTier *float3234 deletesPctAllowed *float3235 }36 type args struct {37 deletesPctAllowed float3238 }39 tests := []struct {40 name string41 fields fields42 args args43 want *Policy44 }{45 {46 name: "set",47 args: args{deletesPctAllowed: 1},48 want: &Policy{deletesPctAllowed: &numberSet},49 },50 {51 name: "change",52 fields: fields{deletesPctAllowed: &numberSet},53 args: args{deletesPctAllowed: 2},54 want: &Policy{deletesPctAllowed: &numberChange},55 },56 }57 for _, tt := range tests {58 t.Run(tt.name, func(t *testing.T) {59 policy := &Policy{60 maxMergeAtOnceExplicit: tt.fields.maxMergeAtOnceExplicit,61 maxMergeAtOnce: tt.fields.maxMergeAtOnce,62 floorSegment: tt.fields.floorSegment,63 maxMergedSegment: tt.fields.maxMergedSegment,64 reclaimDeletesWeight: tt.fields.reclaimDeletesWeight,65 expungeDeletesAllowed: tt.fields.expungeDeletesAllowed,66 segmentsPerTier: tt.fields.segmentsPerTier,67 deletesPctAllowed: tt.fields.deletesPctAllowed,68 }69 if got := policy.SetDeletesPctAllowed(tt.args.deletesPctAllowed); !reflect.DeepEqual(got, tt.want) {70 t.Errorf("SetDeletesPctAllowed() = %v, want %v", got, tt.want)71 }72 })73 }74}75func TestPolicy_SetExpungeDeletesAllowed(t *testing.T) {76 numberSet := float32(1)77 numberChange := float32(2)78 type fields struct {79 maxMergeAtOnceExplicit *uint80 maxMergeAtOnce *uint81 floorSegment *Size82 maxMergedSegment *Size83 reclaimDeletesWeight *float3284 expungeDeletesAllowed *float3285 segmentsPerTier *float3286 deletesPctAllowed *float3287 }88 type args struct {89 expungeDeletesAllowed float3290 }91 tests := []struct {92 name string93 fields fields94 args args95 want *Policy96 }{97 {98 name: "set",99 args: args{expungeDeletesAllowed: 1},100 want: &Policy{expungeDeletesAllowed: &numberSet},101 },102 {103 name: "change",104 fields: fields{expungeDeletesAllowed: &numberSet},105 args: args{expungeDeletesAllowed: 2},106 want: &Policy{expungeDeletesAllowed: &numberChange},107 },108 }109 for _, tt := range tests {110 t.Run(tt.name, func(t *testing.T) {111 policy := &Policy{112 maxMergeAtOnceExplicit: tt.fields.maxMergeAtOnceExplicit,113 maxMergeAtOnce: tt.fields.maxMergeAtOnce,114 floorSegment: tt.fields.floorSegment,115 maxMergedSegment: tt.fields.maxMergedSegment,116 reclaimDeletesWeight: tt.fields.reclaimDeletesWeight,117 expungeDeletesAllowed: tt.fields.expungeDeletesAllowed,118 segmentsPerTier: tt.fields.segmentsPerTier,119 deletesPctAllowed: tt.fields.deletesPctAllowed,120 }121 if got := policy.SetExpungeDeletesAllowed(tt.args.expungeDeletesAllowed); !reflect.DeepEqual(got, tt.want) {122 t.Errorf("SetExpungeDeletesAllowed() = %v, want %v", got, tt.want)123 }124 })125 }126}127func TestPolicy_SetFloorSegment(t *testing.T) {128 type fields struct {129 maxMergeAtOnceExplicit *uint130 maxMergeAtOnce *uint131 floorSegment *Size132 maxMergedSegment *Size133 reclaimDeletesWeight *float32134 expungeDeletesAllowed *float32135 segmentsPerTier *float32136 deletesPctAllowed *float32137 }138 type args struct {139 floorSegment *Size140 }141 tests := []struct {142 name string143 fields fields144 args args145 want *Policy146 }{147 {148 name: "set",149 args: args{floorSegment: NewSize("1mb")},150 want: &Policy{floorSegment: NewSize("1mb")},151 },152 {153 name: "change",154 fields: fields{floorSegment: NewSize("1mb")},155 args: args{floorSegment: NewSize("2mb")},156 want: &Policy{floorSegment: NewSize("2mb")},157 },158 }159 for _, tt := range tests {160 t.Run(tt.name, func(t *testing.T) {161 policy := &Policy{162 maxMergeAtOnceExplicit: tt.fields.maxMergeAtOnceExplicit,163 maxMergeAtOnce: tt.fields.maxMergeAtOnce,164 floorSegment: tt.fields.floorSegment,165 maxMergedSegment: tt.fields.maxMergedSegment,166 reclaimDeletesWeight: tt.fields.reclaimDeletesWeight,167 expungeDeletesAllowed: tt.fields.expungeDeletesAllowed,168 segmentsPerTier: tt.fields.segmentsPerTier,169 deletesPctAllowed: tt.fields.deletesPctAllowed,170 }171 if got := policy.SetFloorSegment(tt.args.floorSegment); !reflect.DeepEqual(got, tt.want) {172 t.Errorf("SetFloorSegment() = %v, want %v", got, tt.want)173 }174 })175 }176}177func TestPolicy_SetMaxMergeAtOnce(t *testing.T) {178 numberSet := uint(1)179 numberChange := uint(2)180 type fields struct {181 maxMergeAtOnceExplicit *uint182 maxMergeAtOnce *uint183 floorSegment *Size184 maxMergedSegment *Size185 reclaimDeletesWeight *float32186 expungeDeletesAllowed *float32187 segmentsPerTier *float32188 deletesPctAllowed *float32189 }190 type args struct {191 maxMergeAtOnce uint192 }193 tests := []struct {194 name string195 fields fields196 args args197 want *Policy198 }{199 {200 name: "set",201 args: args{maxMergeAtOnce: 1},202 want: &Policy{maxMergeAtOnce: &numberSet},203 },204 {205 name: "change",206 fields: fields{maxMergeAtOnce: &numberSet},207 args: args{maxMergeAtOnce: 2},208 want: &Policy{maxMergeAtOnce: &numberChange},209 },210 }211 for _, tt := range tests {212 t.Run(tt.name, func(t *testing.T) {213 policy := &Policy{214 maxMergeAtOnceExplicit: tt.fields.maxMergeAtOnceExplicit,215 maxMergeAtOnce: tt.fields.maxMergeAtOnce,216 floorSegment: tt.fields.floorSegment,217 maxMergedSegment: tt.fields.maxMergedSegment,218 reclaimDeletesWeight: tt.fields.reclaimDeletesWeight,219 expungeDeletesAllowed: tt.fields.expungeDeletesAllowed,220 segmentsPerTier: tt.fields.segmentsPerTier,221 deletesPctAllowed: tt.fields.deletesPctAllowed,222 }223 if got := policy.SetMaxMergeAtOnce(tt.args.maxMergeAtOnce); !reflect.DeepEqual(got, tt.want) {224 t.Errorf("SetMaxMergeAtOnce() = %v, want %v", got, tt.want)225 }226 })227 }228}229func TestPolicy_SetMaxMergeAtOnceExplicit(t *testing.T) {230 numberSet := uint(1)231 numberChange := uint(2)232 type fields struct {233 maxMergeAtOnceExplicit *uint234 maxMergeAtOnce *uint235 floorSegment *Size236 maxMergedSegment *Size237 reclaimDeletesWeight *float32238 expungeDeletesAllowed *float32239 segmentsPerTier *float32240 deletesPctAllowed *float32241 }242 type args struct {243 maxMergeAtOnceExplicit uint244 }245 tests := []struct {246 name string247 fields fields248 args args249 want *Policy250 }{251 {252 name: "set",253 args: args{maxMergeAtOnceExplicit: 1},254 want: &Policy{maxMergeAtOnceExplicit: &numberSet},255 },256 {257 name: "change",258 fields: fields{maxMergeAtOnceExplicit: &numberSet},259 args: args{maxMergeAtOnceExplicit: 2},260 want: &Policy{maxMergeAtOnceExplicit: &numberChange},261 },262 }263 for _, tt := range tests {264 t.Run(tt.name, func(t *testing.T) {265 policy := &Policy{266 maxMergeAtOnceExplicit: tt.fields.maxMergeAtOnceExplicit,267 maxMergeAtOnce: tt.fields.maxMergeAtOnce,268 floorSegment: tt.fields.floorSegment,269 maxMergedSegment: tt.fields.maxMergedSegment,270 reclaimDeletesWeight: tt.fields.reclaimDeletesWeight,271 expungeDeletesAllowed: tt.fields.expungeDeletesAllowed,272 segmentsPerTier: tt.fields.segmentsPerTier,273 deletesPctAllowed: tt.fields.deletesPctAllowed,274 }275 if got := policy.SetMaxMergeAtOnceExplicit(tt.args.maxMergeAtOnceExplicit); !reflect.DeepEqual(got, tt.want) {276 t.Errorf("SetMaxMergeAtOnceExplicit() = %v, want %v", got, tt.want)277 }278 })279 }280}281func TestPolicy_SetMaxMergedSegment(t *testing.T) {282 type fields struct {283 maxMergeAtOnceExplicit *uint284 maxMergeAtOnce *uint285 floorSegment *Size286 maxMergedSegment *Size287 reclaimDeletesWeight *float32288 expungeDeletesAllowed *float32289 segmentsPerTier *float32290 deletesPctAllowed *float32291 }292 type args struct {293 maxMergedSegment *Size294 }295 tests := []struct {296 name string297 fields fields298 args args299 want *Policy300 }{301 {302 name: "set",303 args: args{maxMergedSegment: NewSize("1mb")},304 want: &Policy{maxMergedSegment: NewSize("1mb")},305 },306 {307 name: "change",308 fields: fields{maxMergedSegment: NewSize("1mb")},309 args: args{maxMergedSegment: NewSize("2mb")},310 want: &Policy{maxMergedSegment: NewSize("2mb")},311 },312 }313 for _, tt := range tests {314 t.Run(tt.name, func(t *testing.T) {315 policy := &Policy{316 maxMergeAtOnceExplicit: tt.fields.maxMergeAtOnceExplicit,317 maxMergeAtOnce: tt.fields.maxMergeAtOnce,318 floorSegment: tt.fields.floorSegment,319 maxMergedSegment: tt.fields.maxMergedSegment,320 reclaimDeletesWeight: tt.fields.reclaimDeletesWeight,321 expungeDeletesAllowed: tt.fields.expungeDeletesAllowed,322 segmentsPerTier: tt.fields.segmentsPerTier,323 deletesPctAllowed: tt.fields.deletesPctAllowed,324 }325 if got := policy.SetMaxMergedSegment(tt.args.maxMergedSegment); !reflect.DeepEqual(got, tt.want) {326 t.Errorf("SetMaxMergedSegment() = %v, want %v", got, tt.want)327 }328 })329 }330}331func TestPolicy_SetReclaimDeletesWeight(t *testing.T) {332 numberSet := float32(1)333 numberChange := float32(2)334 type fields struct {335 maxMergeAtOnceExplicit *uint336 maxMergeAtOnce *uint337 floorSegment *Size338 maxMergedSegment *Size339 reclaimDeletesWeight *float32340 expungeDeletesAllowed *float32341 segmentsPerTier *float32342 deletesPctAllowed *float32343 }344 type args struct {345 reclaimDeletesWeight float32346 }347 tests := []struct {348 name string349 fields fields350 args args351 want *Policy352 }{353 {354 name: "set",355 args: args{reclaimDeletesWeight: 1},356 want: &Policy{reclaimDeletesWeight: &numberSet},357 },358 {359 name: "change",360 fields: fields{reclaimDeletesWeight: &numberSet},361 args: args{reclaimDeletesWeight: 2},362 want: &Policy{reclaimDeletesWeight: &numberChange},363 },364 }365 for _, tt := range tests {366 t.Run(tt.name, func(t *testing.T) {367 policy := &Policy{368 maxMergeAtOnceExplicit: tt.fields.maxMergeAtOnceExplicit,369 maxMergeAtOnce: tt.fields.maxMergeAtOnce,370 floorSegment: tt.fields.floorSegment,371 maxMergedSegment: tt.fields.maxMergedSegment,372 reclaimDeletesWeight: tt.fields.reclaimDeletesWeight,373 expungeDeletesAllowed: tt.fields.expungeDeletesAllowed,374 segmentsPerTier: tt.fields.segmentsPerTier,375 deletesPctAllowed: tt.fields.deletesPctAllowed,376 }377 if got := policy.SetReclaimDeletesWeight(tt.args.reclaimDeletesWeight); !reflect.DeepEqual(got, tt.want) {378 t.Errorf("SetReclaimDeletesWeight() = %v, want %v", got, tt.want)379 }380 })381 }382}383func TestPolicy_SetSegmentsPerTier(t *testing.T) {384 numberSet := float32(1)385 numberChange := float32(2)386 type fields struct {387 maxMergeAtOnceExplicit *uint388 maxMergeAtOnce *uint389 floorSegment *Size390 maxMergedSegment *Size391 reclaimDeletesWeight *float32392 expungeDeletesAllowed *float32393 segmentsPerTier *float32394 deletesPctAllowed *float32395 }396 type args struct {397 segmentsPerTier float32398 }399 tests := []struct {400 name string401 fields fields402 args args403 want *Policy404 }{405 {406 name: "set",407 args: args{segmentsPerTier: 1},408 want: &Policy{segmentsPerTier: &numberSet},409 },410 {411 name: "change",412 fields: fields{segmentsPerTier: &numberSet},413 args: args{segmentsPerTier: 2},414 want: &Policy{segmentsPerTier: &numberChange},415 },416 }417 for _, tt := range tests {418 t.Run(tt.name, func(t *testing.T) {419 policy := &Policy{420 maxMergeAtOnceExplicit: tt.fields.maxMergeAtOnceExplicit,421 maxMergeAtOnce: tt.fields.maxMergeAtOnce,422 floorSegment: tt.fields.floorSegment,423 maxMergedSegment: tt.fields.maxMergedSegment,424 reclaimDeletesWeight: tt.fields.reclaimDeletesWeight,425 expungeDeletesAllowed: tt.fields.expungeDeletesAllowed,426 segmentsPerTier: tt.fields.segmentsPerTier,427 deletesPctAllowed: tt.fields.deletesPctAllowed,428 }429 if got := policy.SetSegmentsPerTier(tt.args.segmentsPerTier); !reflect.DeepEqual(got, tt.want) {430 t.Errorf("SetSegmentsPerTier() = %v, want %v", got, tt.want)431 }432 })433 }434}435func TestPolicy_Source(t *testing.T) {436 numberFloat := float32(1)437 numberUint := uint(1)438 sizeSrc, _ := (&Size{value: "1mb"}).Source()439 type fields struct {440 maxMergeAtOnceExplicit *uint441 maxMergeAtOnce *uint442 floorSegment *Size443 maxMergedSegment *Size444 reclaimDeletesWeight *float32445 expungeDeletesAllowed *float32446 segmentsPerTier *float32447 deletesPctAllowed *float32448 }449 tests := []struct {450 name string451 fields fields452 want interface{}453 wantErr bool454 }{455 {456 name: "empty",457 want: map[string]interface{}{},458 },459 {460 name: "max_merge_at_once_explicit",461 fields: fields{maxMergeAtOnceExplicit: &numberUint},462 want: map[string]interface{}{463 "max_merge_at_once_explicit": uint(1),464 },465 },466 {467 name: "max_merge_at_once",468 fields: fields{maxMergeAtOnce: &numberUint},469 want: map[string]interface{}{470 "max_merge_at_once": uint(1),471 },472 },473 {474 name: "reclaim_deletes_weight",475 fields: fields{reclaimDeletesWeight: &numberFloat},476 want: map[string]interface{}{477 "reclaim_deletes_weight": float32(1),478 },479 },480 {481 name: "expunge_deletes_allowed",482 fields: fields{expungeDeletesAllowed: &numberFloat},483 want: map[string]interface{}{484 "expunge_deletes_allowed": float32(1),485 },486 },487 {488 name: "segments_per_tier",489 fields: fields{segmentsPerTier: &numberFloat},490 want: map[string]interface{}{491 "segments_per_tier": float32(1),492 },493 },494 {495 name: "deletes_pct_allowed",496 fields: fields{deletesPctAllowed: &numberFloat},497 want: map[string]interface{}{498 "deletes_pct_allowed": float32(1),499 },500 },501 {502 name: "floor_segment",503 fields: fields{floorSegment: &Size{value: "1mb"}},504 want: map[string]interface{}{505 "floor_segment": sizeSrc,506 },507 },508 {509 name: "max_merged_segment",510 fields: fields{maxMergedSegment: &Size{value: "1mb"}},511 want: map[string]interface{}{512 "max_merged_segment": sizeSrc,513 },514 },515 }516 for _, tt := range tests {517 t.Run(tt.name, func(t *testing.T) {518 policy := &Policy{519 maxMergeAtOnceExplicit: tt.fields.maxMergeAtOnceExplicit,520 maxMergeAtOnce: tt.fields.maxMergeAtOnce,521 floorSegment: tt.fields.floorSegment,522 maxMergedSegment: tt.fields.maxMergedSegment,523 reclaimDeletesWeight: tt.fields.reclaimDeletesWeight,524 expungeDeletesAllowed: tt.fields.expungeDeletesAllowed,525 segmentsPerTier: tt.fields.segmentsPerTier,526 deletesPctAllowed: tt.fields.deletesPctAllowed,527 }528 got, err := policy.Source()529 if (err != nil) != tt.wantErr {530 t.Errorf("Source() error = %v, wantErr %v", err, tt.wantErr)531 return532 }533 if !reflect.DeepEqual(got, tt.want) {534 t.Errorf("Source() got = %v, want %v", got, tt.want)...
impl_test.go
Source:impl_test.go
...66 {67 name: "get by id then error",68 args: args{id: id1, ctx: ctx1, mock: func() {69 s.mock.On("GetByID", mock.Anything, id1, userID1).70 Return(nil, er.ErrGetActivityByID).Once()71 }},72 wantInfo: nil,73 wantErr: true,74 },75 {76 name: "get by id then not found",77 args: args{id: id1, ctx: ctx1, mock: func() {78 s.mock.On("GetByID", mock.Anything, id1, userID1).79 Return(nil, nil).Once()80 }},81 wantInfo: nil,82 wantErr: true,83 },84 {85 name: "get by id then success",86 args: args{id: id1, ctx: ctx1, mock: func() {87 s.mock.On("GetByID", mock.Anything, id1, userID1).88 Return(act1, nil).Once()89 }},90 wantInfo: act1,91 wantErr: false,92 },93 }94 for _, tt := range tests {95 s.T().Run(tt.name, func(t *testing.T) {96 if tt.args.mock != nil {97 tt.args.mock()98 }99 gotInfo, err := s.biz.GetByID(tt.args.ctx, tt.args.id)100 if (err != nil) != tt.wantErr {101 t.Errorf("GetByID() error = %v, wantErr %v", err, tt.wantErr)102 return103 }104 if !reflect.DeepEqual(gotInfo, tt.wantInfo) {105 t.Errorf("GetByID() gotInfo = %v, want %v", gotInfo, tt.wantInfo)106 }107 s.TearDownTest()108 })109 }110}111func (s *bizSuite) Test_impl_List() {112 type args struct {113 ctx contextx.Contextx114 page int115 size int116 mock func()117 }118 tests := []struct {119 name string120 args args121 wantInfos []*event.Activity122 wantTotal int123 wantErr bool124 }{125 {126 name: "missing user info in ctx then error",127 args: args{page: 0, size: 10, ctx: contextx.Background()},128 wantInfos: nil,129 wantTotal: 0,130 wantErr: true,131 },132 {133 name: "invalid page then error",134 args: args{page: -1, size: 10, ctx: ctx1},135 wantInfos: nil,136 wantTotal: 0,137 wantErr: true,138 },139 {140 name: "invalid size then error",141 args: args{page: 1, size: -1, ctx: ctx1},142 wantInfos: nil,143 wantTotal: 0,144 wantErr: true,145 },146 {147 name: "list then error",148 args: args{page: 1, size: 10, ctx: ctx1, mock: func() {149 s.mock.On("List", mock.Anything, userID1, 10, 0).150 Return(nil, errors.New("error")).Once()151 }},152 wantInfos: nil,153 wantTotal: 0,154 wantErr: true,155 },156 {157 name: "list then not found",158 args: args{page: 1, size: 10, ctx: ctx1, mock: func() {159 s.mock.On("List", mock.Anything, userID1, 10, 0).160 Return(nil, nil).Once()161 }},162 wantInfos: nil,163 wantTotal: 0,164 wantErr: true,165 },166 {167 name: "count then error",168 args: args{page: 1, size: 10, ctx: ctx1, mock: func() {169 s.mock.On("List", mock.Anything, userID1, 10, 0).170 Return([]*event.Activity{act1}, nil).Once()171 s.mock.On("Count", mock.Anything, userID1).172 Return(0, errors.New("error")).Once()173 }},174 wantInfos: nil,175 wantTotal: 0,176 wantErr: true,177 },178 {179 name: "list and count then success",180 args: args{page: 1, size: 10, ctx: ctx1, mock: func() {181 s.mock.On("List", mock.Anything, userID1, 10, 0).182 Return([]*event.Activity{act1}, nil).Once()183 s.mock.On("Count", mock.Anything, userID1).184 Return(10, nil).Once()185 }},186 wantInfos: []*event.Activity{act1},187 wantTotal: 10,188 wantErr: false,189 },190 }191 for _, tt := range tests {192 s.T().Run(tt.name, func(t *testing.T) {193 if tt.args.mock != nil {194 tt.args.mock()195 }196 gotInfos, gotTotal, err := s.biz.List(tt.args.ctx, tt.args.page, tt.args.size)197 if (err != nil) != tt.wantErr {198 t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)199 return200 }201 if !reflect.DeepEqual(gotInfos, tt.wantInfos) {202 t.Errorf("List() gotInfos = %v, want %v", gotInfos, tt.wantInfos)203 }204 if gotTotal != tt.wantTotal {205 t.Errorf("List() gotTotal = %v, want %v", gotTotal, tt.wantTotal)206 }207 s.TearDownTest()208 })209 }210}211func (s *bizSuite) Test_impl_ChangeName() {212 type args struct {213 ctx contextx.Contextx214 id int64215 name string216 mock func()217 }218 tests := []struct {219 name string220 args args221 wantInfo *event.Activity222 wantErr bool223 }{224 {225 name: "missing user info in ctx then error",226 args: args{id: id1, name: "test", ctx: contextx.Background()},227 wantInfo: nil,228 wantErr: true,229 },230 {231 name: "missing name then error",232 args: args{id: id1, name: "", ctx: ctx1},233 wantInfo: nil,234 wantErr: true,235 },236 {237 name: "get by id then error",238 args: args{id: id1, name: "test", ctx: ctx1, mock: func() {239 s.mock.On("GetByID", mock.Anything, id1, userID1).Return(nil, errors.New("error")).Once()240 }},241 wantInfo: nil,242 wantErr: true,243 },244 {245 name: "get by id then not found",246 args: args{id: id1, name: "test", ctx: ctx1, mock: func() {247 s.mock.On("GetByID", mock.Anything, id1, userID1).Return(nil, nil).Once()248 }},249 wantInfo: nil,250 wantErr: true,251 },252 {253 name: "update then error",254 args: args{id: id1, name: "test", ctx: ctx1, mock: func() {255 s.mock.On("GetByID", mock.Anything, id1, userID1).Return(act1, nil).Once()256 s.mock.On("Update", mock.Anything, act1).Return(nil, errors.New("error")).Once()257 }},258 wantInfo: nil,259 wantErr: true,260 },261 {262 name: "update then success",263 args: args{id: id1, name: "test", ctx: ctx1, mock: func() {264 s.mock.On("GetByID", mock.Anything, id1, userID1).Return(act1, nil).Once()265 s.mock.On("Update", mock.Anything, act1).Return(act1, nil).Once()266 }},267 wantInfo: act1,268 wantErr: false,269 },270 }271 for _, tt := range tests {272 s.T().Run(tt.name, func(t *testing.T) {273 if tt.args.mock != nil {274 tt.args.mock()275 }276 gotInfo, err := s.biz.ChangeName(tt.args.ctx, tt.args.id, tt.args.name)277 if (err != nil) != tt.wantErr {278 t.Errorf("ChangeName() error = %v, wantErr %v", err, tt.wantErr)279 return280 }281 if !reflect.DeepEqual(gotInfo, tt.wantInfo) {282 t.Errorf("ChangeName() gotInfo = %v, want %v", gotInfo, tt.wantInfo)283 }284 s.TearDownTest()285 })286 }287}288func (s *bizSuite) Test_impl_Delete() {289 type args struct {290 ctx contextx.Contextx291 id int64292 mock func()293 }294 tests := []struct {295 name string296 args args297 wantErr bool298 }{299 {300 name: "missing user info in ctx then error",301 args: args{id: id1, ctx: contextx.Background()},302 wantErr: true,303 },304 {305 name: "delete then error",306 args: args{id: id1, ctx: ctx1, mock: func() {307 s.mock.On("Delete", mock.Anything, id1, userID1).Return(errors.New("error")).Once()308 }},309 wantErr: true,310 },311 {312 name: "delete then success",313 args: args{id: id1, ctx: ctx1, mock: func() {314 s.mock.On("Delete", mock.Anything, id1, userID1).Return(nil).Once()315 }},316 wantErr: false,317 },318 }319 for _, tt := range tests {320 s.T().Run(tt.name, func(t *testing.T) {321 if tt.args.mock != nil {322 tt.args.mock()323 }324 if err := s.biz.Delete(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {325 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)326 }327 s.TearDownTest()328 })329 }330}331func (s *bizSuite) Test_impl_NewWithMembers() {332 type args struct {333 ctx contextx.Contextx334 name string335 email []string336 mock func()337 }338 tests := []struct {339 name string340 args args341 wantInfo *event.Activity342 wantErr bool343 }{344 {345 name: "missing user info in ctx then error",346 args: args{name: "test", email: []string{"test"}, ctx: contextx.Background()},347 wantInfo: nil,348 wantErr: true,349 },350 {351 name: "missing emails then error",352 args: args{name: "test", email: []string{}, ctx: ctx1},353 wantInfo: nil,354 wantErr: true,355 },356 {357 name: "missing name then error",358 args: args{name: "", email: []string{"test"}, ctx: ctx1},359 wantInfo: nil,360 wantErr: true,361 },362 {363 name: "get by emails then error",364 args: args{name: "test", email: []string{"test"}, ctx: ctx1, mock: func() {365 s.mock.On("GetByEmails", mock.Anything, []string{"test"}).Return(nil, errors.New("error")).Once()366 }},367 wantInfo: nil,368 wantErr: true,369 },370 {371 name: "new an act then error",372 args: args{name: "test", email: []string{"test"}, ctx: ctx1, mock: func() {373 s.mock.On("GetByEmails", mock.Anything, []string{"test"}).Return([]*user.Member{member1}, nil).Once()374 s.mock.On("Create", mock.Anything, mock.Anything).Return(nil, errors.New("error")).Once()375 }},376 wantInfo: nil,377 wantErr: true,378 },379 {380 name: "new an act then success",381 args: args{name: "test", email: []string{"test"}, ctx: ctx1, mock: func() {382 s.mock.On("GetByEmails", mock.Anything, []string{"test"}).Return([]*user.Member{member1}, nil).Once()383 s.mock.On("Create", mock.Anything, mock.Anything).Return(act1, nil).Once()384 }},385 wantInfo: act1,386 wantErr: false,387 },388 }389 for _, tt := range tests {390 s.T().Run(tt.name, func(t *testing.T) {391 if tt.args.mock != nil {392 tt.args.mock()393 }394 gotInfo, err := s.biz.NewWithMembers(tt.args.ctx, tt.args.name, tt.args.email)395 if (err != nil) != tt.wantErr {396 t.Errorf("NewWithMembers() error = %v, wantErr %v", err, tt.wantErr)397 return398 }399 if !reflect.DeepEqual(gotInfo, tt.wantInfo) {400 t.Errorf("NewWithMembers() gotInfo = %v, want %v", gotInfo, tt.wantInfo)401 }402 s.TearDownTest()403 })404 }405}406func (s *bizSuite) Test_impl_InviteMembers() {407 type args struct {408 ctx contextx.Contextx409 id int64410 emails []string411 mock func()412 }413 tests := []struct {414 name string415 args args416 wantInfo *event.Activity417 wantErr bool418 }{419 {420 name: "get user by emails then error",421 args: args{id: id1, emails: []string{"test"}, ctx: ctx1, mock: func() {422 s.mock.On("GetByEmails", mock.Anything, []string{"test"}).Return(nil, errors.New("error")).Once()423 }},424 wantInfo: nil,425 wantErr: true,426 },427 {428 name: "invite members then error",429 args: args{id: id1, emails: []string{"test"}, ctx: ctx1, mock: func() {430 s.mock.On("GetByEmails", mock.Anything, []string{"test"}).Return([]*user.Member{member1}, nil).Once()431 s.mock.On("AddMembers", mock.Anything, id1, []*user.Member{member1}).Return(nil, errors.New("error")).Once()432 }},433 wantInfo: nil,434 wantErr: true,435 },436 {437 name: "invite members then success",438 args: args{id: id1, emails: []string{"test"}, ctx: ctx1, mock: func() {439 s.mock.On("GetByEmails", mock.Anything, []string{"test"}).Return([]*user.Member{member1}, nil).Once()440 s.mock.On("AddMembers", mock.Anything, id1, []*user.Member{member1}).Return(act1, nil).Once()441 }},442 wantInfo: act1,443 wantErr: false,444 },445 }446 for _, tt := range tests {447 s.T().Run(tt.name, func(t *testing.T) {448 if tt.args.mock != nil {449 tt.args.mock()450 }451 gotInfo, err := s.biz.InviteMembers(tt.args.ctx, tt.args.id, tt.args.emails)452 if (err != nil) != tt.wantErr {453 t.Errorf("InviteMembers() error = %v, wantErr %v", err, tt.wantErr)454 return...
mta_test.go
Source:mta_test.go
...51 m *mta52 )53 BeforeEach(func() {54 mockHomo = new(mocks.Crypto)55 mockHomo.On("Encrypt", mock.Anything).Return([]byte("encK"), nil).Once()56 var err error57 m, err = NewMta(fieldOrder, mockHomo)58 Expect(err).Should(BeNil())59 })60 AfterEach(func() {61 mockHomo.AssertExpectations(GinkgoT())62 })63 Context("Compute", func() {64 var (65 mockPubkey *mocks.Pubkey66 )67 BeforeEach(func() {68 mockPubkey = new(mocks.Pubkey)69 })70 AfterEach(func() {71 mockPubkey.AssertExpectations(GinkgoT())72 })73 It("should be ok", func() {74 msg := []byte("message")75 mockPubkey.On("VerifyEnc", msg).Return(nil).Once()76 betaRange := big.NewInt(100)77 mockPubkey.On("GetMessageRange", m.fieldOrder).Return(betaRange).Once()78 encBeta := []byte("encBeta")79 var beta []byte80 mockPubkey.On("Encrypt", mock.Anything).Run(func(args mock.Arguments) {81 beta = args[0].([]byte)82 }).Return(encBeta, nil).Once()83 r := []byte("r")84 mockPubkey.On("MulConst", msg, m.a).Return(r, nil).Once()85 mockPubkey.On("Add", r, encBeta).Return(r, nil).Once()86 gotAlpha, gotBeta, err := m.Compute(mockPubkey, msg)87 Expect(err).Should(BeNil())88 Expect(gotAlpha).Should(Equal(new(big.Int).SetBytes(r)))89 Expect(gotBeta).Should(Equal(new(big.Int).Neg(new(big.Int).SetBytes(beta))))90 })91 It("failed to add", func() {92 msg := []byte("message")93 mockPubkey.On("VerifyEnc", msg).Return(nil).Once()94 betaRange := big.NewInt(100)95 mockPubkey.On("GetMessageRange", m.fieldOrder).Return(betaRange).Once()96 encBeta := []byte("encBeta")97 mockPubkey.On("Encrypt", mock.Anything).Return(encBeta, nil).Once()98 r := []byte("r")99 mockPubkey.On("MulConst", msg, m.a).Return(r, nil).Once()100 mockPubkey.On("Add", r, encBeta).Return(nil, unknownErr).Once()101 gotAlpha, gotBeta, err := m.Compute(mockPubkey, msg)102 Expect(err).Should(Equal(unknownErr))103 Expect(gotAlpha).Should(BeNil())104 Expect(gotBeta).Should(BeNil())105 })106 It("failed to MulConst", func() {107 msg := []byte("message")108 mockPubkey.On("VerifyEnc", msg).Return(nil).Once()109 betaRange := big.NewInt(100)110 mockPubkey.On("GetMessageRange", m.fieldOrder).Return(betaRange).Once()111 encBeta := []byte("encBeta")112 mockPubkey.On("Encrypt", mock.Anything).Return(encBeta, nil).Once()113 mockPubkey.On("MulConst", msg, m.a).Return(nil, unknownErr).Once()114 gotAlpha, gotBeta, err := m.Compute(mockPubkey, msg)115 Expect(err).Should(Equal(unknownErr))116 Expect(gotAlpha).Should(BeNil())117 Expect(gotBeta).Should(BeNil())118 })119 It("failed to Encrypt", func() {120 msg := []byte("message")121 mockPubkey.On("VerifyEnc", msg).Return(nil).Once()122 betaRange := big.NewInt(100)123 mockPubkey.On("GetMessageRange", m.fieldOrder).Return(betaRange).Once()124 mockPubkey.On("Encrypt", mock.Anything).Return(nil, unknownErr).Once()125 gotAlpha, gotBeta, err := m.Compute(mockPubkey, msg)126 Expect(err).Should(Equal(unknownErr))127 Expect(gotAlpha).Should(BeNil())128 Expect(gotBeta).Should(BeNil())129 })130 It("failed to VerifyEnc", func() {131 msg := []byte("message")132 mockPubkey.On("VerifyEnc", msg).Return(unknownErr).Once()133 gotAlpha, gotBeta, err := m.Compute(mockPubkey, msg)134 Expect(err).Should(Equal(unknownErr))135 Expect(gotAlpha).Should(BeNil())136 Expect(gotBeta).Should(BeNil())137 })138 })139 It("GetProofWithCheck", func() {140 curve := elliptic.Secp256k1()141 beta := big.NewInt(3)142 proof := []byte("proof")143 mockHomo.On("GetMtaProof", curve, beta, m.a).Return(proof, nil).Once()144 got, err := m.GetProofWithCheck(curve, beta)145 Expect(err).Should(BeNil())146 Expect(got).Should(Equal(proof))147 })148 It("VerifyProofWithCheck", func() {149 curve := elliptic.Secp256k1()150 alpha := big.NewInt(3)151 proof := []byte("proof")152 p := &pt.ECPoint{}153 mockHomo.On("VerifyMtaProof", proof, curve, alpha, m.k).Return(p, nil).Once()154 got, err := m.VerifyProofWithCheck(proof, curve, alpha)155 Expect(err).Should(BeNil())156 Expect(got).Should(Equal(p))157 })158 Context("OverrideA", func() {159 It("should be ok", func() {160 newA := big.NewInt(1)161 got, err := m.OverrideA(newA)162 Expect(err).Should(BeNil())163 m.a = newA164 Expect(got).Should(Equal(m))165 })166 It("over field order", func() {167 got, err := m.OverrideA(m.fieldOrder)168 Expect(err).Should(Equal(utils.ErrNotInRange))169 Expect(got).Should(BeNil())170 })171 })172 It("GetAProof", func() {173 proof, err := m.GetAProof(curve)174 Expect(err).Should(BeNil())175 err = proof.Verify(pt.NewBase(curve))176 Expect(err).Should(BeNil())177 })178 It("Getter func", func() {179 Expect(m.GetAG(curve)).Should(Equal(pt.ScalarBaseMult(curve, m.a)))180 v := big.NewInt(10)181 Expect(m.GetProductWithK(v)).Should(Equal(new(big.Int).Mul(m.k, v)))182 })183 It("GetResult(), inconsistent alphas and betas", func() {184 got, err := m.GetResult(nil, []*big.Int{big.NewInt(1)})185 Expect(err).Should(Equal(ErrInconsistentAlphaAndBeta))186 Expect(got).Should(BeNil())187 })188 It("Decrypt(), homo decrypt fail", func() {189 data := []byte("data")190 mockHomo.On("Decrypt", data).Return(nil, unknownErr).Once()191 got, err := m.Decrypt(new(big.Int).SetBytes(data))192 Expect(err).Should(Equal(unknownErr))193 Expect(got).Should(BeNil())194 })195 DescribeTable("should be ok", func(homo1 homo.Crypto, homo2 homo.Crypto) {196 m1, err := NewMta(fieldOrder, homo1)197 Expect(err).Should(BeNil())198 Expect(m1).ShouldNot(BeNil())199 m2, err := NewMta(fieldOrder, homo2)200 Expect(err).Should(BeNil())201 Expect(m2).ShouldNot(BeNil())202 m1EncryptedK := m1.GetEncK()203 encMessage, beta, err := m2.Compute(homo1.GetPubKey(), m1EncryptedK)204 Expect(err).Should(BeNil())...
Once
Using AI Code Generation
1import (2func main() {3 onceBody := func() {4 fmt.Println("Only once")5 }6 done := make(chan bool)7 for i := 0; i < 10; i++ {8 go func() {9 once.Do(onceBody)10 }()11 }12 for i := 0; i < 10; i++ {13 }14}
Once
Using AI Code Generation
1import (2func main() {3 onceBody := func() {4 fmt.Println("Only Once")5 }6 done := make(chan bool)7 for i := 0; i < 10; i++ {8 go func() {9 once.Do(onceBody)10 }()11 }12 for i := 0; i < 10; i++ {13 }14}
Once
Using AI Code Generation
1import (2func main() {3 done := make(chan bool)4 for i := 0; i < 10; i++ {5 go func() {6 once.Do(func() {7 fmt.Println("Only once")8 })9 }()10 }11 for i := 0; i < 10; i++ {12 }13}
Once
Using AI Code Generation
1import (2func main() {3 for i := 0; i < 10; i++ {4 once.Do(func() {5 fmt.Println("Hello")6 })7 }8}
Once
Using AI Code Generation
1import (2func main() {3 wg.Add(2)4 go func() {5 defer wg.Done()6 once.Do(func() {7 fmt.Println("Hello")8 })9 }()10 go func() {11 defer wg.Done()12 once.Do(func() {13 fmt.Println("World")14 })15 }()16 wg.Wait()17}
Once
Using AI Code Generation
1import (2func main() {3 for i := 0; i < 10; i++ {4 once.Do(func() {5 fmt.Println("This is a test")6 })7 }8}
Once
Using AI Code Generation
1import (2func main() {3 once.Do(func() {4 fmt.Println("I am executing only once")5 })6 once.Do(func() {7 fmt.Println("I am executing only once")8 })9 once.Do(func() {10 fmt.Println("I am executing only once")11 })12}
Once
Using AI Code Generation
1import (2func main() {3 wg.Add(2)4 go func() {5 once.Do(func() {6 fmt.Println("Hello World")7 })8 wg.Done()9 }()10 go func() {11 once.Do(func() {12 fmt.Println("Hello World")13 })14 wg.Done()15 }()16 wg.Wait()17}
Once
Using AI Code Generation
1import (2func main() {3 for i := 0; i < 10; i++ {4 wg.Add(1)5 go func(i int) {6 once.Do(func() {7 fmt.Println("Hello World")8 })9 fmt.Println("I am ", i)10 wg.Done()11 }(i)12 }13 wg.Wait()14}
Once
Using AI Code Generation
1import (2func main() {3 once.Do(func() {4 fmt.Println("Hello World")5 })6 once.Do(func() {7 fmt.Println("Hello World")8 })9}
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!!