Skip to content

Test WalletStore queries #1108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.8
toolchain go1.24.1

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/IBM/idemix v0.0.2-0.20240816143710-3dce4618d760
github.com/IBM/idemix/bccsp/types v0.0.0-20240816143710-3dce4618d760
github.com/IBM/mathlib v0.0.3-0.20241219051532-81539b287cf5
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,7 @@ github.com/kilic/bls12-381 v0.1.0/go.mod h1:vDTTHJONJ6G+P2R74EhnyotQDTliQDnFEwhd
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
github.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
Expand Down
126 changes: 126 additions & 0 deletions token/services/db/sql/common/wallet_test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package common

import (
"context"
"database/sql"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/driver"
"github.com/onsi/gomega"
)

type walletStoreConstructor func(*sql.DB) *WalletStore

func TestGetWalletID(t *testing.T, store walletStoreConstructor) {
gomega.RegisterTestingT(t)
db, mockDB, err := sqlmock.New()
gomega.Expect(err).ToNot(gomega.HaveOccurred())

tokenID := token.Identity([]byte("1234"))
roleID := 5
output := driver.WalletID("my wallet")
mockDB.
ExpectQuery("SELECT wallet_id FROM WALLETS WHERE \\(identity_hash = \\$1\\) AND \\(role_id = \\$2\\)").
WithArgs(tokenID.UniqueID(), roleID).
WillReturnRows(mockDB.NewRows([]string{"request"}).AddRow(output))

actualWalletID, err := store(db).GetWalletID(context.Background(), tokenID, roleID)

gomega.Expect(mockDB.ExpectationsWereMet()).To(gomega.Succeed())
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(actualWalletID).To(gomega.Equal(output))
}

func TestGetWalletIDs(t *testing.T, store walletStoreConstructor) {
gomega.RegisterTestingT(t)
db, mockDB, err := sqlmock.New()
gomega.Expect(err).ToNot(gomega.HaveOccurred())

roleID := 5
output := driver.WalletID("my wallet")
mockDB.
ExpectQuery("SELECT DISTINCT wallet_id FROM WALLETS WHERE role_id = \\$1").
WithArgs(roleID).
WillReturnRows(mockDB.NewRows([]string{"wallet_id"}).AddRow(output))

actualWalletIDs, err := store(db).GetWalletIDs(context.Background(), roleID)

gomega.Expect(mockDB.ExpectationsWereMet()).To(gomega.Succeed())
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(actualWalletIDs).To(gomega.ConsistOf(output))
}

func TestLoadMeta(t *testing.T, store walletStoreConstructor) {
gomega.RegisterTestingT(t)
db, mockDB, err := sqlmock.New()
gomega.Expect(err).ToNot(gomega.HaveOccurred())

tokenID := token.Identity([]byte("1234"))
roleID := 5
walletID := driver.WalletID("my wallet")
output := []byte("some meta data")
mockDB.
ExpectQuery("SELECT meta FROM WALLETS WHERE \\(identity_hash = \\$1\\) AND \\(wallet_id = \\$2\\) AND \\(role_id = \\$3\\)").
WithArgs(tokenID.UniqueID(), walletID, roleID).
WillReturnRows(mockDB.NewRows([]string{"meta"}).AddRow(output))

actual, err := store(db).LoadMeta(context.Background(), tokenID, walletID, roleID)

gomega.Expect(mockDB.ExpectationsWereMet()).To(gomega.Succeed())
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(actual).To(gomega.Equal(output))
}

func TestIdentityExists(t *testing.T, store walletStoreConstructor) {
gomega.RegisterTestingT(t)
db, mockDB, err := sqlmock.New()
gomega.Expect(err).ToNot(gomega.HaveOccurred())

tokenID := token.Identity([]byte("1234"))
roleID := 5
walletID := driver.WalletID("my wallet")
mockDB.
ExpectQuery("SELECT wallet_id FROM WALLETS WHERE \\(identity_hash = \\$1\\) AND \\(wallet_id = \\$2\\) AND \\(role_id = \\$3\\)").
WithArgs(tokenID.UniqueID(), walletID, roleID).
WillReturnRows(mockDB.NewRows([]string{"wallet_id"}).AddRow(walletID))

exists := store(db).IdentityExists(context.Background(), tokenID, walletID, roleID)

gomega.Expect(mockDB.ExpectationsWereMet()).To(gomega.Succeed())
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(exists).To(gomega.BeTrue())
}

func TestStoreIdentity(t *testing.T, store walletStoreConstructor) {
gomega.RegisterTestingT(t)
db, mockDB, err := sqlmock.New()
gomega.Expect(err).ToNot(gomega.HaveOccurred())

tokenID := token.Identity([]byte("1234"))
eID := "5678"
walletID := driver.WalletID("my wallet")
roleID := 5

mockDB.
ExpectQuery("SELECT wallet_id FROM WALLETS WHERE \\(identity_hash = \\$1\\) AND \\(wallet_id = \\$2\\) AND \\(role_id = \\$3\\)").
WithArgs(tokenID.UniqueID(), walletID, roleID).
WillReturnRows(mockDB.NewRows([]string{"wallet_id"}))

mockDB.ExpectExec("INSERT INTO WALLETS "+
"\\(identity_hash, meta, wallet_id, role_id, created_at, enrollment_id\\) "+
"VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5, \\$6\\)").
WithArgs(tokenID.UniqueID(), []uint8(nil), walletID, roleID, sqlmock.AnyArg(), eID).
WillReturnResult(sqlmock.NewResult(1, 1))

err = store(db).StoreIdentity(context.Background(), tokenID, eID, walletID, roleID, nil)

gomega.Expect(mockDB.ExpectationsWereMet()).To(gomega.Succeed())
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}
41 changes: 41 additions & 0 deletions token/services/db/sql/postgres/wallet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package postgres

import (
"database/sql"
"testing"

"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/postgres"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/sql/common"
)

func mockWalletStore(db *sql.DB) *WalletStore {
store, _ := common.NewWalletStore(db, db, common.TableNames{
Wallets: "WALLETS",
}, postgres.NewConditionInterpreter())
return store
}

func TestGetWalletID(t *testing.T) {
common.TestGetWalletID(t, mockWalletStore)
}

func TestGetWalletIDs(t *testing.T) {
common.TestGetWalletIDs(t, mockWalletStore)
}

func TestLoadMeta(t *testing.T) {
common.TestLoadMeta(t, mockWalletStore)
}

func TestIdentityExists(t *testing.T) {
common.TestIdentityExists(t, mockWalletStore)
}

func TestStoreIdentity(t *testing.T) {
common.TestStoreIdentity(t, mockWalletStore)
}
41 changes: 41 additions & 0 deletions token/services/db/sql/sqlite/wallet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package sqlite

import (
"database/sql"
"testing"

"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/sqlite"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/sql/common"
)

func mockWalletStore(db *sql.DB) *WalletStore {
store, _ := common.NewWalletStore(db, db, common.TableNames{
Wallets: "WALLETS",
}, sqlite.NewConditionInterpreter())
return store
}

func TestGetWalletID(t *testing.T) {
common.TestGetWalletID(t, mockWalletStore)
}

func TestGetWalletIDs(t *testing.T) {
common.TestGetWalletIDs(t, mockWalletStore)
}

func TestLoadMeta(t *testing.T) {
common.TestLoadMeta(t, mockWalletStore)
}

func TestIdentityExists(t *testing.T) {
common.TestIdentityExists(t, mockWalletStore)
}

func TestStoreIdentity(t *testing.T) {
common.TestStoreIdentity(t, mockWalletStore)
}
Loading