@@ -7,17 +7,19 @@ package common
77
88import (
99 "database/sql"
10+ "regexp"
1011 "testing"
1112
1213 "github.com/DATA-DOG/go-sqlmock"
14+ sq "github.com/Masterminds/squirrel"
1315 "github.com/hyperledger-labs/fabric-token-sdk/token"
1416 "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/driver"
1517 "github.com/onsi/gomega"
1618)
1719
18- type walletStoreConstructor func (* sql.DB ) * WalletStore
20+ type walletStoreConstructor func (* sql.DB , sq. PlaceholderFormat ) * WalletStore
1921
20- func TestGetWalletID (t * testing.T , store walletStoreConstructor ) {
22+ func TestGetWalletID (t * testing.T , store walletStoreConstructor , pf sq. PlaceholderFormat ) {
2123 gomega .RegisterTestingT (t )
2224 db , mockDB , err := sqlmock .New ()
2325 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
@@ -26,37 +28,37 @@ func TestGetWalletID(t *testing.T, store walletStoreConstructor) {
2628 roleID := 5
2729 output := driver .WalletID ("my wallet" )
2830 mockDB .
29- ExpectQuery ("SELECT wallet_id FROM WALLETS WHERE \\ (identity_hash = \\ $1 \\ ) AND \\ ( role_id = \\ $2 \\ )" ).
31+ ExpectQuery (sqlPattern ( pf , "SELECT wallet_id FROM WALLETS WHERE (identity_hash = ? AND role_id = ?)" ) ).
3032 WithArgs (tokenID .UniqueID (), roleID ).
31- WillReturnRows (mockDB .NewRows ([]string {"request " }).AddRow (output ))
33+ WillReturnRows (mockDB .NewRows ([]string {"wallet_id " }).AddRow (output ))
3234
33- actualWalletID , err := store (db ).GetWalletID (t .Context (), tokenID , roleID )
35+ actualWalletID , err := store (db , pf ).GetWalletID (t .Context (), tokenID , roleID )
3436
3537 gomega .Expect (mockDB .ExpectationsWereMet ()).To (gomega .Succeed ())
3638 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
3739 gomega .Expect (actualWalletID ).To (gomega .Equal (output ))
3840}
3941
40- func TestGetWalletIDs (t * testing.T , store walletStoreConstructor ) {
42+ func TestGetWalletIDs (t * testing.T , store walletStoreConstructor , pf sq. PlaceholderFormat ) {
4143 gomega .RegisterTestingT (t )
4244 db , mockDB , err := sqlmock .New ()
4345 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
4446
4547 roleID := 5
4648 output := driver .WalletID ("my wallet" )
4749 mockDB .
48- ExpectQuery ("SELECT DISTINCT wallet_id FROM WALLETS WHERE role_id = \\ $1" ).
50+ ExpectQuery (sqlPattern ( pf , "SELECT DISTINCT wallet_id FROM WALLETS WHERE role_id = ?" ) ).
4951 WithArgs (roleID ).
5052 WillReturnRows (mockDB .NewRows ([]string {"wallet_id" }).AddRow (output ))
5153
52- actualWalletIDs , err := store (db ).GetWalletIDs (t .Context (), roleID )
54+ actualWalletIDs , err := store (db , pf ).GetWalletIDs (t .Context (), roleID )
5355
5456 gomega .Expect (mockDB .ExpectationsWereMet ()).To (gomega .Succeed ())
5557 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
5658 gomega .Expect (actualWalletIDs ).To (gomega .ConsistOf (output ))
5759}
5860
59- func TestLoadMeta (t * testing.T , store walletStoreConstructor ) {
61+ func TestLoadMeta (t * testing.T , store walletStoreConstructor , pf sq. PlaceholderFormat ) {
6062 gomega .RegisterTestingT (t )
6163 db , mockDB , err := sqlmock .New ()
6264 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
@@ -66,18 +68,18 @@ func TestLoadMeta(t *testing.T, store walletStoreConstructor) {
6668 walletID := driver .WalletID ("my wallet" )
6769 output := []byte ("some meta data" )
6870 mockDB .
69- ExpectQuery ("SELECT meta FROM WALLETS WHERE \\ (identity_hash = \\ $1 \\ ) AND \\ ( wallet_id = \\ $2 \\ ) AND \\ ( role_id = \\ $3 \\ )" ).
71+ ExpectQuery (sqlPattern ( pf , "SELECT meta FROM WALLETS WHERE (identity_hash = ? AND wallet_id = ? AND role_id = ?)" ) ).
7072 WithArgs (tokenID .UniqueID (), walletID , roleID ).
7173 WillReturnRows (mockDB .NewRows ([]string {"meta" }).AddRow (output ))
7274
73- actual , err := store (db ).LoadMeta (t .Context (), tokenID , walletID , roleID )
75+ actual , err := store (db , pf ).LoadMeta (t .Context (), tokenID , walletID , roleID )
7476
7577 gomega .Expect (mockDB .ExpectationsWereMet ()).To (gomega .Succeed ())
7678 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
7779 gomega .Expect (actual ).To (gomega .Equal (output ))
7880}
7981
80- func TestIdentityExists (t * testing.T , store walletStoreConstructor ) {
82+ func TestIdentityExists (t * testing.T , store walletStoreConstructor , pf sq. PlaceholderFormat ) {
8183 gomega .RegisterTestingT (t )
8284 db , mockDB , err := sqlmock .New ()
8385 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
@@ -86,18 +88,17 @@ func TestIdentityExists(t *testing.T, store walletStoreConstructor) {
8688 roleID := 5
8789 walletID := driver .WalletID ("my wallet" )
8890 mockDB .
89- ExpectQuery ("SELECT wallet_id FROM WALLETS WHERE \\ (identity_hash = \\ $1 \\ ) AND \\ ( wallet_id = \\ $2 \\ ) AND \\ ( role_id = \\ $3 \\ )" ).
91+ ExpectQuery (sqlPattern ( pf , "SELECT wallet_id FROM WALLETS WHERE (identity_hash = ? AND wallet_id = ? AND role_id = ?)" ) ).
9092 WithArgs (tokenID .UniqueID (), walletID , roleID ).
9193 WillReturnRows (mockDB .NewRows ([]string {"wallet_id" }).AddRow (walletID ))
9294
93- exists := store (db ).IdentityExists (t .Context (), tokenID , walletID , roleID )
95+ exists := store (db , pf ).IdentityExists (t .Context (), tokenID , walletID , roleID )
9496
9597 gomega .Expect (mockDB .ExpectationsWereMet ()).To (gomega .Succeed ())
96- gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
9798 gomega .Expect (exists ).To (gomega .BeTrue ())
9899}
99100
100- func TestStoreIdentity (t * testing.T , store walletStoreConstructor ) {
101+ func TestStoreIdentity (t * testing.T , store walletStoreConstructor , pf sq. PlaceholderFormat ) {
101102 gomega .RegisterTestingT (t )
102103 db , mockDB , err := sqlmock .New ()
103104 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
@@ -107,13 +108,11 @@ func TestStoreIdentity(t *testing.T, store walletStoreConstructor) {
107108 walletID := driver .WalletID ("my wallet" )
108109 roleID := 5
109110
110- mockDB .ExpectExec ("INSERT INTO WALLETS " +
111- "\\ (identity_hash, meta, wallet_id, role_id, created_at, enrollment_id\\ ) " +
112- "VALUES \\ (\\ $1, \\ $2, \\ $3, \\ $4, \\ $5, \\ $6\\ ) ON CONFLICT DO NOTHING" ).
111+ mockDB .ExpectExec (sqlPattern (pf , "INSERT INTO WALLETS (identity_hash,meta,wallet_id,role_id,created_at,enrollment_id) VALUES (?,?,?,?,?,?) ON CONFLICT DO NOTHING" )).
113112 WithArgs (tokenID .UniqueID (), []uint8 (nil ), walletID , roleID , sqlmock .AnyArg (), eID ).
114113 WillReturnResult (sqlmock .NewResult (1 , 1 ))
115114
116- err = store (db ).StoreIdentity (t .Context (), tokenID , eID , walletID , roleID , nil )
115+ err = store (db , pf ).StoreIdentity (t .Context (), tokenID , eID , walletID , roleID , nil )
117116
118117 gomega .Expect (mockDB .ExpectationsWereMet ()).To (gomega .Succeed ())
119118 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
@@ -129,9 +128,9 @@ func TestStoreIdentityIdempotent(t *testing.T, store walletStoreConstructor) {
129128 walletID := driver .WalletID ("my wallet" )
130129 roleID := 5
131130
132- insertQuery := "INSERT INTO WALLETS " +
133- " \\ (identity_hash, meta, wallet_id, role_id, created_at, enrollment_id \\ ) " +
134- "VALUES \\ ( \\ $1, \\ $2, \\ $3, \\ $4, \\ $5, \\ $6 \\ ) ON CONFLICT DO NOTHING"
131+ // Use Dollar format for mock patterns — tests idempotency, not SQL dialect
132+ pf := sq . Dollar
133+ insertQuery := sqlPattern ( pf , "INSERT INTO WALLETS (identity_hash,meta,wallet_id,role_id,created_at,enrollment_id) VALUES (?,?,?,?,?,? ) ON CONFLICT DO NOTHING")
135134
136135 // First call: row inserted (1 row affected)
137136 mockDB .ExpectExec (insertQuery ).
@@ -143,7 +142,7 @@ func TestStoreIdentityIdempotent(t *testing.T, store walletStoreConstructor) {
143142 WithArgs (tokenID .UniqueID (), []uint8 (nil ), walletID , roleID , sqlmock .AnyArg (), eID ).
144143 WillReturnResult (sqlmock .NewResult (0 , 0 ))
145144
146- s := store (db )
145+ s := store (db , pf )
147146 err = s .StoreIdentity (t .Context (), tokenID , eID , walletID , roleID , nil )
148147 gomega .Expect (err ).ToNot (gomega .HaveOccurred ())
149148
@@ -152,3 +151,11 @@ func TestStoreIdentityIdempotent(t *testing.T, store walletStoreConstructor) {
152151
153152 gomega .Expect (mockDB .ExpectationsWereMet ()).To (gomega .Succeed ())
154153}
154+
155+ func sqlPattern (pf sq.PlaceholderFormat , query string ) string {
156+ replaced , err := pf .ReplacePlaceholders (query )
157+ if err != nil {
158+ return query
159+ }
160+ return regexp .QuoteMeta (replaced )
161+ }
0 commit comments