-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloop_test.go
More file actions
85 lines (68 loc) · 1.46 KB
/
loop_test.go
File metadata and controls
85 lines (68 loc) · 1.46 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
package sqlbless
import (
"encoding/csv"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"github.com/hymkor/sqlbless/dialect"
)
func TestSavePoint(t *testing.T) {
restoreColor := disableColor()
defer restoreColor()
tmpDir := t.TempDir()
testLst := filepath.Join(tmpDir, "output.lst")
scriptPath := filepath.Join(tmpDir, "script.sql")
w, err := os.Create(scriptPath)
if err != nil {
t.Fatal(err.Error())
}
fmt.Fprintf(w, `
CREATE TABLE TESTTBL
( SERIAL NUMERIC,
STR CHAR VARYING(20),
DT CHARACTER VARYING(20),
PRIMARY KEY (SERIAL) );
INSERT INTO TESTTBL VALUES
(10,'HOGE','2024-05-25 13:45:33');
SAVEPOINT SP1;
INSERT INTO TESTTBL VALUES
(20,'HOGE','2024-05-25 13:45:33');
ROLLBACK TO SP1;
SPOOL %s;
SELECT "CNT=",COUNT(*) FROM TESTTBL WHERE STR = 'HOGE';
SPOOL OFF;
ROLLBACK;
EXIT;
`, testLst)
w.Close()
cfg := New()
d, err := dialect.ReadDBInfoFromArgs([]string{"sqlite3", ":memory:"})
if err != nil {
t.Fatal(err.Error())
}
cfg.Script = scriptPath
err = cfg.Run(d.Driver, d.DataSource, d.Dialect)
if err != nil {
t.Fatal(err.Error())
}
fd, err := os.Open(testLst)
if err != nil {
t.Fatal(err.Error())
}
defer fd.Close()
csvr := csv.NewReader(fd)
csvr.Comment = '#'
csvr.FieldsPerRecord = -1
for {
record, err := csvr.Read()
if err == io.EOF {
t.Fatal("target record not found")
}
//println(strings.Join(record, "|"))
if record[0] == "CNT=" && record[1] == "1" {
return
}
}
}