Skip to content

Commit

Permalink
Pass a dynamo.SK to PartialSK method
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Nov 27, 2024
1 parent 4ccf6dc commit d7563bd
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/event-received/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type uidEvent struct {

type dynamodbClient interface {
AnyByPK(ctx context.Context, pk dynamo.PK, v interface{}) error
AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK string, v interface{}) error
AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK dynamo.SK, v interface{}) error
Create(ctx context.Context, v interface{}) error
CreateOnly(ctx context.Context, v interface{}) error
DeleteOne(ctx context.Context, pk dynamo.PK, sk dynamo.SK) error
Expand Down
12 changes: 6 additions & 6 deletions cmd/event-received/mock_dynamodbClient_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type DynamoClient interface {
AllByKeys(ctx context.Context, keys []dynamo.Keys) ([]map[string]dynamodbtypes.AttributeValue, error)
AllByPartialSK(ctx context.Context, pk dynamo.PK, partialSK dynamo.SK, v interface{}) error
AllBySK(ctx context.Context, sk dynamo.SK, v interface{}) error
AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK string, v interface{}) error
AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK dynamo.SK, v interface{}) error
AllKeysByPK(ctx context.Context, pk dynamo.PK) ([]dynamo.Keys, error)
AnyByPK(ctx context.Context, pk dynamo.PK, v interface{}) error
BatchPut(ctx context.Context, items []interface{}) error
Expand Down
12 changes: 6 additions & 6 deletions internal/app/mock_DynamoClient_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/dynamo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *Client) OneByUID(ctx context.Context, uid string, v interface{}) error
return attributevalue.UnmarshalMap(response.Items[0], v)
}

func (c *Client) AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK string, v interface{}) error {
func (c *Client) AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK SK, v interface{}) error {
response, err := c.svc.Query(ctx, &dynamodb.QueryInput{
TableName: aws.String(c.table),
IndexName: aws.String(lpaUIDIndex),
Expand All @@ -96,7 +96,7 @@ func (c *Client) AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK str
},
ExpressionAttributeValues: map[string]types.AttributeValue{
":LpaUID": &types.AttributeValueMemberS{Value: uid},
":SK": &types.AttributeValueMemberS{Value: partialSK},
":SK": &types.AttributeValueMemberS{Value: partialSK.SK()},
},
KeyConditionExpression: aws.String("#LpaUID = :LpaUID"),
FilterExpression: aws.String("begins_with(#SK, :SK)"),
Expand Down
10 changes: 5 additions & 5 deletions internal/dynamo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ func TestAllScheduledEventsByUID(t *testing.T) {
},
ExpressionAttributeValues: map[string]types.AttributeValue{
":LpaUID": &types.AttributeValueMemberS{Value: "lpa-uid"},
":SK": &types.AttributeValueMemberS{Value: "partial-sk"},
":SK": &types.AttributeValueMemberS{Value: "a-partial-sk"},
},
KeyConditionExpression: aws.String("#LpaUID = :LpaUID"),
FilterExpression: aws.String("begins_with(#SK, :SK)"),
Expand All @@ -984,7 +984,7 @@ func TestAllScheduledEventsByUID(t *testing.T) {
c := &Client{table: "this", svc: dynamoDB}

var v []map[string]string
err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", "partial-sk", &v)
err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", testSK("a-partial-sk"), &v)
assert.Nil(t, err)
}

Expand All @@ -997,7 +997,7 @@ func TestAllScheduledEventsByUIDWhenQueryError(t *testing.T) {
c := &Client{table: "this", svc: dynamoDB}

var v []map[string]string
err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", "partial-sk", &v)
err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", testSK("a-partial-sk"), &v)
assert.Equal(t, fmt.Errorf("failed to query scheduled event by UID: %w", expectedError), err)
}

Expand All @@ -1010,7 +1010,7 @@ func TestAllScheduledEventsByUIDWhenNoResults(t *testing.T) {
c := &Client{table: "this", svc: dynamoDB}

var v []map[string]string
err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", "partial-sk", &v)
err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", testSK("a-partial-sk"), &v)
assert.Equal(t, NotFoundError{}, err)
}

Expand All @@ -1026,6 +1026,6 @@ func TestAllScheduledEventsByUIDWhenUnmarshalError(t *testing.T) {
c := &Client{table: "this", svc: dynamoDB}

var v []map[string]string
err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", "partial-sk", v)
err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", testSK("a-partial-sk"), v)
assert.Error(t, err)
}
4 changes: 2 additions & 2 deletions internal/dynamo/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ func ScheduledKey(at time.Time, action int) ScheduledKeyType {
return ScheduledKeyType(scheduledPrefix + "#" + at.Format(time.RFC3339) + "#" + strconv.Itoa(action))
}

func PartialScheduleKey() string {
return scheduledPrefix + "#"
func PartialScheduledKey() ScheduledKeyType {
return ScheduledKeyType(scheduledPrefix + "#")
}

type ReservedKeyType string
Expand Down
1 change: 1 addition & 0 deletions internal/dynamo/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestSK(t *testing.T) {
"VoucherKey": {VoucherKey("S"), "VOUCHER#S"},
"ScheduledKey": {ScheduledKey(time.Date(2024, time.January, 2, 12, 13, 14, 15, time.UTC), 99), "SCHEDULED#2024-01-02T12:13:14Z#99"},
"ReservedKey": {ReservedKey(VoucherKey), "RESERVED#VOUCHER#"},
"PartialScheduledKey": {PartialScheduledKey(), "SCHEDULED#"},
}

for name, tc := range testcases {
Expand Down
12 changes: 6 additions & 6 deletions internal/scheduled/mock_DynamoClient_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/scheduled/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) SetData(data any) {
c.Run(func(_ context.Context, _, _ string, v any) {
c.Run(func(_ context.Context, _ string, _ dynamo.SK, v any) {
b, _ := attributevalue.Marshal(data)
attributevalue.Unmarshal(b, v)
})
Expand Down
4 changes: 2 additions & 2 deletions internal/scheduled/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type DynamoClient interface {
AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK string, v interface{}) error
AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK dynamo.SK, v interface{}) error
AnyByPK(ctx context.Context, pk dynamo.PK, v interface{}) error
Move(ctx context.Context, oldKeys dynamo.Keys, value any) error
DeleteKeys(ctx context.Context, keys []dynamo.Keys) error
Expand Down Expand Up @@ -55,7 +55,7 @@ func (s *Store) Create(ctx context.Context, row Event) error {
func (s *Store) DeleteAllByUID(ctx context.Context, uid string) error {
var events []Event

if err := s.dynamoClient.AllByLpaUIDAndPartialSK(ctx, uid, dynamo.PartialScheduleKey(), &events); err != nil {
if err := s.dynamoClient.AllByLpaUIDAndPartialSK(ctx, uid, dynamo.PartialScheduledKey(), &events); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/scheduled/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestDeleteAllByUID(t *testing.T) {

dynamoClient := newMockDynamoClient(t)
dynamoClient.EXPECT().
AllByLpaUIDAndPartialSK(ctx, "lpa-uid", dynamo.PartialScheduleKey(), mock.Anything).
AllByLpaUIDAndPartialSK(ctx, "lpa-uid", dynamo.PartialScheduledKey(), mock.Anything).
Return(nil).
SetData([]Event{
{LpaUID: "lpa-uid", PK: dynamo.ScheduledDayKey(now), SK: dynamo.ScheduledKey(now, 98)},
Expand Down

0 comments on commit d7563bd

Please sign in to comment.