|
| 1 | +package ddb |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/aws" |
| 8 | + "github.com/aws/aws-sdk-go/aws/session" |
| 9 | + "github.com/aws/aws-sdk-go/service/dynamodb" |
| 10 | +) |
| 11 | + |
| 12 | +type fakeRetryer struct { |
| 13 | + Name string |
| 14 | +} |
| 15 | + |
| 16 | +func (r *fakeRetryer) ShouldRetry(err error) bool { |
| 17 | + r.Name = "fakeRetryer" |
| 18 | + return false |
| 19 | +} |
| 20 | + |
| 21 | +func TestNewCheckpoint(t *testing.T) { |
| 22 | + c, err := New("", "") |
| 23 | + if c == nil { |
| 24 | + t.Errorf("expected checkpoint client instance. got %v", c) |
| 25 | + } |
| 26 | + if err != nil { |
| 27 | + t.Errorf("new checkpoint error expected nil. got %v", err) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestCheckpointSetting(t *testing.T) { |
| 32 | + var ck Checkpoint |
| 33 | + ckPtr := &ck |
| 34 | + |
| 35 | + // Test WithMaxInterval |
| 36 | + setInterval := WithMaxInterval(time.Duration(2 * time.Minute)) |
| 37 | + setInterval(ckPtr) |
| 38 | + |
| 39 | + // Test WithRetryer |
| 40 | + var r fakeRetryer |
| 41 | + setRetryer := WithRetryer(&r) |
| 42 | + setRetryer(ckPtr) |
| 43 | + |
| 44 | + // Test WithDyanmoDBClient |
| 45 | + var fakeDbClient = dynamodb.New( |
| 46 | + session.New(aws.NewConfig()), &aws.Config{ |
| 47 | + Region: aws.String("us-west-2"), |
| 48 | + }, |
| 49 | + ) |
| 50 | + setDDBClient := WithDynamoClient(fakeDbClient) |
| 51 | + setDDBClient(ckPtr) |
| 52 | + |
| 53 | + if ckPtr.maxInterval != time.Duration(2*time.Minute) { |
| 54 | + t.Errorf("new checkpoint maxInterval expected 2 minute. got %v", ckPtr.maxInterval) |
| 55 | + } |
| 56 | + if ckPtr.retryer.ShouldRetry(nil) != false { |
| 57 | + t.Errorf("new checkpoint retryer ShouldRetry always returns %v . got %v", false, ckPtr.retryer.ShouldRetry(nil)) |
| 58 | + } |
| 59 | + if ckPtr.client != fakeDbClient { |
| 60 | + t.Errorf("new checkpoint dynamodb client reference should be %p. got %v", &fakeDbClient, ckPtr.client) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestNewCheckpointWithOptions(t *testing.T) { |
| 65 | + // Test WithMaxInterval |
| 66 | + setInterval := WithMaxInterval(time.Duration(2 * time.Minute)) |
| 67 | + |
| 68 | + // Test WithRetryer |
| 69 | + var r fakeRetryer |
| 70 | + setRetryer := WithRetryer(&r) |
| 71 | + |
| 72 | + // Test WithDyanmoDBClient |
| 73 | + var fakeDbClient = dynamodb.New( |
| 74 | + session.New(aws.NewConfig()), &aws.Config{ |
| 75 | + Region: aws.String("us-west-2"), |
| 76 | + }, |
| 77 | + ) |
| 78 | + setDDBClient := WithDynamoClient(fakeDbClient) |
| 79 | + |
| 80 | + ckPtr, err := New("testapp", "testtable", setInterval, setRetryer, setDDBClient) |
| 81 | + if ckPtr == nil { |
| 82 | + t.Errorf("expected checkpoint client instance. got %v", ckPtr) |
| 83 | + } |
| 84 | + if err != nil { |
| 85 | + t.Errorf("new checkpoint error expected nil. got %v", err) |
| 86 | + } |
| 87 | + if ckPtr.appName != "testapp" { |
| 88 | + t.Errorf("new checkpoint app name expected %v. got %v", "testapp", ckPtr.appName) |
| 89 | + } |
| 90 | + if ckPtr.tableName != "testtable" { |
| 91 | + t.Errorf("new checkpoint table expected %v. got %v", "testtable", ckPtr.maxInterval) |
| 92 | + } |
| 93 | + |
| 94 | + if ckPtr.maxInterval != time.Duration(2*time.Minute) { |
| 95 | + t.Errorf("new checkpoint maxInterval expected 2 minute. got %v", ckPtr.maxInterval) |
| 96 | + } |
| 97 | + if ckPtr.retryer.ShouldRetry(nil) != false { |
| 98 | + t.Errorf("new checkpoint retryer ShouldRetry always returns %v . got %v", false, ckPtr.retryer.ShouldRetry(nil)) |
| 99 | + } |
| 100 | + if ckPtr.client != fakeDbClient { |
| 101 | + t.Errorf("new checkpoint dynamodb client reference should be %p. got %v", &fakeDbClient, ckPtr.client) |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments