Skip to content

Commit 6ef422f

Browse files
committed
Unicode tests.
1 parent ff0cb6f commit 6ef422f

File tree

4 files changed

+91
-46
lines changed

4 files changed

+91
-46
lines changed

driver/driver.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func (sqlite) Open(name string) (_ driver.Conn, err error) {
5252
if err != nil {
5353
return nil, err
5454
}
55+
defer func() {
56+
if err != nil {
57+
c.Close()
58+
}
59+
}()
5560

5661
var pragmas bool
5762
c.txBegin = "BEGIN"
@@ -65,17 +70,15 @@ func (sqlite) Open(name string) (_ driver.Conn, err error) {
6570
case "deferred", "immediate", "exclusive":
6671
c.txBegin = "BEGIN " + s
6772
default:
68-
c.Close()
6973
return nil, fmt.Errorf("sqlite3: invalid _txlock: %s", s)
7074
}
7175

7276
pragmas = len(query["_pragma"]) > 0
7377
}
7478
}
7579
if !pragmas {
76-
err := c.Conn.Exec(`PRAGMA busy_timeout=60000`)
80+
err = c.Conn.Exec(`PRAGMA busy_timeout=60000`)
7781
if err != nil {
78-
c.Close()
7982
return nil, err
8083
}
8184
c.reusable = true
@@ -86,7 +89,6 @@ func (sqlite) Open(name string) (_ driver.Conn, err error) {
8689
PRAGMA_query_only;
8790
`)
8891
if err != nil {
89-
c.Close()
9092
return nil, err
9193
}
9294
if s.Step() {
@@ -95,7 +97,6 @@ func (sqlite) Open(name string) (_ driver.Conn, err error) {
9597
}
9698
err = s.Close()
9799
if err != nil {
98-
c.Close()
99100
return nil, err
100101
}
101102
}

ext/unicode/unicode_test.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package unicode
22

33
import (
44
"errors"
5+
"reflect"
56
"testing"
67

78
"github.com/ncruces/go-sqlite3"
@@ -62,6 +63,67 @@ func TestRegister(t *testing.T) {
6263
}
6364
})
6465
}
66+
67+
err = db.Close()
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
}
72+
73+
func TestRegister_collation(t *testing.T) {
74+
t.Parallel()
75+
76+
db, err := sqlite3.Open(":memory:")
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
defer db.Close()
81+
82+
Register(db)
83+
84+
err = db.Exec(`CREATE TABLE IF NOT EXISTS words (word VARCHAR(10))`)
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
89+
err = db.Exec(`INSERT INTO words (word) VALUES ('côte'), ('cote'), ('coter'), ('coté'), ('cotée'), ('côté')`)
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
94+
err = db.Exec(`SELECT icu_load_collation('fr_FR', 'french')`)
95+
if err != nil {
96+
t.Fatal(err)
97+
}
98+
99+
stmt, _, err := db.Prepare(`SELECT word FROM words ORDER BY word COLLATE french`)
100+
if err != nil {
101+
t.Fatal(err)
102+
}
103+
defer stmt.Close()
104+
105+
got, want := []string{}, []string{"cote", "coté", "côte", "côté", "cotée", "coter"}
106+
107+
for stmt.Step() {
108+
got = append(got, stmt.ColumnText(0))
109+
}
110+
if err := stmt.Err(); err != nil {
111+
t.Fatal(err)
112+
}
113+
114+
if !reflect.DeepEqual(got, want) {
115+
t.Error("not equal")
116+
}
117+
118+
err = stmt.Close()
119+
if err != nil {
120+
t.Fatal(err)
121+
}
122+
123+
err = db.Close()
124+
if err != nil {
125+
t.Fatal(err)
126+
}
65127
}
66128

67129
func TestRegister_error(t *testing.T) {
@@ -99,13 +161,31 @@ func TestRegister_error(t *testing.T) {
99161
t.Errorf("got %v, want sqlite3.ERROR", err)
100162
}
101163

102-
err = db.Exec(`SELECT 'hello' LIKE 'HELLO' ESCAPE '\\' `)
164+
err = db.Exec(`SELECT 'hello' LIKE 'HELLO' ESCAPE '\\'`)
165+
if err == nil {
166+
t.Error("want error")
167+
}
168+
if !errors.Is(err, sqlite3.ERROR) {
169+
t.Errorf("got %v, want sqlite3.ERROR", err)
170+
}
171+
172+
err = db.Exec(`SELECT icu_load_collation('enUS', 'error')`)
103173
if err == nil {
104174
t.Error("want error")
105175
}
106176
if !errors.Is(err, sqlite3.ERROR) {
107177
t.Errorf("got %v, want sqlite3.ERROR", err)
108178
}
179+
180+
err = db.Exec(`SELECT icu_load_collation('enUS', '')`)
181+
if err != nil {
182+
t.Error(err)
183+
}
184+
185+
err = db.Close()
186+
if err != nil {
187+
t.Fatal(err)
188+
}
109189
}
110190

111191
func Test_like2regex(t *testing.T) {

func_test.go

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func ExampleConn_CreateCollation() {
1818
if err != nil {
1919
log.Fatal(err)
2020
}
21+
defer db.Close()
2122

2223
err = db.Exec(`CREATE TABLE IF NOT EXISTS words (word VARCHAR(10))`)
2324
if err != nil {
@@ -46,16 +47,6 @@ func ExampleConn_CreateCollation() {
4647
if err := stmt.Err(); err != nil {
4748
log.Fatal(err)
4849
}
49-
50-
err = stmt.Close()
51-
if err != nil {
52-
log.Fatal(err)
53-
}
54-
55-
err = db.Close()
56-
if err != nil {
57-
log.Fatal(err)
58-
}
5950
// Output:
6051
// cote
6152
// coté
@@ -70,6 +61,7 @@ func ExampleConn_CreateFunction() {
7061
if err != nil {
7162
log.Fatal(err)
7263
}
64+
defer db.Close()
7365

7466
err = db.Exec(`CREATE TABLE IF NOT EXISTS words (word VARCHAR(10))`)
7567
if err != nil {
@@ -100,16 +92,6 @@ func ExampleConn_CreateFunction() {
10092
if err := stmt.Err(); err != nil {
10193
log.Fatal(err)
10294
}
103-
104-
err = stmt.Close()
105-
if err != nil {
106-
log.Fatal(err)
107-
}
108-
109-
err = db.Close()
110-
if err != nil {
111-
log.Fatal(err)
112-
}
11395
// Unordered output:
11496
// COTE
11597
// COTÉ
@@ -124,6 +106,7 @@ func ExampleContext_SetAuxData() {
124106
if err != nil {
125107
log.Fatal(err)
126108
}
109+
defer db.Close()
127110

128111
err = db.Exec(`CREATE TABLE IF NOT EXISTS words (word VARCHAR(10))`)
129112
if err != nil {
@@ -164,16 +147,6 @@ func ExampleContext_SetAuxData() {
164147
if err := stmt.Err(); err != nil {
165148
log.Fatal(err)
166149
}
167-
168-
err = stmt.Close()
169-
if err != nil {
170-
log.Fatal(err)
171-
}
172-
173-
err = db.Close()
174-
if err != nil {
175-
log.Fatal(err)
176-
}
177150
// Unordered output:
178151
// cote
179152
// côte

func_win_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func ExampleConn_CreateWindowFunction() {
1414
if err != nil {
1515
log.Fatal(err)
1616
}
17+
defer db.Close()
1718

1819
err = db.Exec(`CREATE TABLE IF NOT EXISTS words (word VARCHAR(10))`)
1920
if err != nil {
@@ -42,16 +43,6 @@ func ExampleConn_CreateWindowFunction() {
4243
if err := stmt.Err(); err != nil {
4344
log.Fatal(err)
4445
}
45-
46-
err = stmt.Close()
47-
if err != nil {
48-
log.Fatal(err)
49-
}
50-
51-
err = db.Close()
52-
if err != nil {
53-
log.Fatal(err)
54-
}
5546
// Output:
5647
// 1
5748
// 2

0 commit comments

Comments
 (0)