Best Syzkaller code snippet using mocks.CreateInstance
query_test.go
Source:query_test.go
...188 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {189 t.Helper()190 m := new(mocks.MockDynamoDBAPI)191 ctx = context.WithValue(ctx, newClientKey, m)192 m.On("CreateInstance").Return(m)193 table := describeTableOutputFixture(t, false)194 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{195 TableName: aws.String("TEST"),196 }).Return(table, nil)197 condition := expression.KeyEqual(198 expression.Key("TEST_PARTITION_ATTRIBUTE"),199 expression.Value("TEST_PARTITION_VALUE_1"),200 )201 builder := expression.NewBuilder().WithKeyCondition(condition)202 expr, err := builder.Build()203 if err != nil {204 t.Fatalf("expression build error: %v", err)205 }206 input := &dynamodb.QueryInput{207 TableName: aws.String("TEST"),208 ExpressionAttributeNames: expr.Names(),209 ExpressionAttributeValues: expr.Values(),210 KeyConditionExpression: expr.KeyCondition(),211 }212 m.QueryAPIClient.On("Query", ctx, input).Return(queryOutputFixture(t, ""), nil)213 return m214 },215 wantW: jsonFixture(t, []map[string]interface{}{216 {217 "TEST_PARTITION_ATTRIBUTE": "TEST_PARTITION_VALUE_1",218 "TEST_SORT_ATTRIBUTE": "TEST_SORT_VALUE_1",219 "TEST_ATTRIBUTE_1": "TEST_ATTRIBUTE_1_VALUE_1",220 "TEST_ATTRIBUTE_2": 1,221 },222 }),223 },224 {225 name: "Query with partition and sort key",226 args: args{227 ctx: context.Background(),228 tableName: "TEST",229 partitionValue: "TEST_PARTITION_VALUE_1",230 sortCondition: "= TEST_SORT_VALUE_1",231 },232 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {233 t.Helper()234 m := new(mocks.MockDynamoDBAPI)235 ctx = context.WithValue(ctx, newClientKey, m)236 m.On("CreateInstance").Return(m)237 table := describeTableOutputFixture(t, false)238 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{239 TableName: aws.String("TEST"),240 }).Return(table, nil)241 condition := expression.KeyEqual(242 expression.Key("TEST_PARTITION_ATTRIBUTE"),243 expression.Value("TEST_PARTITION_VALUE_1"),244 ).And(expression.KeyEqual(245 expression.Key("TEST_SORT_ATTRIBUTE"),246 expression.Value("TEST_SORT_VALUE_1"),247 ))248 builder := expression.NewBuilder().WithKeyCondition(condition)249 expr, err := builder.Build()250 if err != nil {251 t.Fatalf("expression build error: %v", err)252 }253 input := &dynamodb.QueryInput{254 TableName: aws.String("TEST"),255 ExpressionAttributeNames: expr.Names(),256 ExpressionAttributeValues: expr.Values(),257 KeyConditionExpression: expr.KeyCondition(),258 }259 m.QueryAPIClient.On("Query", ctx, input).Return(queryOutputFixture(t, ""), nil)260 return m261 },262 wantW: jsonFixture(t, []map[string]interface{}{263 {264 "TEST_PARTITION_ATTRIBUTE": "TEST_PARTITION_VALUE_1",265 "TEST_SORT_ATTRIBUTE": "TEST_SORT_VALUE_1",266 "TEST_ATTRIBUTE_1": "TEST_ATTRIBUTE_1_VALUE_1",267 "TEST_ATTRIBUTE_2": 1,268 },269 }),270 },271 {272 name: "Query with partition key and filter",273 args: args{274 ctx: context.Background(),275 tableName: "TEST",276 partitionValue: "TEST_PARTITION_VALUE_1",277 filterCondition: "TEST_ATTRIBUTE_2,N = 1",278 },279 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {280 t.Helper()281 m := new(mocks.MockDynamoDBAPI)282 ctx = context.WithValue(ctx, newClientKey, m)283 m.On("CreateInstance").Return(m)284 table := describeTableOutputFixture(t, false)285 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{286 TableName: aws.String("TEST"),287 }).Return(table, nil)288 keyCondition := expression.KeyEqual(289 expression.Key("TEST_PARTITION_ATTRIBUTE"),290 expression.Value("TEST_PARTITION_VALUE_1"),291 )292 filterCondition := expression.Equal(293 expression.Name("TEST_ATTRIBUTE_2"),294 expression.Value(1),295 )296 builder := expression.NewBuilder().WithKeyCondition(keyCondition).WithCondition(filterCondition)297 expr, err := builder.Build()298 if err != nil {299 t.Fatalf("expression build error: %v", err)300 }301 input := &dynamodb.QueryInput{302 TableName: aws.String("TEST"),303 ExpressionAttributeNames: expr.Names(),304 ExpressionAttributeValues: expr.Values(),305 KeyConditionExpression: expr.KeyCondition(),306 FilterExpression: expr.Condition(),307 }308 m.QueryAPIClient.On("Query", ctx, input).Return(queryOutputFixture(t, ""), nil)309 return m310 },311 wantW: jsonFixture(t, []map[string]interface{}{312 {313 "TEST_PARTITION_ATTRIBUTE": "TEST_PARTITION_VALUE_1",314 "TEST_SORT_ATTRIBUTE": "TEST_SORT_VALUE_1",315 "TEST_ATTRIBUTE_1": "TEST_ATTRIBUTE_1_VALUE_1",316 "TEST_ATTRIBUTE_2": 1,317 },318 }),319 },320 {321 name: "Query with global secondary index",322 args: args{323 ctx: context.Background(),324 tableName: "TEST",325 partitionValue: "TEST_ATTRIBUTE_1_VALUE_1",326 index: "TEST_GSI",327 },328 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {329 t.Helper()330 m := new(mocks.MockDynamoDBAPI)331 ctx = context.WithValue(ctx, newClientKey, m)332 m.On("CreateInstance").Return(m)333 table := describeTableOutputFixture(t, true)334 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{335 TableName: aws.String("TEST"),336 }).Return(table, nil)337 condition := expression.KeyEqual(338 expression.Key("TEST_ATTRIBUTE_1"),339 expression.Value("TEST_ATTRIBUTE_1_VALUE_1"),340 )341 builder := expression.NewBuilder().WithKeyCondition(condition)342 expr, err := builder.Build()343 if err != nil {344 t.Fatalf("expression build error: %v", err)345 }346 input := &dynamodb.QueryInput{347 TableName: aws.String("TEST"),348 ExpressionAttributeNames: expr.Names(),349 ExpressionAttributeValues: expr.Values(),350 KeyConditionExpression: expr.KeyCondition(),351 IndexName: aws.String("TEST_GSI"),352 }353 m.QueryAPIClient.On("Query", ctx, input).Return(queryOutputFixture(t, ""), nil)354 return m355 },356 wantW: jsonFixture(t, []map[string]interface{}{357 {358 "TEST_PARTITION_ATTRIBUTE": "TEST_PARTITION_VALUE_1",359 "TEST_SORT_ATTRIBUTE": "TEST_SORT_VALUE_1",360 "TEST_ATTRIBUTE_1": "TEST_ATTRIBUTE_1_VALUE_1",361 "TEST_ATTRIBUTE_2": 1,362 },363 }),364 },365 {366 name: "Query with projection",367 args: args{368 ctx: context.Background(),369 tableName: "TEST",370 partitionValue: "TEST_PARTITION_VALUE_1",371 sortCondition: "= TEST_SORT_VALUE_1",372 projection: "TEST_ATTRIBUTE_1 TEST_ATTRIBUTE_2",373 },374 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {375 t.Helper()376 m := new(mocks.MockDynamoDBAPI)377 ctx = context.WithValue(ctx, newClientKey, m)378 m.On("CreateInstance").Return(m)379 table := describeTableOutputFixture(t, false)380 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{381 TableName: aws.String("TEST"),382 }).Return(table, nil)383 condition := expression.KeyEqual(384 expression.Key("TEST_PARTITION_ATTRIBUTE"),385 expression.Value("TEST_PARTITION_VALUE_1"),386 ).And(expression.KeyEqual(387 expression.Key("TEST_SORT_ATTRIBUTE"),388 expression.Value("TEST_SORT_VALUE_1"),389 ))390 pj := expression.NamesList(391 expression.Name("TEST_ATTRIBUTE_1"),392 expression.Name("TEST_ATTRIBUTE_2"),393 )394 builder := expression.NewBuilder().WithKeyCondition(condition).WithProjection(pj)395 expr, err := builder.Build()396 if err != nil {397 t.Fatalf("expression build error: %v", err)398 }399 input := &dynamodb.QueryInput{400 TableName: aws.String("TEST"),401 ExpressionAttributeNames: expr.Names(),402 ExpressionAttributeValues: expr.Values(),403 KeyConditionExpression: expr.KeyCondition(),404 ProjectionExpression: expr.Projection(),405 }406 m.QueryAPIClient.On("Query", ctx, input).407 Return(queryOutputFixture(t, "TEST_ATTRIBUTE_1 TEST_ATTRIBUTE_2"), nil)408 return m409 },410 wantW: jsonFixture(t, []map[string]interface{}{411 {412 "TEST_ATTRIBUTE_1": "TEST_ATTRIBUTE_1_VALUE_1",413 "TEST_ATTRIBUTE_2": 1,414 },415 }),416 },417 {418 name: "Query error",419 args: args{420 ctx: context.Background(),421 tableName: "TEST",422 partitionValue: "TEST_PARTITION_VALUE_1",423 },424 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {425 t.Helper()426 m := new(mocks.MockDynamoDBAPI)427 ctx = context.WithValue(ctx, newClientKey, m)428 m.On("CreateInstance").Return(m)429 table := describeTableOutputFixture(t, false)430 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{431 TableName: aws.String("TEST"),432 }).Return(table, nil)433 condition := expression.KeyEqual(434 expression.Key("TEST_PARTITION_ATTRIBUTE"),435 expression.Value("TEST_PARTITION_VALUE_1"),436 )437 builder := expression.NewBuilder().WithKeyCondition(condition)438 expr, err := builder.Build()439 if err != nil {440 t.Fatalf("expression build error: %v", err)441 }442 input := &dynamodb.QueryInput{443 TableName: aws.String("TEST"),444 ExpressionAttributeNames: expr.Names(),445 ExpressionAttributeValues: expr.Values(),446 KeyConditionExpression: expr.KeyCondition(),447 }448 m.QueryAPIClient.On("Query", ctx, input).Return(nil, fmt.Errorf("query error"))449 return m450 },451 wantErr: true,452 },453 {454 name: "Invalid index",455 args: args{456 ctx: context.Background(),457 tableName: "TEST",458 partitionValue: "TEST_PARTITION_VALUE_1",459 index: "INVALID",460 },461 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {462 t.Helper()463 m := new(mocks.MockDynamoDBAPI)464 ctx = context.WithValue(ctx, newClientKey, m)465 m.On("CreateInstance").Return(m)466 table := describeTableOutputFixture(t, false)467 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{468 TableName: aws.String("TEST"),469 }).Return(table, nil)470 return m471 },472 wantErr: true,473 },474 {475 name: "Invalid partition value",476 args: args{477 ctx: context.Background(),478 tableName: "TEST",479 partitionValue: "ERROR",480 },481 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {482 t.Helper()483 m := new(mocks.MockDynamoDBAPI)484 ctx = context.WithValue(ctx, newClientKey, m)485 m.On("CreateInstance").Return(m)486 table := &dynamodb.DescribeTableOutput{487 Table: &types.TableDescription{488 TableName: aws.String("TEST"),489 AttributeDefinitions: []types.AttributeDefinition{490 {491 AttributeName: aws.String("TEST_PARTITION_ATTRIBUTE"),492 AttributeType: types.ScalarAttributeTypeN,493 },494 },495 KeySchema: []types.KeySchemaElement{496 {497 AttributeName: aws.String("TEST_PARTITION_ATTRIBUTE"),498 KeyType: types.KeyTypeHash,499 },500 },501 },502 }503 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{504 TableName: aws.String("TEST"),505 }).Return(table, nil)506 return m507 },508 wantErr: true,509 },510 {511 name: "Invalid sort condition",512 args: args{513 ctx: context.Background(),514 tableName: "TEST",515 partitionValue: "TEST_PARTITION_VALUE_1",516 sortCondition: "ERROR",517 },518 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {519 t.Helper()520 m := new(mocks.MockDynamoDBAPI)521 ctx = context.WithValue(ctx, newClientKey, m)522 m.On("CreateInstance").Return(m)523 table := describeTableOutputFixture(t, false)524 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{525 TableName: aws.String("TEST"),526 }).Return(table, nil)527 return m528 },529 wantErr: true,530 },531 {532 name: "Invalid filter condition",533 args: args{534 ctx: context.Background(),535 tableName: "TEST",536 partitionValue: "TEST_PARTITION_VALUE_1",537 filterCondition: "ERROR = ERROR",538 },539 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {540 t.Helper()541 m := new(mocks.MockDynamoDBAPI)542 ctx = context.WithValue(ctx, newClientKey, m)543 m.On("CreateInstance").Return(m)544 table := describeTableOutputFixture(t, false)545 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{546 TableName: aws.String("TEST"),547 }).Return(table, nil)548 return m549 },550 wantErr: true,551 },552 {553 name: "Error DescribeTable",554 args: args{555 ctx: context.Background(),556 tableName: "TEST",557 partitionValue: "TEST_PARTITION_VALUE_1",558 },559 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {560 t.Helper()561 m := new(mocks.MockDynamoDBAPI)562 ctx = context.WithValue(ctx, newClientKey, m)563 m.On("CreateInstance").Return(m)564 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{565 TableName: aws.String("TEST"),566 }).Return(nil, fmt.Errorf("cannot describe table"))567 return m568 },569 wantErr: true,570 },571 }572 for _, tt := range tests {573 t.Run(tt.name, func(t *testing.T) {574 mock := tt.mocking(t, tt.args.ctx)575 i := &Instance{576 NewClient: mock,577 }...
put_test.go
Source:put_test.go
...36 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {37 t.Helper()38 m := new(mocks.MockDynamoDBAPI)39 ctx = context.WithValue(ctx, newClientKey, m)40 m.On("CreateInstance").Return(m)41 input := &dynamodb.PutItemInput{42 TableName: aws.String("TEST"),43 Item: map[string]types.AttributeValue{44 "TEST_KEY1": &types.AttributeValueMemberS{45 Value: "TEST_VALUE1",46 },47 },48 }49 m.PutItemClient.On("PutItem", ctx, input).Return(&dynamodb.PutItemOutput{}, nil)50 return m51 },52 wantW: "{\n \"unprocessed\": []\n}\n",53 },54 {55 name: "Put 2 data",56 args: args{57 ctx: context.Background(),58 tableName: "TEST",59 item: "{\"TEST_KEY1\":\"TEST_VALUE1\",\"TEST_KEY2\":[1,2,3]}",60 },61 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {62 t.Helper()63 m := new(mocks.MockDynamoDBAPI)64 ctx = context.WithValue(ctx, newClientKey, m)65 m.On("CreateInstance").Return(m)66 input := &dynamodb.PutItemInput{67 TableName: aws.String("TEST"),68 Item: map[string]types.AttributeValue{69 "TEST_KEY1": &types.AttributeValueMemberS{70 Value: "TEST_VALUE1",71 },72 "TEST_KEY2": &types.AttributeValueMemberNS{73 Value: []string{"1", "2", "3"},74 },75 },76 }77 m.PutItemClient.On("PutItem", ctx, input).Return(&dynamodb.PutItemOutput{}, nil)78 return m79 },80 wantW: "{\n \"unprocessed\": []\n}\n",81 },82 {83 name: "Put data with map",84 args: args{85 ctx: context.Background(),86 tableName: "TEST",87 item: "{\"TEST_KEY1\":\"T1\",\"TEST_KEY2\":{" +88 "\"TEST_KEY3\":{\"TEST_KEY4\":[1,2],\"TEST_KEY5\":[\"T5\"]},\"TEST_KEY6\":{\"TEST_KEY7\":1}" +89 "},\"TEST_KEY8\":false}",90 },91 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {92 t.Helper()93 m := new(mocks.MockDynamoDBAPI)94 ctx = context.WithValue(ctx, newClientKey, m)95 m.On("CreateInstance").Return(m)96 input := &dynamodb.PutItemInput{97 TableName: aws.String("TEST"),98 Item: map[string]types.AttributeValue{99 "TEST_KEY1": &types.AttributeValueMemberS{100 Value: "T1",101 },102 "TEST_KEY2": &types.AttributeValueMemberM{103 Value: map[string]types.AttributeValue{104 "TEST_KEY3": &types.AttributeValueMemberM{105 Value: map[string]types.AttributeValue{106 "TEST_KEY4": &types.AttributeValueMemberNS{107 Value: []string{"1", "2"},108 },109 "TEST_KEY5": &types.AttributeValueMemberSS{110 Value: []string{"T5"},111 },112 },113 },114 "TEST_KEY6": &types.AttributeValueMemberM{115 Value: map[string]types.AttributeValue{116 "TEST_KEY7": &types.AttributeValueMemberN{117 Value: "1",118 },119 },120 },121 },122 },123 "TEST_KEY8": &types.AttributeValueMemberBOOL{124 Value: false,125 },126 },127 }128 m.PutItemClient.On("PutItem", ctx, input).Return(&dynamodb.PutItemOutput{}, nil)129 return m130 },131 wantW: "{\n \"unprocessed\": []\n}\n",132 },133 {134 name: "Put data with list",135 args: args{136 ctx: context.Background(),137 tableName: "TEST",138 item: "{\"TEST_KEY1\":[\"T1\", true, 1]}",139 },140 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {141 t.Helper()142 m := new(mocks.MockDynamoDBAPI)143 ctx = context.WithValue(ctx, newClientKey, m)144 m.On("CreateInstance").Return(m)145 input := &dynamodb.PutItemInput{146 TableName: aws.String("TEST"),147 Item: map[string]types.AttributeValue{148 "TEST_KEY1": &types.AttributeValueMemberL{149 Value: []types.AttributeValue{150 &types.AttributeValueMemberS{151 Value: "T1",152 },153 &types.AttributeValueMemberBOOL{154 Value: true,155 },156 &types.AttributeValueMemberN{157 Value: "1",158 },159 },160 },161 },162 }163 m.PutItemClient.On("PutItem", ctx, input).Return(&dynamodb.PutItemOutput{}, nil)164 return m165 },166 wantW: "{\n \"unprocessed\": []\n}\n",167 },168 {169 name: "Put data with list include null",170 args: args{171 ctx: context.Background(),172 tableName: "TEST",173 item: "{\"TEST_KEY1\":[null, \"T1\"]}",174 },175 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {176 t.Helper()177 m := new(mocks.MockDynamoDBAPI)178 ctx = context.WithValue(ctx, newClientKey, m)179 m.On("CreateInstance").Return(m)180 input := &dynamodb.PutItemInput{181 TableName: aws.String("TEST"),182 Item: map[string]types.AttributeValue{183 "TEST_KEY1": &types.AttributeValueMemberL{184 Value: []types.AttributeValue{185 &types.AttributeValueMemberNULL{186 Value: true,187 },188 &types.AttributeValueMemberS{189 Value: "T1",190 },191 },192 },193 },194 }195 m.PutItemClient.On("PutItem", ctx, input).Return(&dynamodb.PutItemOutput{}, nil)196 return m197 },198 wantW: "{\n \"unprocessed\": []\n}\n",199 },200 {201 name: "Put data with null",202 args: args{203 ctx: context.Background(),204 tableName: "TEST",205 item: "{\"TEST_KEY1\":null}",206 },207 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {208 t.Helper()209 m := new(mocks.MockDynamoDBAPI)210 ctx = context.WithValue(ctx, newClientKey, m)211 m.On("CreateInstance").Return(m)212 input := &dynamodb.PutItemInput{213 TableName: aws.String("TEST"),214 Item: map[string]types.AttributeValue{215 "TEST_KEY1": &types.AttributeValueMemberNULL{216 Value: true,217 },218 },219 }220 m.PutItemClient.On("PutItem", ctx, input).Return(&dynamodb.PutItemOutput{}, nil)221 return m222 },223 wantW: "{\n \"unprocessed\": []\n}\n",224 },225 {226 name: "Put 1 item with array",227 args: args{228 ctx: context.Background(),229 tableName: "TEST",230 item: "[{\"TEST_KEY1\":\"TEST_VALUE1\"}]",231 },232 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {233 t.Helper()234 m := new(mocks.MockDynamoDBAPI)235 ctx = context.WithValue(ctx, newClientKey, m)236 m.On("CreateInstance").Return(m)237 input := &dynamodb.BatchWriteItemInput{238 RequestItems: map[string][]types.WriteRequest{239 "TEST": {240 {241 PutRequest: &types.PutRequest{242 Item: map[string]types.AttributeValue{243 "TEST_KEY1": &types.AttributeValueMemberS{244 Value: "TEST_VALUE1",245 },246 },247 },248 },249 },250 },251 }252 m.BatchWriteItemClient.On("BatchWriteItem", ctx, input).Return(&dynamodb.BatchWriteItemOutput{253 UnprocessedItems: map[string][]types.WriteRequest{},254 }, nil)255 return m256 },257 wantW: "{\n \"unprocessed\": []\n}\n",258 },259 {260 name: "Put 2 items",261 args: args{262 ctx: context.Background(),263 tableName: "TEST",264 item: "[{\"TEST_KEY1\":\"TEST_VALUE1\"}," +265 "{\"TEST_KEY2\":\"TEST_VALUE2\", \"TEST_KEY3\":[\"TEST_VALUE31\",32,true]}]",266 },267 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {268 t.Helper()269 m := new(mocks.MockDynamoDBAPI)270 ctx = context.WithValue(ctx, newClientKey, m)271 m.On("CreateInstance").Return(m)272 input := &dynamodb.BatchWriteItemInput{273 RequestItems: map[string][]types.WriteRequest{274 "TEST": {275 {276 PutRequest: &types.PutRequest{277 Item: map[string]types.AttributeValue{278 "TEST_KEY1": &types.AttributeValueMemberS{279 Value: "TEST_VALUE1",280 },281 },282 },283 },284 {285 PutRequest: &types.PutRequest{286 Item: map[string]types.AttributeValue{287 "TEST_KEY2": &types.AttributeValueMemberS{288 Value: "TEST_VALUE2",289 },290 "TEST_KEY3": &types.AttributeValueMemberL{291 Value: []types.AttributeValue{292 &types.AttributeValueMemberS{293 Value: "TEST_VALUE31",294 },295 &types.AttributeValueMemberN{296 Value: "32",297 },298 &types.AttributeValueMemberBOOL{299 Value: true,300 },301 },302 },303 },304 },305 },306 },307 },308 }309 m.BatchWriteItemClient.On("BatchWriteItem", ctx, input).Return(&dynamodb.BatchWriteItemOutput{310 UnprocessedItems: map[string][]types.WriteRequest{},311 }, nil)312 return m313 },314 wantW: "{\n \"unprocessed\": []\n}\n",315 },316 {317 name: "Put item with 1 retry",318 args: args{319 ctx: context.Background(),320 tableName: "TEST",321 item: "[{\"TEST_KEY1\":\"TEST_VALUE1\"}]",322 },323 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {324 t.Helper()325 m := new(mocks.MockDynamoDBAPI)326 ctx = context.WithValue(ctx, newClientKey, m)327 m.On("CreateInstance").Return(m)328 reqItems := map[string][]types.WriteRequest{329 "TEST": {330 {331 PutRequest: &types.PutRequest{332 Item: map[string]types.AttributeValue{333 "TEST_KEY1": &types.AttributeValueMemberS{334 Value: "TEST_VALUE1",335 },336 },337 },338 },339 },340 }341 input := &dynamodb.BatchWriteItemInput{342 RequestItems: reqItems,343 }344 m.BatchWriteItemClient.On("BatchWriteItem", ctx, input).Return(&dynamodb.BatchWriteItemOutput{345 UnprocessedItems: reqItems,346 }, nil).Once()347 m.BatchWriteItemClient.On("BatchWriteItem", ctx, input).Return(&dynamodb.BatchWriteItemOutput{348 UnprocessedItems: map[string][]types.WriteRequest{},349 }, nil)350 return m351 },352 wantW: "{\n \"unprocessed\": []\n}\n",353 },354 {355 name: "Put item, failed retry",356 args: args{357 ctx: context.Background(),358 tableName: "TEST",359 item: "[{\"TEST_KEY1\":\"TEST_VALUE1\"}]",360 },361 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {362 t.Helper()363 m := new(mocks.MockDynamoDBAPI)364 ctx = context.WithValue(ctx, newClientKey, m)365 m.On("CreateInstance").Return(m)366 reqItems := map[string][]types.WriteRequest{367 "TEST": {368 {369 PutRequest: &types.PutRequest{370 Item: map[string]types.AttributeValue{371 "TEST_KEY1": &types.AttributeValueMemberS{372 Value: "TEST_VALUE1",373 },374 },375 },376 },377 },378 }379 input := &dynamodb.BatchWriteItemInput{380 RequestItems: reqItems,381 }382 m.BatchWriteItemClient.On("BatchWriteItem", ctx, input).Return(&dynamodb.BatchWriteItemOutput{383 UnprocessedItems: reqItems,384 }, nil)385 return m386 },387 wantW: "{\n" +388 strings.Repeat(" ", 2) + "\"unprocessed\": [\n" +389 strings.Repeat(" ", 4) + "{\n" +390 strings.Repeat(" ", 6) + "\"DeleteRequest\": null,\n" +391 strings.Repeat(" ", 6) + "\"PutRequest\": {\n" +392 strings.Repeat(" ", 8) + "\"Item\": {\n" +393 strings.Repeat(" ", 10) + "\"TEST_KEY1\": {\n" +394 strings.Repeat(" ", 12) + "\"Value\": \"TEST_VALUE1\"\n" +395 strings.Repeat(" ", 10) + "}\n" +396 strings.Repeat(" ", 8) + "}\n" +397 strings.Repeat(" ", 6) + "}\n" +398 strings.Repeat(" ", 4) + "}\n" +399 strings.Repeat(" ", 2) + "]\n" +400 "}\n",401 },402 {403 name: "Put item from file",404 args: args{405 ctx: context.Background(),406 tableName: "TEST",407 fileName: "TEST.json",408 f: func(s string) (string, error) {409 return "{\"TEST_KEY1\":\"TEST_VALUE1\"}", nil410 },411 },412 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {413 t.Helper()414 m := new(mocks.MockDynamoDBAPI)415 ctx = context.WithValue(ctx, newClientKey, m)416 m.On("CreateInstance").Return(m)417 input := &dynamodb.PutItemInput{418 TableName: aws.String("TEST"),419 Item: map[string]types.AttributeValue{420 "TEST_KEY1": &types.AttributeValueMemberS{421 Value: "TEST_VALUE1",422 },423 },424 }425 m.PutItemClient.On("PutItem", ctx, input).Return(&dynamodb.PutItemOutput{}, nil)426 return m427 },428 wantW: "{\n \"unprocessed\": []\n}\n",429 },430 {431 name: "Put items from file",432 args: args{433 ctx: context.Background(),434 tableName: "TEST",435 fileName: "TEST.json",436 f: func(s string) (string, error) {437 return "[{\"TEST_KEY1\":\"TEST_VALUE1\"},{\"TEST_KEY2\":[true]}]", nil438 },439 },440 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {441 t.Helper()442 m := new(mocks.MockDynamoDBAPI)443 ctx = context.WithValue(ctx, newClientKey, m)444 m.On("CreateInstance").Return(m)445 input := &dynamodb.BatchWriteItemInput{446 RequestItems: map[string][]types.WriteRequest{447 "TEST": {448 {449 PutRequest: &types.PutRequest{450 Item: map[string]types.AttributeValue{451 "TEST_KEY1": &types.AttributeValueMemberS{452 Value: "TEST_VALUE1",453 },454 },455 },456 },457 {458 PutRequest: &types.PutRequest{459 Item: map[string]types.AttributeValue{460 "TEST_KEY2": &types.AttributeValueMemberL{461 Value: []types.AttributeValue{462 &types.AttributeValueMemberBOOL{463 Value: true,464 },465 },466 },467 },468 },469 },470 },471 },472 }473 m.BatchWriteItemClient.On("BatchWriteItem", ctx, input).Return(&dynamodb.BatchWriteItemOutput{474 UnprocessedItems: map[string][]types.WriteRequest{},475 }, nil)476 return m477 },478 wantW: "{\n \"unprocessed\": []\n}\n",479 },480 {481 name: "Error unmarshal JSON",482 args: args{483 ctx: context.Background(),484 tableName: "TEST",485 item: "ERROR",486 },487 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {488 t.Helper()489 m := new(mocks.MockDynamoDBAPI)490 return m491 },492 wantErr: true,493 },494 {495 name: "Error put item",496 args: args{497 ctx: context.Background(),498 tableName: "TEST",499 item: "{\"ERROR\":\"ERROR\"}",500 },501 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {502 t.Helper()503 m := new(mocks.MockDynamoDBAPI)504 ctx = context.WithValue(ctx, newClientKey, m)505 m.On("CreateInstance").Return(m)506 input := &dynamodb.PutItemInput{507 TableName: aws.String("TEST"),508 Item: map[string]types.AttributeValue{509 "ERROR": &types.AttributeValueMemberS{510 Value: "ERROR",511 },512 },513 }514 m.PutItemClient.On("PutItem", ctx, input).Return(nil, fmt.Errorf("cannot put items"))515 return m516 },517 wantErr: true,518 },519 {520 name: "Error batch write item",521 args: args{522 ctx: context.Background(),523 tableName: "TEST",524 item: "[{\"ERROR\":\"ERROR\"}]",525 },526 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {527 t.Helper()528 m := new(mocks.MockDynamoDBAPI)529 ctx = context.WithValue(ctx, newClientKey, m)530 m.On("CreateInstance").Return(m)531 input := &dynamodb.BatchWriteItemInput{532 RequestItems: map[string][]types.WriteRequest{533 "TEST": {534 {535 PutRequest: &types.PutRequest{536 Item: map[string]types.AttributeValue{537 "ERROR": &types.AttributeValueMemberS{538 Value: "ERROR",539 },540 },541 },542 },543 },544 },...
scan_test.go
Source:scan_test.go
...35 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {36 t.Helper()37 m := new(mocks.MockDynamoDBAPI)38 ctx = context.WithValue(ctx, newClientKey, m)39 m.On("CreateInstance").Return(m)40 table := describeTableOutputFixture(t, false)41 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{42 TableName: aws.String("TEST"),43 }).Return(table, nil)44 input := &dynamodb.ScanInput{45 TableName: aws.String("TEST"),46 }47 m.ScanAPIClient.On("Scan", ctx, input).Return(scanOutputFixture(t, ""), nil)48 return m49 },50 wantW: jsonFixture(t, []map[string]interface{}{51 {52 "TEST_PARTITION_ATTRIBUTE": "TEST_PARTITION_VALUE_1",53 "TEST_SORT_ATTRIBUTE": "TEST_SORT_VALUE_1",54 "TEST_ATTRIBUTE_1": "TEST_ATTRIBUTE_1_VALUE_1",55 "TEST_ATTRIBUTE_2": 1,56 },57 }),58 },59 {60 name: "Scan with filter",61 args: args{62 ctx: context.Background(),63 tableName: "TEST",64 filterCondition: "TEST_ATTRIBUTE_2,N = 1",65 },66 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {67 t.Helper()68 m := new(mocks.MockDynamoDBAPI)69 ctx = context.WithValue(ctx, newClientKey, m)70 m.On("CreateInstance").Return(m)71 table := describeTableOutputFixture(t, false)72 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{73 TableName: aws.String("TEST"),74 }).Return(table, nil)75 filterCondition := expression.Equal(76 expression.Name("TEST_ATTRIBUTE_2"),77 expression.Value(1),78 )79 expr, err := expression.NewBuilder().WithCondition(filterCondition).Build()80 if err != nil {81 t.Fatalf("expression build error: %v", err)82 }83 input := &dynamodb.ScanInput{84 TableName: aws.String("TEST"),85 ExpressionAttributeNames: expr.Names(),86 ExpressionAttributeValues: expr.Values(),87 FilterExpression: expr.Condition(),88 }89 m.ScanAPIClient.On("Scan", ctx, input).Return(scanOutputFixture(t, ""), nil)90 return m91 },92 wantW: jsonFixture(t, []map[string]interface{}{93 {94 "TEST_PARTITION_ATTRIBUTE": "TEST_PARTITION_VALUE_1",95 "TEST_SORT_ATTRIBUTE": "TEST_SORT_VALUE_1",96 "TEST_ATTRIBUTE_1": "TEST_ATTRIBUTE_1_VALUE_1",97 "TEST_ATTRIBUTE_2": 1,98 },99 }),100 },101 {102 name: "Scan with projection",103 args: args{104 ctx: context.Background(),105 tableName: "TEST",106 projection: "TEST_ATTRIBUTE_1 TEST_ATTRIBUTE_2",107 },108 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {109 t.Helper()110 m := new(mocks.MockDynamoDBAPI)111 ctx = context.WithValue(ctx, newClientKey, m)112 m.On("CreateInstance").Return(m)113 table := describeTableOutputFixture(t, false)114 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{115 TableName: aws.String("TEST"),116 }).Return(table, nil)117 pj := expression.NamesList(118 expression.Name("TEST_ATTRIBUTE_1"),119 expression.Name("TEST_ATTRIBUTE_2"),120 )121 expr, err := expression.NewBuilder().WithProjection(pj).Build()122 if err != nil {123 t.Fatalf("expression build error: %v", err)124 }125 input := &dynamodb.ScanInput{126 TableName: aws.String("TEST"),127 ExpressionAttributeNames: expr.Names(),128 ExpressionAttributeValues: expr.Values(),129 ProjectionExpression: expr.Projection(),130 }131 m.ScanAPIClient.On("Scan", ctx, input).132 Return(scanOutputFixture(t, "TEST_ATTRIBUTE_1 TEST_ATTRIBUTE_2"), nil)133 return m134 },135 wantW: jsonFixture(t, []map[string]interface{}{136 {137 "TEST_ATTRIBUTE_1": "TEST_ATTRIBUTE_1_VALUE_1",138 "TEST_ATTRIBUTE_2": 1,139 },140 }),141 },142 {143 name: "Scan error",144 args: args{145 ctx: context.Background(),146 tableName: "TEST",147 },148 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {149 t.Helper()150 m := new(mocks.MockDynamoDBAPI)151 ctx = context.WithValue(ctx, newClientKey, m)152 m.On("CreateInstance").Return(m)153 table := describeTableOutputFixture(t, false)154 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{155 TableName: aws.String("TEST"),156 }).Return(table, nil)157 input := &dynamodb.ScanInput{158 TableName: aws.String("TEST"),159 }160 m.ScanAPIClient.On("Scan", ctx, input).Return(nil, fmt.Errorf("scan error"))161 return m162 },163 wantErr: true,164 },165 {166 name: "Invalid filter condition",167 args: args{168 ctx: context.Background(),169 tableName: "TEST",170 filterCondition: "ERROR = ERROR",171 },172 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {173 t.Helper()174 m := new(mocks.MockDynamoDBAPI)175 ctx = context.WithValue(ctx, newClientKey, m)176 m.On("CreateInstance").Return(m)177 table := describeTableOutputFixture(t, false)178 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{179 TableName: aws.String("TEST"),180 }).Return(table, nil)181 return m182 },183 wantErr: true,184 },185 {186 name: "Error DescribeTable",187 args: args{188 ctx: context.Background(),189 tableName: "TEST",190 },191 mocking: func(t *testing.T, ctx context.Context) *mocks.MockDynamoDBAPI {192 t.Helper()193 m := new(mocks.MockDynamoDBAPI)194 ctx = context.WithValue(ctx, newClientKey, m)195 m.On("CreateInstance").Return(m)196 m.DescribeTableAPIClient.On("DescribeTable", ctx, &dynamodb.DescribeTableInput{197 TableName: aws.String("TEST"),198 }).Return(nil, fmt.Errorf("cannot describe table"))199 return m200 },201 wantErr: true,202 },203 }204 for _, tt := range tests {205 t.Run(tt.name, func(t *testing.T) {206 mock := tt.mocking(t, tt.args.ctx)207 i := &Instance{208 NewClient: mock,209 }...
CreateInstance
Using AI Code Generation
1import (2func TestCreateInstance(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := NewMockMyInterface(ctrl)6 mock.EXPECT().CreateInstance().Return("Hello World")7 fmt.Println(mock.CreateInstance())8}9import (10func TestCreateInstance(t *testing.T) {11 ctrl := gomock.NewController(t)12 defer ctrl.Finish()13 mock := NewMockMyInterface(ctrl)14 mock.EXPECT().CreateInstance().DoAndReturn( func() string {15 })16 fmt.Println(mock.CreateInstance())17}18import (19func TestCreateInstance(t *testing.T) {20 ctrl := gomock.NewController(t)21 defer ctrl.Finish()22 mock := NewMockMyInterface(ctrl)23 mock.EXPECT().CreateInstance().DoAndReturn( func() string {24 })25 fmt.Println(mock.CreateInstance())26}27import (28func TestCreateInstance(t *testing.T) {29 ctrl := gomock.NewController(t)30 defer ctrl.Finish()31 mock := NewMockMyInterface(ctrl)32 mock.EXPECT().CreateInstance().DoAndReturn( func() string {33 })34 fmt.Println(mock.CreateInstance())35}36import (37func TestCreateInstance(t *testing.T) {38 ctrl := gomock.NewController(t)39 defer ctrl.Finish()40 mock := NewMockMyInterface(ctrl)41 mock.EXPECT().CreateInstance().DoAndReturn( func() string {42 })43 fmt.Println(mock.CreateInstance())44}45import
CreateInstance
Using AI Code Generation
1func (m *mocks) CreateInstance() {2}3func (m *mocks) DeleteInstance() {4}5import (6func main() {7 http.HandleFunc("/hello", hello)8 http.ListenAndServe(":8080", nil)9}10func hello(w http.ResponseWriter, r *http.Request) {11 fmt.Fprintln(w, "Hello, world!")12}13import (14func main() {15 http.HandleFunc("/hello", hello)16 http.ListenAndServe(":8080", nil)17}18func hello(w http.ResponseWriter, r *http.Request) {19 fmt.Fprintln(w, "Hello, world!")20}
CreateInstance
Using AI Code Generation
1import (2type Mocks struct {3}4func (m *Mocks) CreateInstance() {5 fmt.Println("Create Instance")6}7func TestCreateInstance(t *testing.T) {8 m := new(Mocks)9 m.CreateInstance()10}
CreateInstance
Using AI Code Generation
1public class TestClass {2 public static void main(String[] args) {3 MockClass mockObject = EasyMock.createMock(MockClass.class);4 EasyMock.expect(mockObject.CreateInstance("Test")).andReturn("Test");5 EasyMock.replay(mockObject);6 System.out.println(mockObject.CreateInstance("Test"));7 EasyMock.verify(mockObject);8 }9}10public class TestClass {11 public static void main(String[] args) {12 MockClass mockObject = EasyMock.createMock(MockClass.class);13 EasyMock.expect(mockObject.CreateInstance("Test")).andReturn("Test");14 EasyMock.replay(mockObject);15 System.out.println(mockObject.CreateInstance("Test"));16 EasyMock.verify(mockObject);17 }18}19public class TestClass {20 public static void main(String[] args) {21 MockClass mockObject = EasyMock.createMock(MockClass.class);22 EasyMock.expect(mockObject.CreateInstance("Test")).andReturn("Test");23 EasyMock.replay(mockObject);24 System.out.println(mockObject.CreateInstance("Test"));25 EasyMock.verify(mockObject);26 }27}28public class TestClass {29 public static void main(String[] args) {30 MockClass mockObject = EasyMock.createMock(MockClass.class);31 EasyMock.expect(mockObject.CreateInstance("Test")).andReturn("Test");32 EasyMock.replay(mockObject);33 System.out.println(mockObject.CreateInstance("Test"));34 EasyMock.verify(mockObject);35 }36}37public class TestClass {38 public static void main(String[] args) {
CreateInstance
Using AI Code Generation
1import (2func main() {3 mock.CreateInstance()4}5import (6type Mocks struct {7}8func (m *Mocks) CreateInstance() {9 mock.CreateInstance()10}11type Mocks struct {12}13func (m *Mocks) CreateInstance() {14 fmt.Println("CreateInstance method of the mocks class")15}16./2.go:13: cannot use mock (type Mocks) as type mocks.Mocks in argument to mock.CreateInstance:17 Mocks does not implement mocks.Mocks (wrong type for CreateInstance method)18 have CreateInstance()19 want CreateInstance()
CreateInstance
Using AI Code Generation
1func CreateInstance() *mocks.MockInterface {2 return &mocks.MockInterface{}3}4func TestCreateInstance(t *testing.T) {5 mock := CreateInstance()6 mock.On("Function1").Return("Hello World")7 fmt.Println(mock.Function1())8}
CreateInstance
Using AI Code Generation
1import (2func main() {3 httpClient := &http.Client{}4 if len(os.Args) > 1 {5 httpClient = &mock.MockClient{}6 }7 r := real.Real{8 }9 if err != nil {10 log.Fatal(err)11 }12 fmt.Println(resp)13}14import (15func main() {16 httpClient := &http.Client{}17 if len(os.Args) > 1 {18 httpClient = mock.NewMockClient()19 }20 r := real.Real{21 }22 if err != nil {23 log.Fatal(err)24 }25 fmt.Println(resp)26}27import (28func main() {29 httpClient := &http.Client{}30 if len(os.Args) > 1 {31 httpClient = mock.NewMockClient()32 }33 r := real.Real{34 }35 if err != nil {36 log.Fatal(err)37 }38 fmt.Println(resp)39}40import (41func main() {42 httpClient := &http.Client{}43 if len(os.Args) > 1 {44 httpClient = mock.NewMockClient()45 }
CreateInstance
Using AI Code Generation
1import (2func main() {3 p := mocks.Person{}4 p.CreateInstance()5 fmt.Println(p.Name)6}
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!!