Skip to content

Commit

Permalink
adding a unit test for mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
husobee committed Feb 5, 2024
1 parent e53a580 commit a4f1c94
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
10 changes: 9 additions & 1 deletion services/skus/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,14 @@ type AreTimeLimitedV2CredsSubmittedResult struct {
}

func (pg *Postgres) AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestID uuid.UUID, blindedCreds ...string) (*AreTimeLimitedV2CredsSubmittedResult, error) {
return areTimeLimitedV2CredsSubmitted(ctx, pg.RawDB(), requestID, blindedCreds...)
}

type getContext interface {
GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

func areTimeLimitedV2CredsSubmitted(ctx context.Context, dbi getContext, requestID uuid.UUID, blindedCreds ...string) (*AreTimeLimitedV2CredsSubmittedResult, error) {
if len(blindedCreds) < 1 {
return nil, errors.New("invalid parameter to tlv2 creds signed")
}
Expand All @@ -970,7 +978,7 @@ func (pg *Postgres) AreTimeLimitedV2CredsSubmitted(ctx context.Context, requestI
select 1 from time_limited_v2_order_creds where blinded_creds->>0 = $1 and request_id != $2
) as mismatch
`
err := pg.RawDB().Get(&result, query, blindedCreds[0], requestID)
err := dbi.GetContext(ctx, &result, query, blindedCreds[0], requestID)
if err != nil {
return nil, err
}
Expand Down
71 changes: 71 additions & 0 deletions services/skus/datastore_noint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package skus

import (
"context"
"testing"

"github.com/golang/mock/gomock"
uuid "github.com/satori/go.uuid"
should "github.com/stretchr/testify/assert"
)

type mockGetContext struct {
getContext func(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

func (mgc *mockGetContext) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
if mgc.getContext != nil {
return mgc.getContext(ctx, dest, query, args)
}
return nil
}

func TestAreTimeLimitedV2CredsSubmitted(t *testing.T) {
type tcExpected struct {
result map[string]bool
ok bool

Check failure on line 26 in services/skus/datastore_noint_test.go

View workflow job for this annotation

GitHub Actions / lint

field `ok` is unused (unused)
noErr bool
}

type testCase struct {
name string
dbi getContext
given uuid.UUID
exp tcExpected
}

ctrl := gomock.NewController(t)
defer ctrl.Finish()

tests := []testCase{
{
name: "mismatch",
dbi: &mockGetContext{
getContext: func(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
*dest.(*AreTimeLimitedV2CredsSubmittedResult) = AreTimeLimitedV2CredsSubmittedResult{
AlreadySubmitted: true,
Mismatch: true,
}
return nil
},
},
given: uuid.Must(uuid.FromString("8f51f9ca-b593-4200-9bfb-91ac34748e09")),
exp: tcExpected{
noErr: true,
result: map[string]bool{
"mismatch": true,
},
},
},
}

for i := range tests {
tc := tests[i]

t.Run(tc.name, func(t *testing.T) {
result, err := areTimeLimitedV2CredsSubmitted(context.TODO(), tc.dbi, tc.given, "")
should.Equal(t, tc.exp.result["mismatch"], result.Mismatch)
should.Equal(t, tc.exp.noErr, err == nil)
})
}
}

0 comments on commit a4f1c94

Please sign in to comment.