Skip to content

Commit 3c9fbd5

Browse files
committed
test: tokenLock queries
Signed-off-by: ADIR <adir@il.ibm.com>
1 parent 58e65dc commit 3c9fbd5

File tree

3 files changed

+159
-45
lines changed

3 files changed

+159
-45
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package common
8+
9+
import (
10+
"context"
11+
"database/sql"
12+
"testing"
13+
14+
"github.com/DATA-DOG/go-sqlmock"
15+
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
16+
"github.com/onsi/gomega"
17+
)
18+
19+
type storeConstructor func(*sql.DB) *TokenLockStore
20+
21+
func TestLock(t *testing.T, store storeConstructor) {
22+
gomega.RegisterTestingT(t)
23+
db, mockDB, err := sqlmock.New()
24+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
25+
26+
tokenID := token.ID{TxId: "1234", Index: 5}
27+
trID := "5555"
28+
now := sqlmock.AnyArg()
29+
30+
mockDB.
31+
ExpectExec("INSERT INTO TOKEN_LOCKS \\(consumer_tx_id, tx_id, idx, created_at\\) VALUES \\(\\$1, \\$2, \\$3, \\$4\\)").
32+
WithArgs(trID, tokenID.TxId, tokenID.Index, now).
33+
WillReturnResult(sqlmock.NewResult(0, 1))
34+
35+
err = store(db).Lock(context.Background(), &tokenID, trID)
36+
37+
gomega.Expect(mockDB.ExpectationsWereMet()).To(gomega.Succeed())
38+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
39+
}
40+
41+
func TestUnlockByTxID(t *testing.T, store storeConstructor) {
42+
gomega.RegisterTestingT(t)
43+
db, mockDB, err := sqlmock.New()
44+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
45+
46+
consumerTxID := "1234"
47+
48+
mockDB.
49+
ExpectExec("DELETE FROM TOKEN_LOCKS WHERE consumer_tx_id = \\$1").
50+
WithArgs(consumerTxID).
51+
WillReturnResult(sqlmock.NewResult(0, 1))
52+
53+
err = store(db).UnlockByTxID(context.Background(), consumerTxID)
54+
55+
gomega.Expect(mockDB.ExpectationsWereMet()).To(gomega.Succeed())
56+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
57+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package postgres
8+
9+
import (
10+
"context"
11+
"database/sql"
12+
driver2 "database/sql/driver"
13+
"testing"
14+
"time"
15+
16+
"github.com/DATA-DOG/go-sqlmock"
17+
common2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/common"
18+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/driver"
19+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/sql/common"
20+
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
21+
"github.com/onsi/gomega"
22+
)
23+
24+
func mockTokenLockStorePostgress(db *sql.DB) *TokenLockStore {
25+
var dbs = common2.RWDB{
26+
ReadDB: db, WriteDB: db,
27+
}
28+
29+
store, _ := NewTokenLockStore(&dbs, common.TableNames{
30+
TokenLocks: "TOKEN_LOCKS",
31+
Requests: "REQUESTS",
32+
})
33+
return store
34+
}
35+
36+
func mockTokenLockStore(db *sql.DB) *common.TokenLockStore {
37+
var dbs = common2.RWDB{
38+
ReadDB: db, WriteDB: db,
39+
}
40+
41+
store, _ := NewTokenLockStore(&dbs, common.TableNames{
42+
TokenLocks: "TOKEN_LOCKS",
43+
Requests: "REQUESTS",
44+
})
45+
return store.TokenLockStore
46+
}
47+
48+
func TestLogStaleLocks(t *testing.T) {
49+
gomega.RegisterTestingT(t)
50+
db, mockDB, err := sqlmock.New()
51+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
52+
53+
input := driver.Deleted
54+
55+
record := LockEntry{
56+
ConsumerTxID: "1234",
57+
TokenID: token.ID{TxId: "5678", Index: 5},
58+
Status: &input,
59+
CreatedAt: time.Date(2025, time.June, 8, 10, 0, 0, 0, time.UTC),
60+
}
61+
var timeNow time.Time
62+
output := []driver2.Value{
63+
record.ConsumerTxID, record.TokenID.TxId, record.TokenID.Index, *record.Status, record.CreatedAt, timeNow,
64+
}
65+
mockDB.
66+
ExpectQuery("SELECT TOKEN_LOCKS.consumer_tx_id, TOKEN_LOCKS.tx_id, TOKEN_LOCKS.idx, REQUESTS.status, TOKEN_LOCKS.created_at, NOW\\(\\) AS now " +
67+
"FROM TOKEN_LOCKS LEFT JOIN REQUESTS ON TOKEN_LOCKS.consumer_tx_id = REQUESTS.tx_id " +
68+
"WHERE \\(\\(\\(REQUESTS.status = \\$1\\)\\)\\) OR \\(TOKEN_LOCKS.created_at < NOW\\(\\) - INTERVAL '1 seconds'\\)").
69+
WithArgs(input).
70+
WillReturnRows(mockDB.NewRows([]string{"consumer_tx_id", "tx_id", "idx", "status", "created_at", "now"}).AddRow(output...))
71+
72+
mockDB.ExpectExec("DELETE FROM TOKEN_LOCKS USING REQUESTS WHERE " +
73+
"\\(TOKEN_LOCKS.consumer_tx_id = REQUESTS.tx_id\\) AND " +
74+
"\\(\\(\\(\\(REQUESTS.status = \\$1\\)\\)\\) OR \\(TOKEN_LOCKS.created_at < NOW\\(\\) - INTERVAL '1 seconds'\\)\\)").
75+
WithArgs(input).
76+
WillReturnResult(sqlmock.NewResult(0, 1))
77+
78+
err = mockTokenLockStorePostgress(db).Cleanup(context.Background(), time.Second)
79+
80+
gomega.Expect(mockDB.ExpectationsWereMet()).To(gomega.Succeed())
81+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
82+
}
83+
84+
func TestLock(t *testing.T) {
85+
common.TestLock(t, mockTokenLockStore)
86+
}
87+
88+
func TestUnlockByTxID(t *testing.T) {
89+
common.TestUnlockByTxID(t, mockTokenLockStore)
90+
}

token/services/db/sql/sqlite/tokenlock_test.go

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,84 +7,51 @@ SPDX-License-Identifier: Apache-2.0
77
package sqlite
88

99
import (
10-
"context"
1110
"database/sql"
12-
driver2 "database/sql/driver"
1311
"testing"
1412
"time"
1513

16-
"github.com/DATA-DOG/go-sqlmock"
1714
common2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/common"
1815
q "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/query"
1916
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/sqlite"
2017
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/driver"
2118
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/sql/common"
22-
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/sql/postgres"
23-
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
24-
. "github.com/onsi/gomega"
19+
"github.com/onsi/gomega"
2520
)
2621

27-
func mockTokenLockStore(db *sql.DB) *postgres.TokenLockStore {
22+
func mockTokenLockStore(db *sql.DB) *common.TokenLockStore {
2823
var dbs = common2.RWDB{
2924
ReadDB: db, WriteDB: db,
3025
}
3126

32-
val, _ := postgres.NewTokenLockStore(&dbs, common.TableNames{
27+
store, _ := NewTokenLockStore(&dbs, common.TableNames{
3328
TokenLocks: "TOKEN_LOCKS",
3429
Requests: "REQUESTS",
3530
})
36-
return val
31+
return store.TokenLockStore
3732
}
3833

3934
func TestIsStale(t *testing.T) {
40-
RegisterTestingT(t)
35+
gomega.RegisterTestingT(t)
4136

4237
query, args := q.DeleteFrom("TokenLocks").
4338
Where(IsStale("TokenLocks", "Requests", 5*time.Second)).
4439
Format(sqlite.NewConditionInterpreter())
4540

46-
Expect(query).To(Equal("DELETE FROM TokenLocks WHERE tx_id IN (" +
41+
gomega.Expect(query).To(gomega.Equal("DELETE FROM TokenLocks WHERE tx_id IN (" +
4742
"SELECT tl.tx_id " +
4843
"FROM TokenLocks AS tl " +
4944
"LEFT JOIN Requests AS tr " +
5045
"ON tl.tx_id = tr.tx_id " +
5146
"WHERE (tr.status = $1) OR (tl.created_at < datetime('now', '-5 seconds'))" +
5247
")"))
53-
Expect(args).To(ConsistOf(driver.Deleted))
48+
gomega.Expect(args).To(gomega.ConsistOf(driver.Deleted))
5449
}
5550

56-
func TestLogStaleLocks(t *testing.T) {
57-
RegisterTestingT(t)
58-
db, mockDB, err := sqlmock.New()
59-
Expect(err).ToNot(HaveOccurred())
60-
61-
input := driver.Deleted
62-
63-
record := postgres.LockEntry{
64-
ConsumerTxID: "1234",
65-
TokenID: token.ID{TxId: "5678", Index: 5},
66-
Status: &input,
67-
CreatedAt: time.Date(2025, time.June, 8, 10, 0, 0, 0, time.UTC),
68-
}
69-
var timeNow time.Time
70-
output := []driver2.Value{
71-
record.ConsumerTxID, record.TokenID.TxId, record.TokenID.Index, *record.Status, record.CreatedAt, timeNow,
72-
}
73-
mockDB.
74-
ExpectQuery("SELECT TOKEN_LOCKS.consumer_tx_id, TOKEN_LOCKS.tx_id, TOKEN_LOCKS.idx, REQUESTS.status, TOKEN_LOCKS.created_at, NOW\\(\\) AS now " +
75-
"FROM TOKEN_LOCKS LEFT JOIN REQUESTS ON TOKEN_LOCKS.consumer_tx_id = REQUESTS.tx_id " +
76-
"WHERE \\(\\(\\(REQUESTS.status = \\$1\\)\\)\\) OR \\(TOKEN_LOCKS.created_at < NOW\\(\\) - INTERVAL '1 seconds'\\)").
77-
WithArgs(input).
78-
WillReturnRows(mockDB.NewRows([]string{"consumer_tx_id", "tx_id", "idx", "status", "created_at", "now"}).AddRow(output...))
79-
80-
mockDB.ExpectExec("DELETE FROM TOKEN_LOCKS USING REQUESTS WHERE " +
81-
"\\(TOKEN_LOCKS.consumer_tx_id = REQUESTS.tx_id\\) AND " +
82-
"\\(\\(\\(\\(REQUESTS.status = \\$1\\)\\)\\) OR \\(TOKEN_LOCKS.created_at < NOW\\(\\) - INTERVAL '1 seconds'\\)\\)").
83-
WithArgs(input).
84-
WillReturnResult(sqlmock.NewResult(0, 1))
85-
86-
err = mockTokenLockStore(db).Cleanup(context.Background(), time.Second)
51+
func TestLock(t *testing.T) {
52+
common.TestLock(t, mockTokenLockStore)
53+
}
8754

88-
Expect(mockDB.ExpectationsWereMet()).To(Succeed())
89-
Expect(err).ToNot(HaveOccurred())
55+
func TestUnlockByTxID(t *testing.T) {
56+
common.TestUnlockByTxID(t, mockTokenLockStore)
9057
}

0 commit comments

Comments
 (0)