This repository was archived by the owner on Nov 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdml-batch-insert_test.go
More file actions
111 lines (98 loc) · 3.44 KB
/
Copy pathdml-batch-insert_test.go
File metadata and controls
111 lines (98 loc) · 3.44 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
107
108
109
110
111
package shared
import (
"github.com/cevaris/ordered_map"
"github.com/sirupsen/logrus"
"regexp"
"testing"
)
func TestOracleSqlInsert(t *testing.T) {
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
log.Info("Starting tests for SQL INSERT...")
omKeys := ordered_map.NewOrderedMap()
omKeys.Set("col1", "a")
omKeys.Set("col2", "b")
omCols := ordered_map.NewOrderedMap()
omCols.Set("col3", "c")
db, _ := NewMockConnectionWithMockTx(log, "oracle")
dml := db.GetDmlGenerator()
o := dml.NewInsertGenerator(&SqlStatementGeneratorConfig{
Log: log,
OutputSchema: "",
SchemaSeparator: ".",
OutputTable: "t2",
TargetKeyCols: omKeys,
TargetOtherCols: omCols}).(SqlStmtTxtBatcher)
var batchIsFull bool
var err error
// Create new batch of values size 2.
o.InitBatch(2) // create a new batch with room for 2 rows...
batchIsFull, err = o.AddValuesToBatch([]interface{}{"x", "y", 123}) // first row should succeed.
if err != nil {
t.Fatal(err) // this should not fail here.
}
batchIsFull, err = o.AddValuesToBatch([]interface{}{"p", "q", 2}) // second row should succeed.
if err != nil {
t.Fatal(err) // this should not fail here.
}
if !batchIsFull {
t.Fatal("The batch *should* be full but it is not.")
} else {
log.Debug("Expected, no more room in batch.")
}
// Retry with smaller batch size.
o.InitBatch(1)
batchIsFull, err = o.AddValuesToBatch([]interface{}{"a", "b", 456, 789}) // this should fail as num values does not match len(omKeys) + len(omCols).
if err == nil {
t.Fatal("There should have been an error. Incorrect number of values deliberately supplied in batch.")
}
// Retry with smaller batch size.
o.InitBatch(1)
batchIsFull, err = o.AddValuesToBatch([]interface{}{"a", "b", 456}) // first row should succeed - 3 args checked below.
if err != nil {
t.Fatal(err) // this should not fail here.
}
if !batchIsFull {
t.Fatal("The batch *should* be full but it is not.")
} else {
log.Debug("Expected, no more room in batch.")
}
log.Debug("SQL with bind: ", o.GetStatement())
log.Debug("SQL args/values: ", o.GetValues())
if len(o.GetValues()) != 3 {
t.Fatal("Error, incorrect number of args.")
}
sql := `insert into t2 (a,b,c) values ( :1,:2,:3 )`
re := regexp.MustCompile("[\t\r\n\f]")
got := re.ReplaceAllString(o.GetStatement(), " ")
expected := re.ReplaceAllString(sql, " ")
log.Debug("expected = '", expected, "'")
log.Debug("got = '", got, "'")
if got != expected {
t.Fatal("Bad SQL INSERT generated.")
}
// Test two rows.
log.Info("SQL INSERT test 3, assert multiple rows in a batch gives good SQL")
o.InitBatch(2)
batchIsFull, err = o.AddValuesToBatch([]interface{}{"a", "b", 456})
if err != nil {
t.Fatal(err) // this should not fail here.
}
batchIsFull, err = o.AddValuesToBatch([]interface{}{"c", "d", 789})
if err != nil {
t.Fatal(err) // this should not fail here.
}
log.Debug("SQL with bind: ", o.GetStatement())
log.Debug("SQL args/values: ", o.GetValues())
sql = `insert into t2 (a,b,c) values ( :1,:2,:3 ),( :4,:5,:6 )`
re = regexp.MustCompile("[\t\r\n\f]")
got = re.ReplaceAllString(o.GetStatement(), " ")
expected = re.ReplaceAllString(sql, " ")
log.Debug("expected = '", expected, "'")
log.Debug("got = '", got, "'")
if expected != got {
t.Fatalf("Bad SQL INSERT generated: expected = '%v'; got = '%v'", expected, got)
}
log.Info("SQL INSERT test 3 complete")
log.Info("Testing SQL INSERT success")
}