-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
82 lines (76 loc) · 1.66 KB
/
main_test.go
File metadata and controls
82 lines (76 loc) · 1.66 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
package sqlbless
import (
"bufio"
"os"
"path/filepath"
"strings"
"testing"
"github.com/hymkor/sqlbless/dialect"
_ "github.com/hymkor/sqlbless/dialect/sqlite"
)
func disableColor() (restore func()) {
if noColor, ok := os.LookupEnv("NO_COLOR"); ok {
restore = func() { os.Setenv("NO_COLOR", noColor) }
} else {
restore = func() { os.Unsetenv("NO_COLOR") }
}
os.Setenv("NO_COLOR", "1")
return
}
func TestConfigRun(t *testing.T) {
restoreColor := disableColor()
defer restoreColor()
testLst := filepath.Join(t.TempDir(), "output.lst")
auto :=
"CREATE TABLE TESTTBL|" +
"( TESTNO NUMERIC ,|" +
" DT CHARACTER VARYING(20) ,|" +
" PRIMARY KEY (TESTNO) )||" +
"INSERT INTO TESTTBL VALUES|" +
"(10,'2024-05-25 13:45:33')||" +
"COMMIT||" +
"EDIT TESTTBL||" +
"/10|lr2015-06-07 20:21:22|qyy" +
"SPOOL " + testLst + "||" +
"SELECT * FROM TESTTBL||" +
"SPOOL OFF||" +
"ROLLBACK||" +
"EXIT||"
cfg := New()
cfg.Auto = auto
cfg.Debug = true
d, err := dialect.ReadDBInfoFromArgs([]string{"sqlite3", ":memory:"})
if err != nil {
t.Fatal(err.Error())
}
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()
sc := bufio.NewScanner(fd)
count := 0
for sc.Scan() {
line := sc.Text()
if len(line) > 0 && line[0] == '#' {
continue
}
count++
if count == 1 {
continue
}
field := strings.Split(line, ",")
if len(field) >= 2 && field[1] == "2015-06-07 20:21:22" {
// OK
return
}
}
if sc.Err() != nil {
t.Fatal(err.Error())
}
t.Fatalf("%s: not found", "2015-06-07 20:21:22")
}