-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_insert_test.go
178 lines (142 loc) · 4.56 KB
/
integration_insert_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
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"bytes"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func testInsert_SingleTable(t *testing.T, createTestContext func(t testing.TB) *TestContext) {
t.Run("NoTable", func(t *testing.T) {
t.Parallel()
tc := createTestContext(t)
defer tc.CleanUp(t)
client := tc.Client()
_, _, err := client.From("test").
Insert(map[string]interface{}{"id": 1}, false, "", "", "").
Execute()
assert.Error(t, err)
assert.Contains(t, err.Error(), "no such table: test")
})
t.Run("InsertSingleValue", func(t *testing.T) {
t.Parallel()
tc := createTestContext(t)
defer tc.CleanUp(t)
tc.ExecuteSQL(t, "CREATE TABLE test (id int)")
client := tc.Client()
_, _, err := client.From("test").
Insert(map[string]interface{}{"id": 1}, false, "", "", "").
Execute()
assert.NoError(t, err)
res, _, err := client.From("test").Select("id", "", false).
Execute()
assert.NoError(t, err)
var rv []map[string]interface{}
tc.DecodeResult(t, res, &rv)
assert.Len(t, rv, 1)
assert.EqualValues(t, 1, rv[0]["id"])
})
t.Run("InsertValues", func(t *testing.T) {
t.Parallel()
tc := createTestContext(t)
defer tc.CleanUp(t)
tc.ExecuteSQL(t, "CREATE TABLE test (id int)")
client := tc.Client()
_, _, err := client.From("test").
Insert([]map[string]interface{}{{"id": 1}, {"id": 1}}, false, "", "", "").
Execute()
assert.NoError(t, err)
res, _, err := client.From("test").Select("id", "", false).
Execute()
assert.NoError(t, err)
var rv []map[string]interface{}
tc.DecodeResult(t, res, &rv)
assert.Len(t, rv, 2)
for _, row := range rv {
assert.EqualValues(t, 1, row["id"])
}
})
t.Run("UpsertMergeDuplicates", func(t *testing.T) {
t.Parallel()
tc := createTestContext(t)
defer tc.CleanUp(t)
tc.ExecuteSQL(t, "CREATE TABLE test (id int primary key, s text)")
tc.ExecuteSQL(t, `INSERT INTO test (id, s) values (1, "a"), (2, "b")`)
client := tc.Client()
_, _, err := client.From("test").
Insert([]map[string]interface{}{
{"id": 1, "s": "b"}, {"id": 2, "s": "c"},
}, true, "", "", "").
Execute()
assert.NoError(t, err)
res, _, err := client.From("test").Select("*", "", false).
Execute()
assert.NoError(t, err)
var rv []map[string]interface{}
tc.DecodeResult(t, res, &rv)
assert.Len(t, rv, 2)
for idx, row := range rv {
assert.EqualValues(t, idx+1, row["id"])
assert.EqualValues(t, string('a'+rune(idx+1)), row["s"])
}
})
t.Run("UpsertIgnoreDuplicates", func(t *testing.T) {
t.Parallel()
tc := createTestContext(t)
defer tc.CleanUp(t)
tc.ExecuteSQL(t, "CREATE TABLE test (id int primary key, s text)")
tc.ExecuteSQL(t, `INSERT INTO test (id, s) values (1, "a"), (2, "b")`)
payload := bytes.NewBufferString(`[{"id": 1, "s": "b"}, {"id": 2, "s": "c"}]`)
req := tc.NewRequest(t, http.MethodPost, "test", payload)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Prefer", "resolution=ignore-duplicates")
resp := tc.ExecuteRequest(t, req)
defer resp.Body.Close()
client := tc.Client()
res, _, err := client.From("test").Select("*", "", false).
Execute()
assert.NoError(t, err)
var rv []map[string]interface{}
tc.DecodeResult(t, res, &rv)
assert.Len(t, rv, 2)
for idx, row := range rv {
assert.EqualValues(t, idx+1, row["id"])
assert.EqualValues(t, string('a'+rune(idx)), row["s"])
}
})
t.Run("UpsertMergeDuplicatesWithOnConflicts", func(t *testing.T) {
t.Parallel()
tc := createTestContext(t)
defer tc.CleanUp(t)
tc.ExecuteSQL(t, "CREATE TABLE test (id int, s text)")
tc.ExecuteSQL(t, "CREATE UNIQUE INDEX test_id on test (id)")
tc.ExecuteSQL(t, `INSERT INTO test (id, s) values (1, "a"), (2, "b")`)
client := tc.Client()
_, _, err := client.From("test").
Insert([]map[string]interface{}{
{"id": 1, "s": "b"}, {"id": 2, "s": "c"},
}, true, "id", "", "").
Execute()
assert.NoError(t, err)
res, _, err := client.From("test").Select("*", "", false).
Execute()
assert.NoError(t, err)
var rv []map[string]interface{}
tc.DecodeResult(t, res, &rv)
assert.Len(t, rv, 2)
for idx, row := range rv {
assert.EqualValues(t, idx+1, row["id"])
assert.EqualValues(t, string('a'+rune(idx+1)), row["s"])
}
})
}
func TestInsert_SingleTable(t *testing.T) {
t.Run("in memory db", func(t *testing.T) {
testInsert_SingleTable(t, createTestContextUsingInMemoryDB)
})
t.Run("HMAC token auth", func(t *testing.T) {
testInsert_SingleTable(t, createTestContextWithHMACTokenAuth)
})
t.Run("RSA token auth", func(t *testing.T) {
testDelete_SingleTable(t, createTestContextWithRSATokenAuth)
})
}