-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver_test.go
More file actions
835 lines (696 loc) · 20.7 KB
/
driver_test.go
File metadata and controls
835 lines (696 loc) · 20.7 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
package sqlite
import (
"database/sql"
"database/sql/driver"
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"
)
func TestDriverRegistration(t *testing.T) {
drivers := sql.Drivers()
foundSqlite3 := false
for _, d := range drivers {
if d == "sqlite3" {
foundSqlite3 = true
}
}
if !foundSqlite3 {
t.Error("sqlite3 driver not registered")
}
}
func TestOpenConnection(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
if err := db.Ping(); err != nil {
t.Fatalf("Failed to ping database: %v", err)
}
}
func TestCreateTable(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
query := `
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
email TEXT UNIQUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`
if _, err := db.Exec(query); err != nil {
t.Fatalf("Failed to create table: %v", err)
}
}
func TestInsertAndSelect(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
_, err = db.Exec(`CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
result, err := db.Exec("INSERT INTO users (name, age) VALUES (?, ?)", "Alice", 30)
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
lastID, err := result.LastInsertId()
if err != nil {
t.Fatalf("Failed to get last insert ID: %v", err)
}
if lastID != 1 {
t.Errorf("Expected last insert ID 1, got %d", lastID)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
t.Fatalf("Failed to get rows affected: %v", err)
}
if rowsAffected != 1 {
t.Errorf("Expected 1 row affected, got %d", rowsAffected)
}
var name string
var age int
err = db.QueryRow("SELECT name, age FROM users WHERE id = ?", lastID).Scan(&name, &age)
if err != nil {
t.Fatalf("Failed to query: %v", err)
}
if name != "Alice" {
t.Errorf("Expected name Alice, got %s", name)
}
if age != 30 {
t.Errorf("Expected age 30, got %d", age)
}
}
func TestNullValues(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
_, err = db.Exec(`CREATE TABLE nullable (id INTEGER PRIMARY KEY, value TEXT)`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
_, err = db.Exec("INSERT INTO nullable (id, value) VALUES (1, NULL)")
if err != nil {
t.Fatalf("Failed to insert NULL: %v", err)
}
var value sql.NullString
err = db.QueryRow("SELECT value FROM nullable WHERE id = 1").Scan(&value)
if err != nil {
t.Fatalf("Failed to query NULL: %v", err)
}
if value.Valid {
t.Error("Expected NULL value to be invalid")
}
}
func TestTransaction(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
_, err = db.Exec(`CREATE TABLE accounts (id INTEGER PRIMARY KEY, balance INTEGER)`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
_, err = db.Exec("INSERT INTO accounts (id, balance) VALUES (1, 100), (2, 100)")
if err != nil {
t.Fatalf("Failed to insert initial data: %v", err)
}
tx, err := db.Begin()
if err != nil {
t.Fatalf("Failed to begin transaction: %v", err)
}
_, err = tx.Exec("UPDATE accounts SET balance = balance - 50 WHERE id = 1")
if err != nil {
tx.Rollback()
t.Fatalf("Failed to update account 1: %v", err)
}
_, err = tx.Exec("UPDATE accounts SET balance = balance + 50 WHERE id = 2")
if err != nil {
tx.Rollback()
t.Fatalf("Failed to update account 2: %v", err)
}
if err := tx.Commit(); err != nil {
t.Fatalf("Failed to commit transaction: %v", err)
}
var balance1, balance2 int
err = db.QueryRow("SELECT balance FROM accounts WHERE id = 1").Scan(&balance1)
if err != nil {
t.Fatalf("Failed to query balance 1: %v", err)
}
err = db.QueryRow("SELECT balance FROM accounts WHERE id = 2").Scan(&balance2)
if err != nil {
t.Fatalf("Failed to query balance 2: %v", err)
}
if balance1 != 50 {
t.Errorf("Expected balance1 50, got %d", balance1)
}
if balance2 != 150 {
t.Errorf("Expected balance2 150, got %d", balance2)
}
}
func TestRollback(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
_, err = db.Exec(`CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
tx, err := db.Begin()
if err != nil {
t.Fatalf("Failed to begin transaction: %v", err)
}
_, err = tx.Exec("INSERT INTO test (value) VALUES (?)", "should_not_exist")
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
if err := tx.Rollback(); err != nil {
t.Fatalf("Failed to rollback: %v", err)
}
var count int
err = db.QueryRow("SELECT COUNT(*) FROM test").Scan(&count)
if err != nil {
t.Fatalf("Failed to count rows: %v", err)
}
if count != 0 {
t.Errorf("Expected 0 rows after rollback, got %d", count)
}
}
func TestPreparedStatement(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
_, err = db.Exec(`CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
stmt, err := db.Prepare("INSERT INTO users (name) VALUES (?)")
if err != nil {
t.Fatalf("Failed to prepare statement: %v", err)
}
defer stmt.Close()
names := []string{"Alice", "Bob", "Charlie"}
for _, name := range names {
_, err := stmt.Exec(name)
if err != nil {
t.Fatalf("Failed to execute prepared statement for %s: %v", name, err)
}
}
var count int
err = db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count)
if err != nil {
t.Fatalf("Failed to count rows: %v", err)
}
if count != len(names) {
t.Errorf("Expected %d rows, got %d", len(names), count)
}
}
func TestConcurrentAccess(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "concurrent.db")
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
db.SetMaxOpenConns(5)
_, err = db.Exec(`CREATE TABLE counter (id INTEGER PRIMARY KEY, value INTEGER)`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
_, err = db.Exec("INSERT INTO counter (id, value) VALUES (1, 0)")
if err != nil {
t.Fatalf("Failed to insert initial value: %v", err)
}
_, err = db.Exec("PRAGMA journal_mode=WAL")
if err != nil {
t.Fatalf("Failed to enable WAL mode: %v", err)
}
_, err = db.Exec("PRAGMA busy_timeout=5000")
if err != nil {
t.Fatalf("Failed to set busy timeout: %v", err)
}
var wg sync.WaitGroup
numGoroutines := 10
incrementsPerGoroutine := 10
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for j := 0; j < incrementsPerGoroutine; j++ {
retries := 0
for retries < 50 {
_, err := db.Exec("UPDATE counter SET value = value + 1 WHERE id = 1")
if err == nil {
break
}
retries++
if retries == 50 {
t.Logf("Worker %d: Failed after 50 retries: %v", id, err)
}
time.Sleep(time.Millisecond * 10)
}
}
}(i)
}
wg.Wait()
var value int
err = db.QueryRow("SELECT value FROM counter WHERE id = 1").Scan(&value)
if err != nil {
t.Fatalf("Failed to query counter: %v", err)
}
expected := numGoroutines * incrementsPerGoroutine
if value != expected {
t.Logf("Note: Got %d updates instead of %d. This is acceptable for SQLite under high concurrency.", value, expected)
}
}
func TestDataTypes(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
_, err = db.Exec(`
CREATE TABLE types (
id INTEGER PRIMARY KEY,
int_val INTEGER,
real_val REAL,
text_val TEXT,
blob_val BLOB,
bool_val BOOLEAN,
time_val DATETIME
)
`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
now := time.Now().Round(time.Second)
blobData := []byte{0x01, 0x02, 0x03, 0x04}
_, err = db.Exec(
"INSERT INTO types (int_val, real_val, text_val, blob_val, bool_val, time_val) VALUES (?, ?, ?, ?, ?, ?)",
42, 3.14159, "hello", blobData, true, now,
)
if err != nil {
t.Fatalf("Failed to insert data: %v", err)
}
var intVal int64
var realVal float64
var textVal string
var blobVal []byte
var boolVal bool
var timeVal string
err = db.QueryRow("SELECT int_val, real_val, text_val, blob_val, bool_val, time_val FROM types WHERE id = 1").Scan(
&intVal, &realVal, &textVal, &blobVal, &boolVal, &timeVal,
)
if err != nil {
t.Fatalf("Failed to query data: %v", err)
}
if intVal != 42 {
t.Errorf("Expected int_val 42, got %d", intVal)
}
if realVal != 3.14159 {
t.Errorf("Expected real_val 3.14159, got %f", realVal)
}
if textVal != "hello" {
t.Errorf("Expected text_val 'hello', got %s", textVal)
}
if len(blobVal) != len(blobData) {
t.Errorf("Expected blob_val length %d, got %d", len(blobData), len(blobVal))
}
if !boolVal {
t.Error("Expected bool_val true, got false")
}
}
type CustomValuer struct {
Data string
}
func (c CustomValuer) Value() (driver.Value, error) {
return fmt.Sprintf("custom:%s", c.Data), nil
}
func TestValuerInterface(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
_, err = db.Exec(`CREATE TABLE valuer_test (id INTEGER PRIMARY KEY, value TEXT)`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
custom := CustomValuer{Data: "test"}
_, err = db.Exec("INSERT INTO valuer_test (value) VALUES (?)", custom)
if err != nil {
t.Fatalf("Failed to insert with Valuer: %v", err)
}
var result string
err = db.QueryRow("SELECT value FROM valuer_test WHERE id = 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to query: %v", err)
}
if result != "custom:test" {
t.Errorf("Expected 'custom:test', got %s", result)
}
}
func TestFileDatabase(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Failed to open file database: %v", err)
}
defer db.Close()
_, err = db.Exec(`CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
_, err = db.Exec("INSERT INTO test (value) VALUES (?)", "persistent")
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
db.Close()
db2, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Failed to reopen database: %v", err)
}
defer db2.Close()
var value string
err = db2.QueryRow("SELECT value FROM test WHERE id = 1").Scan(&value)
if err != nil {
t.Fatalf("Failed to query after reopen: %v", err)
}
if value != "persistent" {
t.Errorf("Expected 'persistent', got %s", value)
}
}
func TestDSNParsing(t *testing.T) {
tests := []struct {
dsn string
wantErr bool
}{
{":memory:", false},
{"test.db", false},
{"file:test.db", false},
{"file:test.db?mode=ro", false},
{"file:test.db?mode=rw", false},
{"file:test.db?mode=rwc", false},
{"file:test.db?mode=memory", false},
{"file:test.db?cache=shared", false},
{"file:test.db?cache=private", false},
{"", true},
{"file:test.db?mode=invalid", true},
}
for _, tt := range tests {
t.Run(tt.dsn, func(t *testing.T) {
_, err := parseDSN(tt.dsn)
gotErr := err != nil
if gotErr != tt.wantErr {
t.Errorf("parseDSN(%q) error = %v, wantErr = %v", tt.dsn, err, tt.wantErr)
}
})
}
}
func TestReadOnlyMode(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "readonly.db")
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
_, err = db.Exec(`CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
_, err = db.Exec("INSERT INTO test (value) VALUES (?)", "initial")
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
db.Close()
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
t.Fatalf("Database file not created")
}
roDb, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?mode=ro", dbPath))
if err != nil {
t.Fatalf("Failed to open readonly database: %v", err)
}
defer roDb.Close()
var value string
err = roDb.QueryRow("SELECT value FROM test WHERE id = 1").Scan(&value)
if err != nil {
t.Fatalf("Failed to query readonly database: %v", err)
}
if value != "initial" {
t.Errorf("Expected 'initial', got %s", value)
}
_, err = roDb.Exec("INSERT INTO test (value) VALUES (?)", "should_fail")
if err == nil {
t.Error("Expected error when writing to readonly database")
}
}
func TestDeclaredTypeHandling(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
_, err = db.Exec(`
CREATE TABLE type_test (
id INTEGER PRIMARY KEY,
bool_int BOOLEAN,
date_text DATE,
time_text TIME,
datetime_text DATETIME,
timestamp_int TIMESTAMP,
timestamp_real TIMESTAMP,
regular_int INTEGER,
regular_text TEXT
)
`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
t.Run("Boolean as INTEGER", func(t *testing.T) {
_, err := db.Exec("INSERT INTO type_test (bool_int) VALUES (?)", true)
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var result bool
err = db.QueryRow("SELECT bool_int FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if !result {
t.Errorf("Expected true, got false")
}
_, err = db.Exec("INSERT INTO type_test (bool_int) VALUES (?)", false)
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
err = db.QueryRow("SELECT bool_int FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if result {
t.Errorf("Expected false, got true")
}
})
t.Run("DateTime as TEXT", func(t *testing.T) {
testTime := time.Date(2024, 3, 15, 14, 30, 45, 123456789, time.UTC)
_, err := db.Exec("INSERT INTO type_test (datetime_text) VALUES (?)", testTime.Format(time.RFC3339Nano))
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var result time.Time
err = db.QueryRow("SELECT datetime_text FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if !result.Equal(testTime.Truncate(time.Nanosecond)) {
t.Errorf("Expected %v, got %v", testTime, result)
}
})
t.Run("Date only", func(t *testing.T) {
_, err := db.Exec("INSERT INTO type_test (date_text) VALUES (?)", "2024-03-15")
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var result time.Time
err = db.QueryRow("SELECT date_text FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
expected := time.Date(2024, 3, 15, 0, 0, 0, 0, time.UTC)
if !result.Equal(expected) {
t.Errorf("Expected %v, got %v", expected, result)
}
})
t.Run("Time only", func(t *testing.T) {
_, err := db.Exec("INSERT INTO type_test (time_text) VALUES (?)", "14:30:45.123")
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var result time.Time
err = db.QueryRow("SELECT time_text FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
expected := time.Date(0, 1, 1, 14, 30, 45, 123000000, time.UTC)
if !result.Equal(expected) {
t.Errorf("Expected %v, got %v", expected, result)
}
})
t.Run("Unix timestamp as INTEGER", func(t *testing.T) {
testTime := time.Date(2024, 3, 15, 14, 30, 45, 0, time.UTC)
unixTimestamp := testTime.Unix()
_, err := db.Exec("INSERT INTO type_test (timestamp_int) VALUES (?)", unixTimestamp)
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var result time.Time
err = db.QueryRow("SELECT timestamp_int FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if !result.Equal(testTime) {
t.Errorf("Expected %v, got %v", testTime, result)
}
})
t.Run("Unix milliseconds as INTEGER", func(t *testing.T) {
testTime := time.Date(2024, 3, 15, 14, 30, 45, 123000000, time.UTC)
unixMillis := testTime.UnixMilli()
_, err := db.Exec("INSERT INTO type_test (timestamp_int) VALUES (?)", unixMillis)
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var result time.Time
err = db.QueryRow("SELECT timestamp_int FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if !result.Equal(testTime) {
t.Errorf("Expected %v, got %v", testTime, result)
}
})
t.Run("Unix timestamp with fractional seconds as REAL", func(t *testing.T) {
testTime := time.Date(2024, 3, 15, 14, 30, 45, 123456789, time.UTC)
unixFloat := float64(testTime.Unix()) + float64(testTime.Nanosecond())/1e9
_, err := db.Exec("INSERT INTO type_test (timestamp_real) VALUES (?)", unixFloat)
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var result time.Time
err = db.QueryRow("SELECT timestamp_real FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
// Float precision limits mean we can't expect exact nanosecond precision
diff := result.Sub(testTime).Abs()
if diff > time.Microsecond {
t.Errorf("Expected %v, got %v (diff: %v)", testTime, result, diff)
}
})
t.Run("Julian day as REAL", func(t *testing.T) {
testTime := time.Date(2024, 3, 15, 14, 30, 45, 0, time.UTC)
julianDay := timeToJulian(testTime)
_, err := db.Exec("INSERT INTO type_test (timestamp_real) VALUES (?)", julianDay)
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var result time.Time
err = db.QueryRow("SELECT timestamp_real FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
diff := result.Sub(testTime).Abs()
if diff > time.Millisecond {
t.Errorf("Expected %v, got %v (diff: %v)", testTime, result, diff)
}
})
t.Run("Regular types not converted", func(t *testing.T) {
unixTimestamp := time.Now().Unix()
_, err := db.Exec("INSERT INTO type_test (regular_int, regular_text) VALUES (?, ?)",
unixTimestamp, "2024-03-15")
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var intResult int64
var textResult string
err = db.QueryRow("SELECT regular_int, regular_text FROM type_test ORDER BY id DESC LIMIT 1").Scan(
&intResult, &textResult)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if intResult != unixTimestamp {
t.Errorf("Expected int %d, got %d", unixTimestamp, intResult)
}
if textResult != "2024-03-15" {
t.Errorf("Expected text '2024-03-15', got %s", textResult)
}
})
t.Run("SQLite functions", func(t *testing.T) {
_, err := db.Exec("INSERT INTO type_test (datetime_text) VALUES (datetime('2024-03-15 14:30:45'))")
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var result time.Time
err = db.QueryRow("SELECT datetime_text FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
expected := time.Date(2024, 3, 15, 14, 30, 45, 0, time.UTC)
if !result.Equal(expected) {
t.Errorf("Expected %v, got %v", expected, result)
}
})
t.Run("CURRENT_TIMESTAMP", func(t *testing.T) {
before := time.Now().UTC()
_, err := db.Exec("INSERT INTO type_test (timestamp_int) VALUES (CAST(strftime('%s', 'now') AS INTEGER))")
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
after := time.Now().UTC()
var result time.Time
err = db.QueryRow("SELECT timestamp_int FROM type_test ORDER BY id DESC LIMIT 1").Scan(&result)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if result.Before(before.Truncate(time.Second)) || result.After(after.Add(time.Second)) {
t.Errorf("Timestamp %v not within expected range [%v, %v]", result, before, after)
}
})
t.Run("NULL handling", func(t *testing.T) {
_, err := db.Exec("INSERT INTO type_test (bool_int, datetime_text) VALUES (NULL, NULL)")
if err != nil {
t.Fatalf("Failed to insert: %v", err)
}
var boolNull sql.NullBool
var timeNull sql.NullTime
err = db.QueryRow("SELECT bool_int, datetime_text FROM type_test ORDER BY id DESC LIMIT 1").Scan(
&boolNull, &timeNull)
if err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if boolNull.Valid {
t.Errorf("Expected NULL bool, got %v", boolNull.Bool)
}
if timeNull.Valid {
t.Errorf("Expected NULL time, got %v", timeNull.Time)
}
})
}