-
Notifications
You must be signed in to change notification settings - Fork 5
/
row_test.go
165 lines (137 loc) · 4.52 KB
/
row_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package freedb
import (
"context"
"fmt"
"strconv"
"testing"
"github.com/FreeLeh/GoFreeDB/google/auth"
"github.com/stretchr/testify/assert"
)
type testPerson struct {
Name string `json:"name" db:"name"`
Age int64 `json:"age" db:"age"`
DOB string `json:"dob" db:"dob"`
}
func TestGoogleSheetRowStore_Integration(t *testing.T) {
spreadsheetID, authJSON, shouldRun := getIntegrationTestInfo()
if !shouldRun {
t.Skip("integration test should be run only in GitHub Actions")
}
sheetName := fmt.Sprintf("integration_row_%d", currentTimeMs())
googleAuth, err := auth.NewServiceFromJSON([]byte(authJSON), auth.GoogleSheetsReadWrite, auth.ServiceConfig{})
if err != nil {
t.Fatalf("error when instantiating google auth: %s", err)
}
db := NewGoogleSheetRowStore(
googleAuth,
spreadsheetID,
sheetName,
GoogleSheetRowStoreConfig{Columns: []string{"name", "age", "dob"}},
)
defer func() {
deleteSheet(t, db.wrapper, spreadsheetID, []string{db.sheetName})
_ = db.Close(context.Background())
}()
var out []testPerson
err = db.Select(&out, "name", "age").Offset(10).Limit(10).Exec(context.Background())
assert.Nil(t, err)
assert.Empty(t, out)
err = db.Insert(
testPerson{"name1", 10, "1999-01-01"},
testPerson{"name2", 11, "2000-01-01"},
).Exec(context.Background())
assert.Nil(t, err)
// Nil type
err = db.Insert(nil).Exec(context.Background())
assert.NotNil(t, err)
err = db.Insert(testPerson{
Name: "name3",
Age: 9007199254740992,
DOB: "2001-01-01",
}).Exec(context.Background())
assert.Nil(t, err)
err = db.Update(map[string]interface{}{"name": "name4"}).
Where("age = ?", 10).
Exec(context.Background())
assert.Nil(t, err)
expected := []testPerson{
{"name2", 11, "2000-01-01"},
{"name3", 9007199254740992, "2001-01-01"},
}
err = db.Select(&out, "name", "age", "dob").
Where("name = ? OR name = ?", "name2", "name3").
OrderBy([]ColumnOrderBy{{"name", OrderByAsc}}).
Limit(2).
Exec(context.Background())
assert.Nil(t, err)
assert.Equal(t, expected, out)
count, err := db.Count().
Where("name = ? OR name = ?", "name2", "name3").
Exec(context.Background())
assert.Nil(t, err)
assert.Equal(t, uint64(2), count)
err = db.Delete().Where("name = ?", "name4").Exec(context.Background())
assert.Nil(t, err)
}
func TestGoogleSheetRowStore_Integration_EdgeCases(t *testing.T) {
spreadsheetID, authJSON, shouldRun := getIntegrationTestInfo()
if !shouldRun {
t.Skip("integration test should be run only in GitHub Actions")
}
sheetName := fmt.Sprintf("integration_row_%d", currentTimeMs())
googleAuth, err := auth.NewServiceFromJSON([]byte(authJSON), auth.GoogleSheetsReadWrite, auth.ServiceConfig{})
if err != nil {
t.Fatalf("error when instantiating google auth: %s", err)
}
db := NewGoogleSheetRowStore(
googleAuth,
spreadsheetID,
sheetName,
GoogleSheetRowStoreConfig{Columns: []string{"name", "age", "dob"}},
)
defer func() {
deleteSheet(t, db.wrapper, spreadsheetID, []string{db.sheetName})
_ = db.Close(context.Background())
}()
// Non-struct types
err = db.Insert([]interface{}{"name3", 12, "2001-01-01"}).Exec(context.Background())
assert.NotNil(t, err)
// IEEE 754 unsafe integer
err = db.Insert([]interface{}{"name3", 9007199254740993, "2001-01-01"}).Exec(context.Background())
assert.NotNil(t, err)
// IEEE 754 unsafe integer
err = db.Insert(
testPerson{"name1", 10, "1999-01-01"},
testPerson{"name2", 11, "2000-01-01"},
).Exec(context.Background())
assert.Nil(t, err)
err = db.Update(map[string]interface{}{"name": "name4", "age": int64(9007199254740993)}).
Exec(context.Background())
assert.NotNil(t, err)
}
func TestInjectTimestampCol(t *testing.T) {
result := injectTimestampCol(GoogleSheetRowStoreConfig{Columns: []string{"col1", "col2"}})
assert.Equal(t, GoogleSheetRowStoreConfig{Columns: []string{rowIdxCol, "col1", "col2"}}, result)
}
func TestGoogleSheetRowStoreConfig_validate(t *testing.T) {
t.Run("empty_columns", func(t *testing.T) {
conf := GoogleSheetRowStoreConfig{Columns: []string{}}
assert.NotNil(t, conf.validate())
})
t.Run("too_many_columns", func(t *testing.T) {
columns := make([]string, 0)
for i := 0; i < 27; i++ {
columns = append(columns, strconv.FormatInt(int64(i), 10))
}
conf := GoogleSheetRowStoreConfig{Columns: columns}
assert.NotNil(t, conf.validate())
})
t.Run("no_error", func(t *testing.T) {
columns := make([]string, 0)
for i := 0; i < 10; i++ {
columns = append(columns, strconv.FormatInt(int64(i), 10))
}
conf := GoogleSheetRowStoreConfig{Columns: columns}
assert.Nil(t, conf.validate())
})
}