-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_query_test.go
124 lines (104 loc) · 4.14 KB
/
reader_query_test.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package spnr
import (
"context"
"fmt"
"testing"
"cloud.google.com/go/spanner"
"github.com/stretchr/testify/assert"
)
func TestQuery(t *testing.T) {
ctx := context.Background()
assert.Nil(t, prepareReadTest(ctx))
var fetched []Test
err := testRepository.
Reader(ctx, dataClient.Single()).
Query("select * from Test order by `String` asc", nil, &fetched)
assert.Nil(t, err)
assert.Len(t, fetched, 2)
assert.Equal(t, testRecord1.String, fetched[0].String)
assert.Equal(t, testRecord1.NullString, fetched[0].NullString)
assert.Equal(t, testRecord1.NullInt64, fetched[0].NullInt64)
assert.Equal(t, testRecord1.ArrayInt64, fetched[0].ArrayInt64)
assert.Equal(t, testRecord2.String, fetched[1].String)
assert.Equal(t, testRecord2.NullString, fetched[1].NullString)
assert.Equal(t, testRecord2.NullInt64, fetched[1].NullInt64)
assert.Equal(t, testRecord2.ArrayInt64, fetched[1].ArrayInt64)
assert.Nil(t, cleanUpReadTest(ctx))
}
func TestQueryOne(t *testing.T) {
ctx := context.Background()
assert.Nil(t, prepareReadTest(ctx))
var fetched Test
params1 := map[string]any{"string": testRecord1.String}
err := testRepository.
Reader(ctx, dataClient.Single()).
QueryOne("select * from Test where `String` = @string", params1, &fetched)
assert.Nil(t, err)
assert.Equal(t, testRecord1.String, fetched.String)
assert.Equal(t, testRecord1.Bytes, fetched.Bytes)
assert.Equal(t, testRecord1.Int64, fetched.Int64)
assert.Equal(t, testRecord1.Float64, fetched.Float64)
assert.Equal(t, testRecord1.Date, fetched.Date)
assert.Equal(t, testRecord1.Timestamp, fetched.Timestamp)
assert.Equal(t, testRecord1.NullString, fetched.NullString)
assert.Equal(t, testRecord1.NullInt64, fetched.NullInt64)
assert.Equal(t, testRecord1.ArrayInt64, fetched.ArrayInt64)
assert.Equal(t, testRecord1.ArrayBytes, fetched.ArrayBytes)
assert.Nil(t, cleanUpReadTest(ctx))
}
func TestQueryOneOrderChanged(t *testing.T) {
ctx := context.Background()
assert.Nil(t, prepareReadTest(ctx))
var fetched TestOrderChanged
query := fmt.Sprintf("select %s from Test where `String` = @string", ToAllColumnNames(&TestOrderChanged{}))
params1 := map[string]any{"string": testRecord1.String}
err := testRepository.
Reader(ctx, dataClient.Single()).
QueryOne(query, params1, &fetched)
assert.Nil(t, err)
assert.Equal(t, testRecord1.String, fetched.String)
assert.Equal(t, testRecord1.Bytes, fetched.Bytes)
assert.Equal(t, testRecord1.Int64, fetched.Int64)
assert.Equal(t, testRecord1.Float64, fetched.Float64)
assert.Equal(t, testRecord1.Date, fetched.Date)
assert.Equal(t, testRecord1.Timestamp, fetched.Timestamp)
assert.Equal(t, testRecord1.NullString, fetched.NullString)
assert.Equal(t, testRecord1.NullInt64, fetched.NullInt64)
assert.Equal(t, testRecord1.ArrayInt64, fetched.ArrayInt64)
assert.Equal(t, testRecord1.ArrayBytes, fetched.ArrayBytes)
assert.Nil(t, cleanUpReadTest(ctx))
}
func TestQueryAsFields(t *testing.T) {
ctx := context.Background()
assert.Nil(t, prepareReadTest(ctx))
var fetched []spanner.NullInt64
err := testRepository.
Reader(ctx, dataClient.Single()).
QueryValues("select NullInt64 from Test order by NullInt64 desc", nil, &fetched)
assert.Nil(t, err)
assert.Len(t, fetched, 2)
assert.True(t, fetched[0].Valid)
assert.True(t, fetched[1].Valid)
assert.Equal(t, testRecord2.NullInt64.Int64, fetched[0].Int64)
assert.Equal(t, testRecord1.NullInt64.Int64, fetched[1].Int64)
assert.Nil(t, cleanUpReadTest(ctx))
}
func TestQueryOneAsField(t *testing.T) {
ctx := context.Background()
assert.Nil(t, prepareReadTest(ctx))
var arrayInt641 []int64
params1 := map[string]any{"string": testRecord1.String}
err := testRepository.
Reader(ctx, dataClient.Single()).
QueryValue("select ArrayInt64 from Test where `String` = @string", params1, &arrayInt641)
assert.Nil(t, err)
assert.Equal(t, testRecord1.ArrayInt64, arrayInt641)
var arrayInt642 []int64
params2 := map[string]any{"string": testRecord2.String}
err = testRepository.
Reader(ctx, dataClient.Single()).
QueryValue("select ArrayInt64 from Test where `String` = @string", params2, &arrayInt642)
assert.Nil(t, err)
assert.Equal(t, testRecord2.ArrayInt64, arrayInt642)
assert.Nil(t, cleanUpReadTest(ctx))
}