Skip to content

Commit

Permalink
add test for cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
fogfish committed Oct 3, 2021
1 parent 6f7ddad commit 332b7dd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ type Person struct {

//
// Identity implements thing interface
func (p Person) Identity() (string, string) {
return p.Org, p.ID
}
func (p Person) Identity() (string, string) { return p.Org, p.ID }

//
// this data type is a normal Golang struct
Expand Down
1 change: 1 addition & 0 deletions ddb.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func (seq *dbSeq) Tail() bool {

// Cursor is the global position in the sequence
func (seq *dbSeq) Cursor() (string, string) {
// Note: q.ExclusiveStartKey is set by sequence seeding
if seq.q.ExclusiveStartKey != nil {
var hkey, skey string
val := seq.q.ExclusiveStartKey
Expand Down
63 changes: 48 additions & 15 deletions ddb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestDdbMatchNone(t *testing.T) {
map[string]*dynamodb.AttributeValue{
":prefix": {S: aws.String("dead:beef")},
},
0,
0, nil,
)

seq := ddb.Match(person{Prefix: dynamo.NewIRI("dead:beef")})
Expand All @@ -145,7 +145,7 @@ func TestDdbMatchOne(t *testing.T) {
map[string]*dynamodb.AttributeValue{
":prefix": {S: aws.String("dead:beef")},
},
1,
1, nil,
)

seq := ddb.Match(person{Prefix: dynamo.NewIRI("dead:beef")})
Expand All @@ -165,7 +165,7 @@ func TestDdbMatchMany(t *testing.T) {
map[string]*dynamodb.AttributeValue{
":prefix": {S: aws.String("dead:beef")},
},
5,
5, nil,
)

cnt := 0
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestDdbFMapNone(t *testing.T) {
map[string]*dynamodb.AttributeValue{
":prefix": {S: aws.String("dead:beef")},
},
0,
0, nil,
)

err := ddb.Match(person{Prefix: dynamo.NewIRI("dead:beef")}).FMap(seq.Join)
Expand All @@ -221,7 +221,7 @@ func TestDdbFMapPrefixOnly(t *testing.T) {
map[string]*dynamodb.AttributeValue{
":prefix": {S: aws.String("dead:beef")},
},
2,
2, nil,
)
thing := entity()

Expand All @@ -232,14 +232,13 @@ func TestDdbFMapPrefixOnly(t *testing.T) {
}

func TestDdbFMapPrefixAndSuffix(t *testing.T) {

seq := persons{}
ddb := mockQuery(
map[string]*dynamodb.AttributeValue{
":prefix": {S: aws.String("dead:beef")},
":suffix": {S: aws.String("a/b/c")},
},
2,
2, nil,
)
thing := entity()

Expand All @@ -259,7 +258,7 @@ func TestDdbFMapIDs(t *testing.T) {
map[string]*dynamodb.AttributeValue{
":prefix": {S: aws.String("dead:beef")},
},
2,
2, nil,
)
prefix, suffix := entity().Identity()
thing := []string{prefix, suffix}
Expand All @@ -270,6 +269,34 @@ func TestDdbFMapIDs(t *testing.T) {
If(seq).Should().Equal(dynamo.Identities{thing, thing})
}

func TestDdbCursorAndContinue(t *testing.T) {
// seq := persons{}
ddb := mockQuery(
map[string]*dynamodb.AttributeValue{
":prefix": {S: aws.String("dead:beef")},
},
2,
map[string]*dynamodb.AttributeValue{
"prefix": {S: aws.String("dead:beef")},
"suffix": {S: aws.String("1")},
},
)

dbseq := ddb.Match(person{Prefix: dynamo.NewIRI("dead:beef")})
dbseq.Tail()
hkey0, skey0 := dbseq.Cursor()

dbseq = ddb.Match(person{Prefix: dynamo.NewIRI("dead:beef")}).Continue(hkey0, skey0)
dbseq.Tail()
hkey1, skey1 := dbseq.Cursor()

it.Ok(t).
If(hkey0).Equal("dead:beef").
If(skey0).Equal("1").
If(hkey1).Equal("dead:beef").
If(skey1).Equal("1")
}

//-----------------------------------------------------------------------------
//
// Mock Dynamo DB
Expand Down Expand Up @@ -373,12 +400,17 @@ func (mock *ddbUpdateItem) UpdateItemWithContext(ctx aws.Context, input *dynamod
//
type ddbQuery struct {
dynamodbiface.DynamoDBAPI
expectKey map[string]*dynamodb.AttributeValue
returnLen int
expectKey map[string]*dynamodb.AttributeValue
returnLen int
returnLastKey map[string]*dynamodb.AttributeValue
}

func mockQuery(expectKey map[string]*dynamodb.AttributeValue, returnLen int) dynamo.KeyValNoContext {
return mockDynamoDB(&ddbQuery{expectKey: expectKey, returnLen: returnLen})
func mockQuery(
expectKey map[string]*dynamodb.AttributeValue,
returnLen int,
returnLastKey map[string]*dynamodb.AttributeValue,
) dynamo.KeyValNoContext {
return mockDynamoDB(&ddbQuery{expectKey: expectKey, returnLen: returnLen, returnLastKey: returnLastKey})
}

func (mock *ddbQuery) QueryWithContext(ctx aws.Context, input *dynamodb.QueryInput, opts ...request.Option) (*dynamodb.QueryOutput, error) {
Expand All @@ -398,9 +430,10 @@ func (mock *ddbQuery) QueryWithContext(ctx aws.Context, input *dynamodb.QueryInp
}

return &dynamodb.QueryOutput{
ScannedCount: aws.Int64(int64(mock.returnLen)),
Count: aws.Int64(int64(mock.returnLen)),
Items: seq,
ScannedCount: aws.Int64(int64(mock.returnLen)),
Count: aws.Int64(int64(mock.returnLen)),
Items: seq,
LastEvaluatedKey: mock.returnLastKey,
}, nil
}

Expand Down

0 comments on commit 332b7dd

Please sign in to comment.