Skip to content

Commit

Permalink
Elide processScanOutput into getSubscribersInState
Browse files Browse the repository at this point in the history
Now that the StartKey abstraction is no longer part of the public API,
the separate processScanOutput method no longer provided much value.
  • Loading branch information
mbland committed May 8, 2023
1 parent da2739c commit af0f7a4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 85 deletions.
8 changes: 1 addition & 7 deletions db/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,9 @@ func (db *DynamoDb) getSubscribersInState(

if output, err = db.Client.Scan(ctx, input); err != nil {
err = fmt.Errorf("failed to get %s subscribers: %s", state, err)
} else {
subs, nextStartKey, err = processScanOutput(output)
return
}
return
}

func processScanOutput(
output *dynamodb.ScanOutput,
) (subs []*Subscriber, nextStartKey dbAttributes, err error) {
nextStartKey = output.LastEvaluatedKey
subs = make([]*Subscriber, len(output.Items))
errs := make([]error, len(subs))
Expand Down
80 changes: 2 additions & 78 deletions db/dynamodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,82 +273,6 @@ func TestParseSubscriber(t *testing.T) {
})
}

const testStartKeyValue = "foo@bar.com"

var testStartKey dbAttributes = dbAttributes{
"primary": &dbString{Value: testStartKeyValue},
}

func TestProcessScanOutput(t *testing.T) {
setup := func() *dynamodb.ScanOutput {
return &dynamodb.ScanOutput{
Items: []dbAttributes{
newSubscriberRecord(testVerifiedSubscribers[0]),
newSubscriberRecord(testVerifiedSubscribers[1]),
newSubscriberRecord(testVerifiedSubscribers[2]),
},
}
}

checkDbStartKeyContains := func(
t *testing.T, startKey dbAttributes, key, value string,
) {
t.Helper()

assert.Assert(t, is.Contains(startKey, key))
actualKey := startKey[key].(*dbString)
assert.Equal(t, value, actualKey.Value)
}

t.Run("Succeeds", func(t *testing.T) {
expectedSubs := []*Subscriber{
testVerifiedSubscribers[0],
testVerifiedSubscribers[1],
testVerifiedSubscribers[2],
}

t.Run("WithoutNextStartKey", func(t *testing.T) {
output := setup()

subs, nextStartKey, err := processScanOutput(output)

assert.NilError(t, err)
assert.Assert(t, is.Nil(nextStartKey))
assert.DeepEqual(t, expectedSubs, subs)
})

t.Run("WithNextStartKey", func(t *testing.T) {
output := setup()
output.LastEvaluatedKey = testStartKey

subs, nextKey, err := processScanOutput(output)

assert.NilError(t, err)
checkDbStartKeyContains(t, nextKey, "primary", testStartKeyValue)
assert.DeepEqual(t, expectedSubs, subs)
})
})

t.Run("ReturnsParseSubscriberErrors", func(t *testing.T) {
output := setup()
const statusKey string = string(SubscriberPending)
for _, record := range output.Items {
record[statusKey] = toDynamoDbTimestamp(testTimestamp)
}

subs, _, err := processScanOutput(output)

assert.DeepEqual(t, []*Subscriber{nil, nil, nil}, subs)
expectedErr := fmt.Sprintf(
"failed to parse subscriber: "+
"contains both '%s' and '%s' attributes",
SubscriberPending,
SubscriberVerified,
)
assert.ErrorContains(t, err, expectedErr)
})
}

func setupDbWithSubscribers() (dyndb *DynamoDb, client *TestDynamoDbClient) {
client = &TestDynamoDbClient{}
dyndb = &DynamoDb{client, "subscribers-table"}
Expand All @@ -373,7 +297,7 @@ func TestGetSubscribersInState(t *testing.T) {
assert.DeepEqual(t, testVerifiedSubscribers, subs)
})

t.Run("FailsIfScanFails", func(t *testing.T) {
t.Run("ReturnsErrorIfScanFails", func(t *testing.T) {
dyndb, client := setupDbWithSubscribers()
client.scanErr = errors.New("scanning error")

Expand All @@ -387,7 +311,7 @@ func TestGetSubscribersInState(t *testing.T) {
assert.ErrorContains(t, err, expectedErr)
})

t.Run("FailsIfProcessScanOutputFails", func(t *testing.T) {
t.Run("ReturnsErrorIfParsingSubscribersFails", func(t *testing.T) {
dyndb, client := setupDbWithSubscribers()
status := SubscriberVerified
client.addSubscriberRecord(dbAttributes{
Expand Down

0 comments on commit af0f7a4

Please sign in to comment.