forked from LFDT-Panurus/panurus
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery_test.go
More file actions
106 lines (91 loc) · 3.1 KB
/
Copy pathquery_test.go
File metadata and controls
106 lines (91 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package common_test
import (
"testing"
q "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query"
qcommon "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/common"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/query/cond"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/sqlite"
"github.com/stretchr/testify/assert"
)
func TestSelect_Compile(t *testing.T) {
// Simple SELECT
query, args := q.Select().
FieldsByName("id", "name").
From(q.Table("users")).
Where(cond.Eq("id", 1)).
Format(sqlite.NewConditionInterpreter())
assert.Equal(t, "SELECT id, name FROM users WHERE id = $1", query)
assert.Equal(t, 1, args[0])
// SELECT DISTINCT
query, args = q.SelectDistinct().
FieldsByName("email_id").
From(q.Table("customers")).
Format(sqlite.NewConditionInterpreter())
assert.Equal(t, "SELECT DISTINCT email_id FROM customers", query)
assert.Empty(t, args)
// No columns selected
query, args = q.Select().
AllFields().
From(q.Table("users")).
Format(sqlite.NewConditionInterpreter())
assert.Equal(t, "SELECT * FROM users", query)
assert.Empty(t, args)
}
func TestInsert_Compile(t *testing.T) {
// Simple INSERT
query, args := q.InsertInto("users").
Fields("id", "name").
Row(1, "nnn").
Format()
assert.Equal(t, "INSERT INTO users (id, name) VALUES ($1, $2)", query)
assert.Equal(t, 1, args[0])
assert.Equal(t, "nnn", args[1])
}
func TestUpdate_Compile(t *testing.T) {
// Simple UPDATE
query, args := q.Update("users").
Set("name", "TheName").
Set("age", 16).
Where(cond.Eq("id", 1)).
Format(sqlite.NewConditionInterpreter())
assert.Equal(t, "UPDATE users SET name = $1, age = $2 WHERE id = $3", query)
assert.Equal(t, "TheName", args[0])
assert.Equal(t, 16, args[1])
assert.Equal(t, 1, args[2])
}
func TestDelete_Compile(t *testing.T) {
// Simple DELETE
query, args := q.DeleteFrom("users").
Where(cond.Eq("id", 1)).
Format(sqlite.NewConditionInterpreter())
assert.Equal(t, "DELETE FROM users WHERE id = $1", query)
assert.Equal(t, 1, args[0])
}
// TestUnionAll_Compile verifies that two SELECT queries can be combined into
// a single UNION ALL statement via a shared builder, with placeholder
// numbering continuing across branches and args concatenated in order.
// This is the pattern used by UnspentTokensIteratorBy.
func TestUnionAll_Compile(t *testing.T) {
ci := sqlite.NewConditionInterpreter()
branch1 := q.Select().
FieldsByName("id", "name").
From(q.Table("users")).
Where(cond.Eq("id", 1))
branch2 := q.Select().
FieldsByName("id", "name").
From(q.Table("admins")).
Where(cond.Eq("name", "alice"))
sb := qcommon.NewBuilder()
branch1.FormatTo(ci, sb)
sb.WriteString(" UNION ALL ")
branch2.FormatTo(ci, sb)
query, args := sb.Build()
assert.Equal(t,
"SELECT id, name FROM users WHERE id = $1 UNION ALL SELECT id, name FROM admins WHERE name = $2",
query)
assert.Equal(t, []any{1, "alice"}, args)
}