Skip to content

Commit

Permalink
test: add test for Redrive func of SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatanabe committed Oct 26, 2023
1 parent b6f6da3 commit b98ecf5
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 3 deletions.
7 changes: 5 additions & 2 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,10 @@ func (c *queueSDKClient[T]) Redrive(ctx context.Context, id string) (*Result, er
return nil, &IDNotFoundError{}
}
if retrieved.QueueType == QueueTypeStandard {
return nil, &RecordNotConstructedError{} // FIXME
return nil, &RecordNotConstructedError{} // FIXME Define more appropriately named errors.
}
if retrieved.Status == StatusProcessing {
return nil, &IllegalStateError{} // FIXME
return nil, &IllegalStateError{} // FIXME Define more appropriately named errors.
}
retrieved.MarkAsRedrive(c.clock.Now())
expr, err := expression.NewBuilder().
Expand All @@ -414,6 +414,9 @@ func (c *queueSDKClient[T]) Redrive(ctx context.Context, id string) (*Result, er
).Set(
expression.Name("queue_type"),
expression.Value(retrieved.QueueType),
).Set(
expression.Name("status"),
expression.Value(retrieved.Status),
).Set(
expression.Name("last_updated_timestamp"),
expression.Value(retrieved.LastUpdatedTimestamp),
Expand Down
126 changes: 125 additions & 1 deletion sdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,131 @@ func TestQueueSDKClientSendToDLQ(t *testing.T) {
}

func TestQueueSDKClientRedrive(t *testing.T) {
// FIXME
type args struct {
id string
}
tests := []struct {
name string
setup func(*testing.T) (*dynamodb.Client, func())
sdkClock clock.Clock
args args
want *Result
wantErr error
}{
{
name: "should return IDNotProvidedError",
setup: func(t *testing.T) (*dynamodb.Client, func()) {
return setupDynamoDB(t,
&types.PutRequest{
Item: newTestMessageItemAsReady("A-101", clock.Now()).MarshalMapUnsafe(),
},
)
},
args: args{
id: "",
},
want: nil,
wantErr: &IDNotProvidedError{},
},
{
name: "should return IDNotFoundError",
setup: func(t *testing.T) (*dynamodb.Client, func()) {
return setupDynamoDB(t,
&types.PutRequest{
Item: newTestMessageItemAsReady("A-101", clock.Now()).MarshalMapUnsafe(),
},
)
},
args: args{
id: "B-202",
},
want: nil,
wantErr: &IDNotFoundError{},
},
{
name: "should return RecordNotConstructedError",
setup: func(t *testing.T) (*dynamodb.Client, func()) {
return setupDynamoDB(t,
&types.PutRequest{
Item: newTestMessageItemAsReady("A-101", clock.Now()).MarshalMapUnsafe(),
},
)
},
args: args{
id: "A-101",
},
want: nil,
wantErr: &RecordNotConstructedError{},
},
{
name: "should return IllegalStateError",
setup: func(t *testing.T) (*dynamodb.Client, func()) {
return setupDynamoDB(t, func() *types.PutRequest {
msg := newTestMessageItemAsDLQ("A-101", clock.Now())
msg.MarkAsPeeked(clock.Now())
return &types.PutRequest{
Item: msg.MarshalMapUnsafe(),
}
}())
},
args: args{
id: "A-101",
},
want: nil,
wantErr: &IllegalStateError{},
},

{
name: "should redrive succeeds",
setup: func(t *testing.T) (*dynamodb.Client, func()) {
return setupDynamoDB(t,
&types.PutRequest{
Item: newTestMessageItemAsDLQ("A-101", time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)).MarshalMapUnsafe(),
},
)
},
sdkClock: mockClock{
t: time.Date(2023, 12, 1, 0, 0, 10, 0, time.UTC),
},
args: args{
id: "A-101",
},
want: &Result{
ID: "A-101",
Status: StatusReady,
LastUpdatedTimestamp: clock.FormatRFC3339(time.Date(2023, 12, 1, 0, 0, 10, 0, time.UTC)),
Version: 2,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
raw, clean := tt.setup(t)
defer clean()
ctx := context.Background()
client, err := NewQueueSDKClient[test.MessageData](ctx, WithAWSDynamoDBClient(raw), withClock(tt.sdkClock), WithAWSVisibilityTimeout(1))
if err != nil {
t.Fatalf("NewQueueSDKClient() error = %v", err)
return
}
result, err := client.Redrive(ctx, tt.args.id)
if tt.wantErr != nil {
if err != tt.wantErr {
t.Errorf("Redrive() error = %v, wantErr %v", err, tt.wantErr)
return
}
return
}
if err != nil {
t.Errorf("Redrive() error = %v", err)
return
}
if !reflect.DeepEqual(result, tt.want) {
t.Errorf("Redrive() got = %v, want %v", result, tt.want)
}
})
}
}

func TestQueueSDKClientGetQueueStats(t *testing.T) {
Expand Down

0 comments on commit b98ecf5

Please sign in to comment.