-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_row_integration_test.go
141 lines (116 loc) · 2.94 KB
/
multi_row_integration_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
// Copyright (c) 2016, M Bogus.
// This source file is part of the batchsql open source project
// Licensed under Apache License v2.0
// See LICENSE file for license information
// +build integration
package batchsql
import (
"database/sql"
"testing"
_ "github.com/mattn/go-sqlite3"
)
func TestMultiInsert(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Error(err)
return
}
defer db.Close()
_, err = db.Exec("CREATE TABLE t (v INT)")
if err != nil {
t.Error(err)
return
}
mr := MultiRow{Conn: db, BatchSize: 5}
qry := "INSERT INTO t VALUES (?)"
count := 12
var args []([]interface{})
for i := 0; i < count; i++ {
args = append(args, []interface{}{i + 1})
}
err = mr.MultiInsert(qry, args...)
if err != nil {
t.Error(err)
return
}
var dbCount int
db.QueryRow("SELECT COUNT(1) FROM t").Scan(&dbCount)
if count != dbCount {
t.Errorf("Expected: %d got %d rows", count, dbCount)
}
}
func TestMultiInsertNoArgs(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Error(err)
return
}
defer db.Close()
mr := MultiRow{Conn: db, BatchSize: 5}
var args []([]interface{})
err = mr.MultiInsert("XXX", args...)
if err == nil {
t.Errorf("Expected error")
return
}
if err.Error() != "Invalid multi-row INSERT call with no arguments" {
t.Errorf("Unexpected error: %v", err)
}
}
func TestMultiInsertNoInsert(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Error(err)
return
}
defer db.Close()
mr := MultiRow{Conn: db, BatchSize: 5}
var args []([]interface{})
args = append(args, []interface{}{1})
err = mr.MultiInsert("UPDATE t SET v = 1", args...)
if err == nil {
t.Errorf("Expected error")
return
}
if err.Error() != "Invalid statement for multi-row INSERT: INSERT [ INTO ] <object> [ column_list ] VALUES sequence not recognized" {
t.Errorf("Unexpected error: %v", err)
}
}
func TestMultiInsertInvalidMultiInsert(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Error(err)
return
}
defer db.Close()
mr := MultiRow{Conn: db, BatchSize: 5}
var args []([]interface{})
args = append(args, []interface{}{11})
err = mr.MultiInsert("INSERT INTO t (v) SELECT a FROM b", args...)
if err == nil {
t.Errorf("Expected error")
return
}
if err.Error() != "Invalid statement for multi-row INSERT: ... VALUES (? [, ...?]) ... sequence not recognized" {
t.Errorf("Unexpected error: %v", err)
}
}
func TestMultiInsertInvalidMultiInsertNoValues(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Error(err)
return
}
defer db.Close()
mr := MultiRow{Conn: db, BatchSize: 5}
var args []([]interface{})
args = append(args, []interface{}{11})
err = mr.MultiInsert("INSERT INTO t (v) VALUES ( )", args...)
if err == nil {
t.Errorf("Expected error")
return
}
if err.Error() != "Unable to parse VALUES (? [, ...?]) sequence" {
t.Errorf("Unexpected error: %v", err)
}
}