6
6
package sqlite3
7
7
8
8
import (
9
- "crypto/rand"
10
9
"database/sql"
11
10
"database/sql/driver"
12
- "encoding/hex"
13
11
"errors"
14
12
"fmt"
13
+ "io/ioutil"
15
14
"net/url"
16
15
"os"
17
16
"path/filepath"
@@ -25,15 +24,19 @@ import (
25
24
"github.com/mattn/go-sqlite3/sqlite3_test"
26
25
)
27
26
28
- func TempFilename () string {
29
- randBytes := make ([]byte , 16 )
30
- rand .Read (randBytes )
31
- return filepath .Join (os .TempDir (), "foo" + hex .EncodeToString (randBytes )+ ".db" )
27
+ func TempFilename (t * testing.T ) string {
28
+ f , err := ioutil .TempFile ("" , "go-sqlite3-test-" )
29
+ if err != nil {
30
+ t .Fatal (err )
31
+ }
32
+ f .Close ()
33
+ return f .Name ()
32
34
}
33
35
34
36
func doTestOpen (t * testing.T , option string ) (string , error ) {
35
37
var url string
36
- tempFilename := TempFilename ()
38
+ tempFilename := TempFilename (t )
39
+ defer os .Remove (tempFilename )
37
40
if option != "" {
38
41
url = tempFilename + option
39
42
} else {
@@ -84,8 +87,29 @@ func TestOpen(t *testing.T) {
84
87
}
85
88
}
86
89
90
+ func TestReadonly (t * testing.T ) {
91
+ tempFilename := TempFilename (t )
92
+ defer os .Remove (tempFilename )
93
+
94
+ db1 , err := sql .Open ("sqlite3" , "file:" + tempFilename )
95
+ if err != nil {
96
+ t .Fatal (err )
97
+ }
98
+ db1 .Exec ("CREATE TABLE test (x int, y float)" )
99
+
100
+ db2 , err := sql .Open ("sqlite3" , "file:" + tempFilename + "?mode=ro" )
101
+ if err != nil {
102
+ t .Fatal (err )
103
+ }
104
+ _ = db2
105
+ _ , err = db2 .Exec ("INSERT INTO test VALUES (1, 3.14)" )
106
+ if err == nil {
107
+ t .Fatal ("didn't expect INSERT into read-only database to work" )
108
+ }
109
+ }
110
+
87
111
func TestClose (t * testing.T ) {
88
- tempFilename := TempFilename ()
112
+ tempFilename := TempFilename (t )
89
113
db , err := sql .Open ("sqlite3" , tempFilename )
90
114
if err != nil {
91
115
t .Fatal ("Failed to open database:" , err )
@@ -111,7 +135,7 @@ func TestClose(t *testing.T) {
111
135
}
112
136
113
137
func TestInsert (t * testing.T ) {
114
- tempFilename := TempFilename ()
138
+ tempFilename := TempFilename (t )
115
139
db , err := sql .Open ("sqlite3" , tempFilename )
116
140
if err != nil {
117
141
t .Fatal ("Failed to open database:" , err )
@@ -150,7 +174,7 @@ func TestInsert(t *testing.T) {
150
174
}
151
175
152
176
func TestUpdate (t * testing.T ) {
153
- tempFilename := TempFilename ()
177
+ tempFilename := TempFilename (t )
154
178
db , err := sql .Open ("sqlite3" , tempFilename )
155
179
if err != nil {
156
180
t .Fatal ("Failed to open database:" , err )
@@ -215,7 +239,7 @@ func TestUpdate(t *testing.T) {
215
239
}
216
240
217
241
func TestDelete (t * testing.T ) {
218
- tempFilename := TempFilename ()
242
+ tempFilename := TempFilename (t )
219
243
db , err := sql .Open ("sqlite3" , tempFilename )
220
244
if err != nil {
221
245
t .Fatal ("Failed to open database:" , err )
@@ -276,7 +300,7 @@ func TestDelete(t *testing.T) {
276
300
}
277
301
278
302
func TestBooleanRoundtrip (t * testing.T ) {
279
- tempFilename := TempFilename ()
303
+ tempFilename := TempFilename (t )
280
304
db , err := sql .Open ("sqlite3" , tempFilename )
281
305
if err != nil {
282
306
t .Fatal ("Failed to open database:" , err )
@@ -325,7 +349,7 @@ func TestBooleanRoundtrip(t *testing.T) {
325
349
}
326
350
327
351
func TestTimestamp (t * testing.T ) {
328
- tempFilename := TempFilename ()
352
+ tempFilename := TempFilename (t )
329
353
db , err := sql .Open ("sqlite3" , tempFilename )
330
354
if err != nil {
331
355
t .Fatal ("Failed to open database:" , err )
@@ -408,7 +432,7 @@ func TestTimestamp(t *testing.T) {
408
432
}
409
433
410
434
func TestBoolean (t * testing.T ) {
411
- tempFilename := TempFilename ()
435
+ tempFilename := TempFilename (t )
412
436
db , err := sql .Open ("sqlite3" , tempFilename )
413
437
if err != nil {
414
438
t .Fatal ("Failed to open database:" , err )
@@ -500,7 +524,7 @@ func TestBoolean(t *testing.T) {
500
524
}
501
525
502
526
func TestFloat32 (t * testing.T ) {
503
- tempFilename := TempFilename ()
527
+ tempFilename := TempFilename (t )
504
528
db , err := sql .Open ("sqlite3" , tempFilename )
505
529
if err != nil {
506
530
t .Fatal ("Failed to open database:" , err )
@@ -538,7 +562,7 @@ func TestFloat32(t *testing.T) {
538
562
}
539
563
540
564
func TestNull (t * testing.T ) {
541
- tempFilename := TempFilename ()
565
+ tempFilename := TempFilename (t )
542
566
db , err := sql .Open ("sqlite3" , tempFilename )
543
567
if err != nil {
544
568
t .Fatal ("Failed to open database:" , err )
@@ -570,7 +594,7 @@ func TestNull(t *testing.T) {
570
594
}
571
595
572
596
func TestTransaction (t * testing.T ) {
573
- tempFilename := TempFilename ()
597
+ tempFilename := TempFilename (t )
574
598
db , err := sql .Open ("sqlite3" , tempFilename )
575
599
if err != nil {
576
600
t .Fatal ("Failed to open database:" , err )
@@ -630,7 +654,7 @@ func TestTransaction(t *testing.T) {
630
654
}
631
655
632
656
func TestWAL (t * testing.T ) {
633
- tempFilename := TempFilename ()
657
+ tempFilename := TempFilename (t )
634
658
db , err := sql .Open ("sqlite3" , tempFilename )
635
659
if err != nil {
636
660
t .Fatal ("Failed to open database:" , err )
@@ -678,7 +702,7 @@ func TestWAL(t *testing.T) {
678
702
func TestTimezoneConversion (t * testing.T ) {
679
703
zones := []string {"UTC" , "US/Central" , "US/Pacific" , "Local" }
680
704
for _ , tz := range zones {
681
- tempFilename := TempFilename ()
705
+ tempFilename := TempFilename (t )
682
706
db , err := sql .Open ("sqlite3" , tempFilename + "?_loc=" + url .QueryEscape (tz ))
683
707
if err != nil {
684
708
t .Fatal ("Failed to open database:" , err )
@@ -786,7 +810,7 @@ func TestSuite(t *testing.T) {
786
810
// TODO: Execer & Queryer currently disabled
787
811
// https://github.com/mattn/go-sqlite3/issues/82
788
812
func TestExecer (t * testing.T ) {
789
- tempFilename := TempFilename ()
813
+ tempFilename := TempFilename (t )
790
814
db , err := sql .Open ("sqlite3" , tempFilename )
791
815
if err != nil {
792
816
t .Fatal ("Failed to open database:" , err )
@@ -806,7 +830,7 @@ func TestExecer(t *testing.T) {
806
830
}
807
831
808
832
func TestQueryer (t * testing.T ) {
809
- tempFilename := TempFilename ()
833
+ tempFilename := TempFilename (t )
810
834
db , err := sql .Open ("sqlite3" , tempFilename )
811
835
if err != nil {
812
836
t .Fatal ("Failed to open database:" , err )
@@ -847,7 +871,7 @@ func TestQueryer(t *testing.T) {
847
871
}
848
872
849
873
func TestStress (t * testing.T ) {
850
- tempFilename := TempFilename ()
874
+ tempFilename := TempFilename (t )
851
875
db , err := sql .Open ("sqlite3" , tempFilename )
852
876
if err != nil {
853
877
t .Fatal ("Failed to open database:" , err )
@@ -885,7 +909,7 @@ func TestStress(t *testing.T) {
885
909
886
910
func TestDateTimeLocal (t * testing.T ) {
887
911
zone := "Asia/Tokyo"
888
- tempFilename := TempFilename ()
912
+ tempFilename := TempFilename (t )
889
913
db , err := sql .Open ("sqlite3" , tempFilename + "?_loc=" + zone )
890
914
if err != nil {
891
915
t .Fatal ("Failed to open database:" , err )
@@ -952,7 +976,7 @@ func TestVersion(t *testing.T) {
952
976
}
953
977
954
978
func TestNumberNamedParams (t * testing.T ) {
955
- tempFilename := TempFilename ()
979
+ tempFilename := TempFilename (t )
956
980
db , err := sql .Open ("sqlite3" , tempFilename )
957
981
if err != nil {
958
982
t .Fatal ("Failed to open database:" , err )
@@ -988,7 +1012,7 @@ func TestNumberNamedParams(t *testing.T) {
988
1012
}
989
1013
990
1014
func TestStringContainingZero (t * testing.T ) {
991
- tempFilename := TempFilename ()
1015
+ tempFilename := TempFilename (t )
992
1016
db , err := sql .Open ("sqlite3" , tempFilename )
993
1017
if err != nil {
994
1018
t .Fatal ("Failed to open database:" , err )
@@ -1048,7 +1072,7 @@ func (t TimeStamp) Value() (driver.Value, error) {
1048
1072
}
1049
1073
1050
1074
func TestDateTimeNow (t * testing.T ) {
1051
- tempFilename := TempFilename ()
1075
+ tempFilename := TempFilename (t )
1052
1076
db , err := sql .Open ("sqlite3" , tempFilename )
1053
1077
if err != nil {
1054
1078
t .Fatal ("Failed to open database:" , err )
0 commit comments