Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gosqlite
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gosqlite
Commits
a8febe56
Commit
a8febe56
authored
Apr 20, 2013
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improves tests coverage.
parent
10edd7e3
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
104 additions
and
43 deletions
+104
-43
date_test.go
date_test.go
+1
-1
driver_test.go
driver_test.go
+13
-7
example_test.go
example_test.go
+9
-8
function_test.go
function_test.go
+7
-7
sqlite_test.go
sqlite_test.go
+13
-5
stmt_test.go
stmt_test.go
+59
-12
vtab_test.go
vtab_test.go
+2
-3
No files found.
date_test.go
View file @
a8febe56
...
@@ -22,7 +22,7 @@ func TestJulianDay(t *testing.T) {
...
@@ -22,7 +22,7 @@ func TestJulianDay(t *testing.T) {
}
}
}
}
func
TestBind
(
t
*
testing
.
T
)
{
func
TestBind
Time
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
var
delta
int
var
delta
int
...
...
driver_test.go
View file @
a8febe56
...
@@ -13,12 +13,12 @@ const (
...
@@ -13,12 +13,12 @@ const (
ddl
=
"DROP TABLE IF EXISTS test;"
+
ddl
=
"DROP TABLE IF EXISTS test;"
+
"CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL,"
+
"CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL,"
+
" name TEXT);"
" name TEXT);"
dml
=
"INSERT INTO test (name)
values
('Bart');"
+
dml
=
"INSERT INTO test (name)
VALUES
('Bart');"
+
"INSERT INTO test (name)
values
('Lisa');"
+
"INSERT INTO test (name)
VALUES
('Lisa');"
+
"UPDATE test
set name = 'El Barto' where
name = 'Bart';"
+
"UPDATE test
SET name = 'El Barto' WHERE
name = 'Bart';"
+
"DELETE
from test where
name = 'Bart';"
"DELETE
FROM test WHERE
name = 'Bart';"
insert
=
"INSERT
into test (name) values
(?)"
insert
=
"INSERT
INTO test (name) VALUES
(?)"
query
=
"SELECT * FROM test
where name like
?"
query
=
"SELECT * FROM test
WHERE name LIKE
?"
)
)
func
sqlOpen
(
t
*
testing
.
T
)
*
sql
.
DB
{
func
sqlOpen
(
t
*
testing
.
T
)
*
sql
.
DB
{
...
@@ -133,8 +133,14 @@ func TestSqlPrepare(t *testing.T) {
...
@@ -133,8 +133,14 @@ func TestSqlPrepare(t *testing.T) {
stmt
,
err
:=
db
.
Prepare
(
insert
)
stmt
,
err
:=
db
.
Prepare
(
insert
)
checkNoError
(
t
,
err
,
"Error while preparing stmt: %s"
)
checkNoError
(
t
,
err
,
"Error while preparing stmt: %s"
)
defer
checkSqlStmtClose
(
stmt
,
t
)
defer
checkSqlStmtClose
(
stmt
,
t
)
_
,
err
=
stmt
.
Exec
(
"Bart"
)
result
,
err
:
=
stmt
.
Exec
(
"Bart"
)
checkNoError
(
t
,
err
,
"Error while executing stmt: %s"
)
checkNoError
(
t
,
err
,
"Error while executing stmt: %s"
)
id
,
err
:=
result
.
LastInsertId
()
checkNoError
(
t
,
err
,
"Error while calling LastInsertId: %s"
)
assertEquals
(
t
,
"expected %d got %d LastInsertId"
,
int64
(
3
),
id
)
changes
,
err
:=
result
.
RowsAffected
()
checkNoError
(
t
,
err
,
"Error while calling RowsAffected: %s"
)
assertEquals
(
t
,
"expected %d got %d RowsAffected"
,
int64
(
1
),
changes
)
}
}
func
TestRowsWithStmtClosed
(
t
*
testing
.
T
)
{
func
TestRowsWithStmtClosed
(
t
*
testing
.
T
)
{
...
...
example_test.go
View file @
a8febe56
...
@@ -7,6 +7,7 @@ package sqlite_test
...
@@ -7,6 +7,7 @@ package sqlite_test
import
(
import
(
"fmt"
"fmt"
"github.com/gwenn/gosqlite"
"github.com/gwenn/gosqlite"
"sync"
)
)
func
check
(
err
error
)
{
func
check
(
err
error
)
{
...
@@ -26,7 +27,7 @@ func Example() {
...
@@ -26,7 +27,7 @@ func Example() {
defer
ins
.
Finalize
()
defer
ins
.
Finalize
()
_
,
err
=
ins
.
Insert
(
"gosqlite driver"
)
_
,
err
=
ins
.
Insert
(
"gosqlite driver"
)
check
(
err
)
check
(
err
)
s
,
err
:=
db
.
Prepare
(
"SELECT name
from test WHERE name like
?"
,
"%go%"
)
s
,
err
:=
db
.
Prepare
(
"SELECT name
FROM test WHERE name LIKE
?"
,
"%go%"
)
check
(
err
)
check
(
err
)
defer
s
.
Finalize
()
defer
s
.
Finalize
()
var
name
string
var
name
string
...
@@ -68,7 +69,7 @@ func ExampleStmt_ExecDml() {
...
@@ -68,7 +69,7 @@ func ExampleStmt_ExecDml() {
err
=
db
.
Exec
(
"CREATE TABLE test (content TEXT); INSERT INTO test VALUES ('Go'); INSERT INTO test VALUES ('SQLite')"
)
err
=
db
.
Exec
(
"CREATE TABLE test (content TEXT); INSERT INTO test VALUES ('Go'); INSERT INTO test VALUES ('SQLite')"
)
check
(
err
)
check
(
err
)
s
,
err
:=
db
.
Prepare
(
"UPDATE test SET content = content || 'lang'
where content like
?"
)
s
,
err
:=
db
.
Prepare
(
"UPDATE test SET content = content || 'lang'
WHERE content LIKE
?"
)
check
(
err
)
check
(
err
)
defer
s
.
Finalize
()
defer
s
.
Finalize
()
changes
,
err
:=
s
.
ExecDml
(
"%o"
)
changes
,
err
:=
s
.
ExecDml
(
"%o"
)
...
@@ -103,7 +104,7 @@ func ExampleStmt_NamedScan() {
...
@@ -103,7 +104,7 @@ func ExampleStmt_NamedScan() {
check
(
err
)
check
(
err
)
defer
db
.
Close
()
defer
db
.
Close
()
s
,
err
:=
db
.
Prepare
(
"SELECT 1
as id, 'Go' as
name UNION SELECT 2, 'SQLite'"
)
s
,
err
:=
db
.
Prepare
(
"SELECT 1
AS id, 'Go' AS
name UNION SELECT 2, 'SQLite'"
)
check
(
err
)
check
(
err
)
defer
s
.
Finalize
()
defer
s
.
Finalize
()
...
@@ -125,7 +126,7 @@ func ExampleStmt_Scan() {
...
@@ -125,7 +126,7 @@ func ExampleStmt_Scan() {
check
(
err
)
check
(
err
)
defer
db
.
Close
()
defer
db
.
Close
()
s
,
err
:=
db
.
Prepare
(
"SELECT 1
as id, 'Go' as name, 'Y' as
status UNION SELECT 2, 'SQLite', 'yes'"
)
s
,
err
:=
db
.
Prepare
(
"SELECT 1
AS id, 'Go' AS name, 'Y' AS
status UNION SELECT 2, 'SQLite', 'yes'"
)
check
(
err
)
check
(
err
)
defer
s
.
Finalize
()
defer
s
.
Finalize
()
...
@@ -165,13 +166,13 @@ func ExampleNewBackup() {
...
@@ -165,13 +166,13 @@ func ExampleNewBackup() {
cbs
:=
make
(
chan
sqlite
.
BackupStatus
)
cbs
:=
make
(
chan
sqlite
.
BackupStatus
)
defer
close
(
cbs
)
defer
close
(
cbs
)
ack
:=
make
(
chan
bool
)
var
wg
sync
.
WaitGroup
defer
close
(
ack
)
wg
.
Add
(
1
)
go
func
()
{
go
func
()
{
for
{
for
{
s
:=
<-
cbs
s
:=
<-
cbs
fmt
.
Printf
(
"backup progress (remaining: %d)
\n
"
,
s
.
Remaining
)
fmt
.
Printf
(
"backup progress (remaining: %d)
\n
"
,
s
.
Remaining
)
ack
<-
true
wg
.
Done
()
if
s
.
Remaining
==
0
{
if
s
.
Remaining
==
0
{
break
break
}
}
...
@@ -179,7 +180,7 @@ func ExampleNewBackup() {
...
@@ -179,7 +180,7 @@ func ExampleNewBackup() {
}()
}()
err
=
bck
.
Run
(
100
,
250000
,
cbs
)
err
=
bck
.
Run
(
100
,
250000
,
cbs
)
check
(
err
)
check
(
err
)
<-
ack
wg
.
Wait
()
// Output: backup progress (remaining: 0)
// Output: backup progress (remaining: 0)
}
}
...
...
function_test.go
View file @
a8febe56
...
@@ -27,7 +27,7 @@ func TestScalarFunction(t *testing.T) {
...
@@ -27,7 +27,7 @@ func TestScalarFunction(t *testing.T) {
err
:=
db
.
CreateScalarFunction
(
"half"
,
1
,
nil
,
half
,
nil
)
err
:=
db
.
CreateScalarFunction
(
"half"
,
1
,
nil
,
half
,
nil
)
checkNoError
(
t
,
err
,
"couldn't create function: %s"
)
checkNoError
(
t
,
err
,
"couldn't create function: %s"
)
var
d
float64
var
d
float64
err
=
db
.
OneValue
(
"
select
half(6)"
,
&
d
)
err
=
db
.
OneValue
(
"
SELECT
half(6)"
,
&
d
)
checkNoError
(
t
,
err
,
"couldn't retrieve result: %s"
)
checkNoError
(
t
,
err
,
"couldn't retrieve result: %s"
)
assertEquals
(
t
,
"Expected %f but got %f"
,
3.0
,
d
)
assertEquals
(
t
,
"Expected %f but got %f"
,
3.0
,
d
)
err
=
db
.
CreateScalarFunction
(
"half"
,
1
,
nil
,
nil
,
nil
)
err
=
db
.
CreateScalarFunction
(
"half"
,
1
,
nil
,
nil
,
nil
)
...
@@ -73,7 +73,7 @@ func TestRegexpFunction(t *testing.T) {
...
@@ -73,7 +73,7 @@ func TestRegexpFunction(t *testing.T) {
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
err
:=
db
.
CreateScalarFunction
(
"regexp"
,
2
,
nil
,
re
,
reDestroy
)
err
:=
db
.
CreateScalarFunction
(
"regexp"
,
2
,
nil
,
re
,
reDestroy
)
checkNoError
(
t
,
err
,
"couldn't create function: %s"
)
checkNoError
(
t
,
err
,
"couldn't create function: %s"
)
s
,
err
:=
db
.
Prepare
(
"
select regexp('l.s[aeiouy]', name) from (select 'lisa' as name union all select
'bart')"
)
s
,
err
:=
db
.
Prepare
(
"
SELECT regexp('l.s[aeiouy]', name) from (SELECT 'lisa' AS name UNION ALL SELECT
'bart')"
)
checkNoError
(
t
,
err
,
"couldn't prepare statement: %s"
)
checkNoError
(
t
,
err
,
"couldn't prepare statement: %s"
)
defer
checkFinalize
(
s
,
t
)
defer
checkFinalize
(
s
,
t
)
...
@@ -108,7 +108,7 @@ func TestUserFunction(t *testing.T) {
...
@@ -108,7 +108,7 @@ func TestUserFunction(t *testing.T) {
err
:=
db
.
CreateScalarFunction
(
"user"
,
0
,
nil
,
user
,
nil
)
err
:=
db
.
CreateScalarFunction
(
"user"
,
0
,
nil
,
user
,
nil
)
checkNoError
(
t
,
err
,
"couldn't create function: %s"
)
checkNoError
(
t
,
err
,
"couldn't create function: %s"
)
var
name
string
var
name
string
err
=
db
.
OneValue
(
"
select
user()"
,
&
name
)
err
=
db
.
OneValue
(
"
SELECT
user()"
,
&
name
)
checkNoError
(
t
,
err
,
"couldn't retrieve result: %s"
)
checkNoError
(
t
,
err
,
"couldn't retrieve result: %s"
)
assert
(
t
,
"unexpected user name: %q"
,
len
(
name
)
>
0
)
assert
(
t
,
"unexpected user name: %q"
,
len
(
name
)
>
0
)
err
=
db
.
CreateScalarFunction
(
"user"
,
1
,
nil
,
nil
,
nil
)
err
=
db
.
CreateScalarFunction
(
"user"
,
1
,
nil
,
nil
,
nil
)
...
@@ -142,7 +142,7 @@ func TestSumFunction(t *testing.T) {
...
@@ -142,7 +142,7 @@ func TestSumFunction(t *testing.T) {
err
:=
db
.
CreateAggregateFunction
(
"mysum"
,
1
,
nil
,
sumStep
,
sumFinal
,
nil
)
err
:=
db
.
CreateAggregateFunction
(
"mysum"
,
1
,
nil
,
sumStep
,
sumFinal
,
nil
)
checkNoError
(
t
,
err
,
"couldn't create function: %s"
)
checkNoError
(
t
,
err
,
"couldn't create function: %s"
)
var
i
int
var
i
int
err
=
db
.
OneValue
(
"
select mysum(i) from (select 2 as i union all select
2)"
,
&
i
)
err
=
db
.
OneValue
(
"
SELECT mysum(i) FROM (SELECT 2 AS i UNION ALL SELECT
2)"
,
&
i
)
checkNoError
(
t
,
err
,
"couldn't execute statement: %s"
)
checkNoError
(
t
,
err
,
"couldn't execute statement: %s"
)
assertEquals
(
t
,
"expected %d but got %v"
,
4
,
i
)
assertEquals
(
t
,
"expected %d but got %v"
,
4
,
i
)
...
@@ -170,7 +170,7 @@ func BenchmarkLike(b *testing.B) {
...
@@ -170,7 +170,7 @@ func BenchmarkLike(b *testing.B) {
db
,
_
:=
Open
(
":memory:"
)
db
,
_
:=
Open
(
":memory:"
)
defer
db
.
Close
()
defer
db
.
Close
()
randomFill
(
db
,
1
)
randomFill
(
db
,
1
)
cs
,
_
:=
db
.
Prepare
(
"SELECT count(1) FROM test
where name like
'lisa'"
)
cs
,
_
:=
db
.
Prepare
(
"SELECT count(1) FROM test
WHERE name LIKE
'lisa'"
)
defer
cs
.
Finalize
()
defer
cs
.
Finalize
()
b
.
StartTimer
()
b
.
StartTimer
()
...
@@ -186,7 +186,7 @@ func BenchmarkHalf(b *testing.B) {
...
@@ -186,7 +186,7 @@ func BenchmarkHalf(b *testing.B) {
defer
db
.
Close
()
defer
db
.
Close
()
randomFill
(
db
,
1
)
randomFill
(
db
,
1
)
db
.
CreateScalarFunction
(
"half"
,
1
,
nil
,
half
,
nil
)
db
.
CreateScalarFunction
(
"half"
,
1
,
nil
,
half
,
nil
)
cs
,
_
:=
db
.
Prepare
(
"SELECT count(1) FROM test
where
half(rank) > 20"
)
cs
,
_
:=
db
.
Prepare
(
"SELECT count(1) FROM test
WHERE
half(rank) > 20"
)
defer
cs
.
Finalize
()
defer
cs
.
Finalize
()
b
.
StartTimer
()
b
.
StartTimer
()
...
@@ -202,7 +202,7 @@ func BenchmarkRegexp(b *testing.B) {
...
@@ -202,7 +202,7 @@ func BenchmarkRegexp(b *testing.B) {
defer
db
.
Close
()
defer
db
.
Close
()
randomFill
(
db
,
1
)
randomFill
(
db
,
1
)
db
.
CreateScalarFunction
(
"regexp"
,
2
,
nil
,
re
,
reDestroy
)
db
.
CreateScalarFunction
(
"regexp"
,
2
,
nil
,
re
,
reDestroy
)
cs
,
_
:=
db
.
Prepare
(
"SELECT count(1) FROM test
where
name regexp '(?i)
\\
blisa
\\
b'"
)
cs
,
_
:=
db
.
Prepare
(
"SELECT count(1) FROM test
WHERE
name regexp '(?i)
\\
blisa
\\
b'"
)
defer
cs
.
Finalize
()
defer
cs
.
Finalize
()
b
.
StartTimer
()
b
.
StartTimer
()
...
...
sqlite_test.go
View file @
a8febe56
...
@@ -119,9 +119,9 @@ func TestSavepoint(t *testing.T) {
...
@@ -119,9 +119,9 @@ func TestSavepoint(t *testing.T) {
func
TestExists
(
t
*
testing
.
T
)
{
func
TestExists
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
b
:=
Must
(
db
.
Exists
(
"SELECT 1
where
1 = 0"
))
b
:=
Must
(
db
.
Exists
(
"SELECT 1
WHERE
1 = 0"
))
assert
(
t
,
"No row expected"
,
!
b
)
assert
(
t
,
"No row expected"
,
!
b
)
b
=
Must
(
db
.
Exists
(
"SELECT 1
where
1 = 1"
))
b
=
Must
(
db
.
Exists
(
"SELECT 1
WHERE
1 = 1"
))
assert
(
t
,
"One row expected"
,
b
)
assert
(
t
,
"One row expected"
,
b
)
}
}
...
@@ -183,7 +183,7 @@ func TestOpenSameMemoryDb(t *testing.T) {
...
@@ -183,7 +183,7 @@ func TestOpenSameMemoryDb(t *testing.T) {
db2
,
err
:=
Open
(
"file:dummy.db?mode=memory&cache=shared"
,
OpenUri
,
OpenReadWrite
,
OpenCreate
,
OpenFullMutex
)
db2
,
err
:=
Open
(
"file:dummy.db?mode=memory&cache=shared"
,
OpenUri
,
OpenReadWrite
,
OpenCreate
,
OpenFullMutex
)
checkNoError
(
t
,
err
,
"open error: %s"
)
checkNoError
(
t
,
err
,
"open error: %s"
)
defer
checkClose
(
db2
,
t
)
defer
checkClose
(
db2
,
t
)
_
,
err
=
db2
.
Exists
(
"SELECT 1
from
test"
)
_
,
err
=
db2
.
Exists
(
"SELECT 1
FROM
test"
)
checkNoError
(
t
,
err
,
"exists error: %s"
)
checkNoError
(
t
,
err
,
"exists error: %s"
)
}
}
...
@@ -191,7 +191,7 @@ func TestConnExecWithSelect(t *testing.T) {
...
@@ -191,7 +191,7 @@ func TestConnExecWithSelect(t *testing.T) {
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
err
:=
db
.
Exec
(
"
select
1"
)
err
:=
db
.
Exec
(
"
SELECT
1"
)
assert
(
t
,
"error expected"
,
err
!=
nil
)
assert
(
t
,
"error expected"
,
err
!=
nil
)
if
serr
,
ok
:=
err
.
(
*
StmtError
);
ok
{
if
serr
,
ok
:=
err
.
(
*
StmtError
);
ok
{
assertEquals
(
t
,
"expected %q but got %q"
,
Row
,
serr
.
Code
())
assertEquals
(
t
,
"expected %q but got %q"
,
Row
,
serr
.
Code
())
...
@@ -232,7 +232,15 @@ func TestConnSettings(t *testing.T) {
...
@@ -232,7 +232,15 @@ func TestConnSettings(t *testing.T) {
}
}
func
TestComplete
(
t
*
testing
.
T
)
{
func
TestComplete
(
t
*
testing
.
T
)
{
assert
(
t
,
"expected complete statement"
,
Complete
(
"select 1;"
))
assert
(
t
,
"expected complete statement"
,
Complete
(
"SELECT 1;"
))
}
func
TestExecMisuse
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
createTable
(
db
,
t
)
err
:=
db
.
Exec
(
"INSERT INTO test VALUES (?, ?, ?, ?); INSERT INTO test VALUES (?, ?, ?, ?)"
,
0
,
273.1
,
1
,
"test"
)
assert
(
t
,
"exec misuse expected"
,
err
!=
nil
)
}
}
func
assertEquals
(
t
*
testing
.
T
,
format
string
,
expected
,
actual
interface
{})
{
func
assertEquals
(
t
*
testing
.
T
,
format
string
,
expected
,
actual
interface
{})
{
...
...
stmt_test.go
View file @
a8febe56
...
@@ -50,7 +50,7 @@ func TestInsertWithStatement(t *testing.T) {
...
@@ -50,7 +50,7 @@ func TestInsertWithStatement(t *testing.T) {
cs
,
_
:=
db
.
Prepare
(
"SELECT COUNT(*) FROM test"
)
cs
,
_
:=
db
.
Prepare
(
"SELECT COUNT(*) FROM test"
)
defer
checkFinalize
(
cs
,
t
)
defer
checkFinalize
(
cs
,
t
)
assert
(
t
,
"
select
statement should be readonly"
,
cs
.
ReadOnly
())
assert
(
t
,
"
SELECT
statement should be readonly"
,
cs
.
ReadOnly
())
if
!
Must
(
cs
.
Next
())
{
if
!
Must
(
cs
.
Next
())
{
t
.
Fatal
(
"no result for count"
)
t
.
Fatal
(
"no result for count"
)
}
}
...
@@ -58,7 +58,7 @@ func TestInsertWithStatement(t *testing.T) {
...
@@ -58,7 +58,7 @@ func TestInsertWithStatement(t *testing.T) {
checkNoError
(
t
,
cs
.
Scan
(
&
i
),
"error scanning count: %s"
)
checkNoError
(
t
,
cs
.
Scan
(
&
i
),
"error scanning count: %s"
)
assertEquals
(
t
,
"count should be %d, but it is %d"
,
1000
,
i
)
assertEquals
(
t
,
"count should be %d, but it is %d"
,
1000
,
i
)
rs
,
_
:=
db
.
Prepare
(
"SELECT float_num, int_num, a_string FROM test
where a_string like
? ORDER BY int_num LIMIT 2"
,
"hel%"
)
rs
,
_
:=
db
.
Prepare
(
"SELECT float_num, int_num, a_string FROM test
WHERE a_string LIKE
? ORDER BY int_num LIMIT 2"
,
"hel%"
)
defer
checkFinalize
(
rs
,
t
)
defer
checkFinalize
(
rs
,
t
)
columnCount
=
rs
.
ColumnCount
()
columnCount
=
rs
.
ColumnCount
()
assertEquals
(
t
,
"column count error: expected %d but got %d"
,
3
,
columnCount
)
assertEquals
(
t
,
"column count error: expected %d but got %d"
,
3
,
columnCount
)
...
@@ -92,7 +92,7 @@ func TestScanColumn(t *testing.T) {
...
@@ -92,7 +92,7 @@ func TestScanColumn(t *testing.T) {
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
s
,
err
:=
db
.
Prepare
(
"
select
1, null, 0"
)
s
,
err
:=
db
.
Prepare
(
"
SELECT
1, null, 0"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
defer
checkFinalize
(
s
,
t
)
defer
checkFinalize
(
s
,
t
)
if
!
Must
(
s
.
Next
())
{
if
!
Must
(
s
.
Next
())
{
...
@@ -114,7 +114,7 @@ func TestNamedScanColumn(t *testing.T) {
...
@@ -114,7 +114,7 @@ func TestNamedScanColumn(t *testing.T) {
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
s
,
err
:=
db
.
Prepare
(
"
select 1 as i1, null as i2, 0 as
i3"
)
s
,
err
:=
db
.
Prepare
(
"
SELECT 1 AS i1, null AS i2, 0 AS
i3"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
defer
checkFinalize
(
s
,
t
)
defer
checkFinalize
(
s
,
t
)
if
!
Must
(
s
.
Next
())
{
if
!
Must
(
s
.
Next
())
{
...
@@ -130,13 +130,16 @@ func TestNamedScanColumn(t *testing.T) {
...
@@ -130,13 +130,16 @@ func TestNamedScanColumn(t *testing.T) {
null
=
Must
(
s
.
ScanByName
(
"i3"
,
&
i3
))
null
=
Must
(
s
.
ScanByName
(
"i3"
,
&
i3
))
assert
(
t
,
"expected not null value"
,
!
null
)
assert
(
t
,
"expected not null value"
,
!
null
)
assertEquals
(
t
,
"expected %d but got %d"
,
0
,
i3
)
assertEquals
(
t
,
"expected %d but got %d"
,
0
,
i3
)
_
,
err
=
s
.
ScanByName
(
"invalid"
,
&
i1
)
assert
(
t
,
"expected invalid name"
,
err
!=
nil
)
}
}
func
TestScanCheck
(
t
*
testing
.
T
)
{
func
TestScanCheck
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
s
,
err
:=
db
.
Prepare
(
"
select
'hello'"
)
s
,
err
:=
db
.
Prepare
(
"
SELECT
'hello'"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
defer
checkFinalize
(
s
,
t
)
defer
checkFinalize
(
s
,
t
)
if
!
Must
(
s
.
Next
())
{
if
!
Must
(
s
.
Next
())
{
...
@@ -157,7 +160,7 @@ func TestScanNull(t *testing.T) {
...
@@ -157,7 +160,7 @@ func TestScanNull(t *testing.T) {
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
s
,
err
:=
db
.
Prepare
(
"
select
null"
)
s
,
err
:=
db
.
Prepare
(
"
SELECT
null"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
defer
checkFinalize
(
s
,
t
)
defer
checkFinalize
(
s
,
t
)
if
!
Must
(
s
.
Next
())
{
if
!
Must
(
s
.
Next
())
{
...
@@ -177,7 +180,7 @@ func TestScanNotNull(t *testing.T) {
...
@@ -177,7 +180,7 @@ func TestScanNotNull(t *testing.T) {
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
s
,
err
:=
db
.
Prepare
(
"
select
1"
)
s
,
err
:=
db
.
Prepare
(
"
SELECT
1"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
defer
checkFinalize
(
s
,
t
)
defer
checkFinalize
(
s
,
t
)
if
!
Must
(
s
.
Next
())
{
if
!
Must
(
s
.
Next
())
{
...
@@ -198,7 +201,7 @@ func TestScanError(t *testing.T) {
...
@@ -198,7 +201,7 @@ func TestScanError(t *testing.T) {
db := open(t)
db := open(t)
defer checkClose(db, t)
defer checkClose(db, t)
s, err := db.Prepare("
select
1")
s, err := db.Prepare("
SELECT
1")
checkNoError(t, err, "prepare error: %s")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t)
defer checkFinalize(s, t)
if !Must(s.Next()) {
if !Must(s.Next()) {
...
@@ -239,7 +242,7 @@ func TestStmtWithClosedDb(t *testing.T) {
...
@@ -239,7 +242,7 @@ func TestStmtWithClosedDb(t *testing.T) {
db
.
SetCacheSize
(
0
)
db
.
SetCacheSize
(
0
)
s
,
err
:=
db
.
Prepare
(
"
select
1"
)
s
,
err
:=
db
.
Prepare
(
"
SELECT
1"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
assertEquals
(
t
,
"expected Conn: %p, actual: %p"
,
db
,
s
.
Conn
())
assertEquals
(
t
,
"expected Conn: %p, actual: %p"
,
db
,
s
.
Conn
())
defer
s
.
Finalize
()
defer
s
.
Finalize
()
...
@@ -256,7 +259,7 @@ func TestStmtExecWithSelect(t *testing.T) {
...
@@ -256,7 +259,7 @@ func TestStmtExecWithSelect(t *testing.T) {
db
:=
open
(
t
)
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
defer
checkClose
(
db
,
t
)
s
,
err
:=
db
.
Prepare
(
"
select
1"
)
s
,
err
:=
db
.
Prepare
(
"
SELECT
1"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
defer
s
.
Finalize
()
defer
s
.
Finalize
()
...
@@ -280,7 +283,7 @@ func TestStmtSelectWithInsert(t *testing.T) {
...
@@ -280,7 +283,7 @@ func TestStmtSelectWithInsert(t *testing.T) {
defer
s
.
Finalize
()
defer
s
.
Finalize
()
exists
,
err
:=
s
.
SelectOneRow
()
exists
,
err
:=
s
.
SelectOneRow
()
checkNoError
(
t
,
err
,
"
select
error: %s"
)
checkNoError
(
t
,
err
,
"
SELECT
error: %s"
)
assert
(
t
,
"no row expected"
,
!
exists
)
assert
(
t
,
"no row expected"
,
!
exists
)
}
}
...
@@ -305,9 +308,16 @@ func TestNamedBind(t *testing.T) {
...
@@ -305,9 +308,16 @@ func TestNamedBind(t *testing.T) {
checkNoError
(
t
,
err
,
"named bind error: %s"
)
checkNoError
(
t
,
err
,
"named bind error: %s"
)
_
,
err
=
is
.
Next
()
_
,
err
=
is
.
Next
()
checkNoError
(
t
,
err
,
"named bind step error: %s"
)
checkNoError
(
t
,
err
,
"named bind step error: %s"
)
err
=
is
.
NamedBind
(
":b"
,
byt
,
":invalid"
,
nil
)
assert
(
t
,
"invalid param name expected"
,
err
!=
nil
)
err
=
is
.
NamedBind
(
":b"
)
assert
(
t
,
"missing params"
,
err
!=
nil
)
err
=
is
.
NamedBind
(
byt
,
":b"
)
assert
(
t
,
"invalid param name"
,
err
!=
nil
)
checkFinalize
(
is
,
t
)
checkFinalize
(
is
,
t
)
s
,
err
:=
db
.
Prepare
(
"SELECT data
as bs, byte as
b FROM test"
)
s
,
err
:=
db
.
Prepare
(
"SELECT data
AS bs, byte AS
b FROM test"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
defer
checkFinalize
(
s
,
t
)
defer
checkFinalize
(
s
,
t
)
if
!
Must
(
s
.
Next
())
{
if
!
Must
(
s
.
Next
())
{
...
@@ -319,4 +329,41 @@ func TestNamedBind(t *testing.T) {
...
@@ -319,4 +329,41 @@ func TestNamedBind(t *testing.T) {
checkNoError
(
t
,
err
,
"named scan error: %s"
)
checkNoError
(
t
,
err
,
"named scan error: %s"
)
assertEquals
(
t
,
"expected blob: %v, actual: %s"
,
len
(
blob
),
len
(
bs
))
assertEquals
(
t
,
"expected blob: %v, actual: %s"
,
len
(
blob
),
len
(
bs
))
assertEquals
(
t
,
"expected byte: %c, actual: %c"
,
byt
,
b
)
assertEquals
(
t
,
"expected byte: %c, actual: %c"
,
byt
,
b
)
err
=
s
.
NamedScan
(
"b"
)
assert
(
t
,
"missing params"
,
err
!=
nil
)
err
=
s
.
NamedScan
(
&
b
,
"b"
)
assert
(
t
,
"invalid param name"
,
err
!=
nil
)
}
func
TestBind
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
err
:=
db
.
Exec
(
"CREATE TABLE test (data TEXT, bool INT)"
)
checkNoError
(
t
,
err
,
"exec error: %s"
)
is
,
err
:=
db
.
Prepare
(
"INSERT INTO test (data, bool) VALUES (?, ?)"
)
defer
checkFinalize
(
is
,
t
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
err
=
is
.
Bind
(
nil
,
true
)
checkNoError
(
t
,
err
,
"bind error: %s"
)
_
,
err
=
is
.
Next
()
checkNoError
(
t
,
err
,
"step error: %s"
)
err
=
is
.
Bind
(
nil
,
db
)
assert
(
t
,
"unsupported type error expected"
,
err
!=
nil
)
}
func
TestInsertMisuse
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
checkClose
(
db
,
t
)
err
:=
db
.
Exec
(
"CREATE TABLE test (data TEXT, bool INT)"
)
checkNoError
(
t
,
err
,
"exec error: %s"
)
is
,
err
:=
db
.
Prepare
(
"INSERT INTO test (data, bool) VALUES (?, ?)"
)
defer
checkFinalize
(
is
,
t
)
checkNoError
(
t
,
err
,
"prepare error: %s"
)
_
,
err
=
is
.
Insert
()
assert
(
t
,
"missing bind parameters expected"
,
err
!=
nil
)
}
}
vtab_test.go
View file @
a8febe56
...
@@ -105,16 +105,15 @@ func TestCreateModule(t *testing.T) {
...
@@ -105,16 +105,15 @@ func TestCreateModule(t *testing.T) {
err
=
db
.
Exec
(
"CREATE VIRTUAL TABLE vtab USING test('1', 2, three)"
)
err
=
db
.
Exec
(
"CREATE VIRTUAL TABLE vtab USING test('1', 2, three)"
)
checkNoError
(
t
,
err
,
"couldn't create virtual table: %s"
)
checkNoError
(
t
,
err
,
"couldn't create virtual table: %s"
)
s
,
err
:=
db
.
Prepare
(
"SELECT
* from
vtab"
)
s
,
err
:=
db
.
Prepare
(
"SELECT
rowid, * FROM
vtab"
)
checkNoError
(
t
,
err
,
"couldn't select from virtual table: %s"
)
checkNoError
(
t
,
err
,
"couldn't select from virtual table: %s"
)
defer
checkFinalize
(
s
,
t
)
defer
checkFinalize
(
s
,
t
)
var
i
,
value
int
var
i
,
value
int
err
=
s
.
Select
(
func
(
s
*
Stmt
)
(
err
error
)
{
err
=
s
.
Select
(
func
(
s
*
Stmt
)
(
err
error
)
{
if
err
=
s
.
Scan
(
&
value
);
err
!=
nil
{
if
err
=
s
.
Scan
(
&
i
,
&
value
);
err
!=
nil
{
return
return
}
}
assertEquals
(
t
,
"Expected '%d' but got '%d'"
,
intarray
[
i
],
value
)
assertEquals
(
t
,
"Expected '%d' but got '%d'"
,
intarray
[
i
],
value
)
i
++
return
return
})
})
checkNoError
(
t
,
err
,
"couldn't select from virtual table: %s"
)
checkNoError
(
t
,
err
,
"couldn't select from virtual table: %s"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment