Skip to content

Commit fc8eb06

Browse files
author
Hayim.Shaul@ibm.com
committed
add integration test for PutBindings
Signed-off-by: Hayim.Shaul@ibm.com <hayimsha@fhe3.haifa.ibm.com>
1 parent e055350 commit fc8eb06

File tree

3 files changed

+105
-16
lines changed

3 files changed

+105
-16
lines changed

platform/view/services/storage/driver/sql/postgres/binding.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,20 @@ func newBindingStore(readDB, writeDB *sql.DB, table string) *BindingStore {
4040
}
4141
}
4242

43-
func (db *BindingStore) PutBindings(ctx context.Context, longTerm view.Identity, ephemeral ...view.Identity) error {
44-
if len(ephemeral) == 0 {
43+
func (db *BindingStore) PutBindings(ctx context.Context, longTerm view.Identity, ephemerals ...view.Identity) error {
44+
if len(ephemerals) == 0 {
4545
return nil
4646
}
4747

48-
logger.DebugfContext(ctx, "put bindings for %d ephemeral(s) with long term [%s]", len(ephemeral), longTerm.UniqueID())
49-
50-
// Resolve canonical long-term ID
51-
if lt, err := db.GetLongTerm(ctx, longTerm); err != nil {
52-
return err
53-
} else if lt != nil && !lt.IsNone() {
54-
logger.DebugfContext(ctx, "replacing [%s] with long term [%s]", longTerm.UniqueID(), lt.UniqueID())
55-
longTerm = lt
56-
} else {
57-
logger.DebugfContext(ctx, "Id [%s] is an unregistered long term ID", longTerm.UniqueID())
58-
}
48+
logger.DebugfContext(ctx, "put bindings for %d ephemeral(s) with long term [%s]", len(ephemerals), longTerm.UniqueID())
5949

6050
// Build single INSERT with multiple VALUES
6151
query := fmt.Sprintf(`INSERT INTO %s (ephemeral_hash, long_term_id) VALUES `, db.table)
6252

6353
args := []interface{}{}
6454
argsReferences := []string{"($1, $2)"}
6555
args = append(args, longTerm.UniqueID(), longTerm)
66-
for i, eph := range ephemeral {
56+
for i, eph := range ephemerals {
6757
args = append(args, eph.UniqueID(), longTerm)
6858
oneArgRef := fmt.Sprintf("($%d, $%d)", i*2+3, i*2+4)
6959
argsReferences = append(argsReferences, oneArgRef)
@@ -76,11 +66,11 @@ func (db *BindingStore) PutBindings(ctx context.Context, longTerm view.Identity,
7666

7767
_, err := db.writeDB.ExecContext(ctx, query, args...)
7868
if err == nil {
79-
logger.DebugfContext(ctx, "long-term and ephemeral ids registered [%s,%s]", longTerm, ephemeral)
69+
logger.DebugfContext(ctx, "long-term and ephemeral ids registered [%s,%s]", longTerm, ephemerals)
8070
return nil
8171
}
8272
if errors.Is(db.errorWrapper.WrapError(err), driver.UniqueKeyViolation) {
83-
logger.InfofContext(ctx, "some tuples [%v, %s] already in db. Skipping...", ephemeral, longTerm)
73+
logger.InfofContext(ctx, "some tuples [%v, %s] already in db. Skipping...", ephemerals, longTerm)
8474
return nil
8575
}
8676
return errors.Wrapf(err, "failed executing query [%s]", query)

platform/view/services/storage/driver/sql/postgres/binding_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ SPDX-License-Identifier: Apache-2.0
77
package postgres
88

99
import (
10+
"context"
1011
"testing"
1112

1213
"github.com/DATA-DOG/go-sqlmock"
1314
common3 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/common"
15+
testing2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/common/testing"
1416
common2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/common"
17+
18+
// postgres2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/postgres"
19+
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
1520
"github.com/stretchr/testify/require"
1621
)
1722

@@ -38,3 +43,53 @@ func TestPutBindingsMultipleEphemerals(t *testing.T) {
3843

3944
common2.PutBindings(t, store, mock)
4045
}
46+
47+
func TestPutBindingsMultipleEphemeralsFull(t *testing.T) {
48+
// if os.Getenv("TEST_POSTGRES") != "true" {
49+
// t.Skip("set environment variable TEST_POSTGRES to true to include postgres test")
50+
// }
51+
// if testing.Short() {
52+
// t.Skip("skipping postgres test in short mode")
53+
// }
54+
55+
t.Log("starting postgres")
56+
// terminate, pgConnStr, err := postgres2.StartPostgres(t, false)
57+
terminate, pgConnStr, err := StartPostgres(t, false)
58+
require.NoError(t, err)
59+
defer terminate()
60+
t.Log("postgres ready")
61+
62+
cp := NewConfigProvider(testing2.MockConfig(Config{
63+
DataSource: pgConnStr,
64+
}))
65+
db, err := NewPersistenceWithOpts(cp, NewDbProvider(), "", NewBindingStore)
66+
require.NoError(t, err)
67+
68+
ctx := context.Background()
69+
70+
// Input identities
71+
longTerm := view.Identity("long")
72+
e1 := view.Identity("eph1")
73+
e2 := view.Identity("eph2")
74+
75+
// Check that store does not have bindings for e1 and e2
76+
lt, err := db.GetLongTerm(ctx, e1)
77+
require.NoError(t, err)
78+
require.ElementsMatch(t, len(lt), 0)
79+
lt, err = db.GetLongTerm(ctx, e2)
80+
require.NoError(t, err)
81+
require.ElementsMatch(t, len(lt), 0)
82+
83+
// Create new bindings
84+
err = db.PutBindings(ctx, longTerm, e1, e2)
85+
require.NoError(t, err)
86+
87+
// Check that the bindings where correctly written
88+
lt, err = db.GetLongTerm(ctx, e1)
89+
require.NoError(t, err)
90+
require.ElementsMatch(t, lt, longTerm)
91+
92+
lt, err = db.GetLongTerm(ctx, e2)
93+
require.NoError(t, err)
94+
require.ElementsMatch(t, lt, longTerm)
95+
}

platform/view/services/storage/driver/sql/sqlite/binding_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ SPDX-License-Identifier: Apache-2.0
77
package sqlite
88

99
import (
10+
"context"
11+
"fmt"
12+
"path"
1013
"testing"
1114

1215
"github.com/DATA-DOG/go-sqlmock"
16+
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils"
1317
common3 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/common"
1418
common2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/common"
19+
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
20+
"github.com/stretchr/testify/assert"
1521
"github.com/stretchr/testify/require"
1622
)
1723

@@ -38,3 +44,41 @@ func TestPutBindingsMultipleEphemerals(t *testing.T) {
3844

3945
common2.PutBindings(t, store, mock)
4046
}
47+
48+
func TestPutBindingsMultipleEphemeralsFull(t *testing.T) {
49+
tempDir := t.TempDir()
50+
o := Opts{
51+
DataSource: fmt.Sprintf("file:%s.sqlite?_pragma=busy_timeout(1000)", path.Join(tempDir, "benchmark")),
52+
}
53+
dbs := utils.MustGet(open(o))
54+
tables := common2.GetTableNames(o.TablePrefix, o.TableNameParams...)
55+
db := newBindingStore(dbs.ReadDB, dbs.WriteDB, tables.Binding)
56+
assert.NoError(t, db.CreateSchema())
57+
ctx := context.Background()
58+
59+
// Input identities
60+
longTerm := view.Identity("long")
61+
e1 := view.Identity("eph1")
62+
e2 := view.Identity("eph2")
63+
64+
// Check that store does not have bindings for e1 and e2
65+
lt, err := db.GetLongTerm(ctx, e1)
66+
require.NoError(t, err)
67+
require.ElementsMatch(t, len(lt), 0)
68+
lt, err = db.GetLongTerm(ctx, e2)
69+
require.NoError(t, err)
70+
require.ElementsMatch(t, len(lt), 0)
71+
72+
// Create new bindings
73+
err = db.PutBindings(ctx, longTerm, e1, e2)
74+
require.NoError(t, err)
75+
76+
// Check that the bindings where correctly written
77+
lt, err = db.GetLongTerm(ctx, e1)
78+
require.NoError(t, err)
79+
require.ElementsMatch(t, lt, longTerm)
80+
81+
lt, err = db.GetLongTerm(ctx, e2)
82+
require.NoError(t, err)
83+
require.ElementsMatch(t, lt, longTerm)
84+
}

0 commit comments

Comments
 (0)