@@ -5,111 +5,50 @@ package database
55
66import (
77 "context"
8- "os"
98 "path/filepath"
109 "testing"
10+
11+ "github.com/stretchr/testify/require"
1112)
1213
13- func TestDatabase_EdgeCases (t * testing.T ) {
14- tempDir := t .TempDir ()
15- nestedPath := filepath .Join (tempDir , "nested" , "test.db" )
14+ func TestDatabase_Open_MkdirError (t * testing.T ) {
1615 ctx := context .Background ()
16+ // An invalid path that cannot be created, e.g. /dev/null/test.db
17+ invalidPath := filepath .Join ("/dev/null" , "test.db" )
1718
18- // 1. Successful initialization
19- config := Config {
20- Path : nestedPath ,
19+ _ , err := Open (ctx , Config {
20+ Path : invalidPath ,
2121 WALMode : true ,
22- }
23- db , err := Open (ctx , config )
24- if err != nil {
25- t .Fatalf ("Failed to open nested db: %v" , err )
26- }
27-
28- if p := db .Path (); p != nestedPath {
29- t .Errorf ("Path() returned %s, expected %s" , p , nestedPath )
30- }
31-
32- if conn := db .Conn (); conn == nil {
33- t .Error ("Conn() returned nil" )
34- }
35-
36- if err := db .Ping (ctx ); err != nil {
37- t .Errorf ("Ping failed: %v" , err )
38- }
39-
40- // 2. BeginTx
41- tx , err := db .BeginTx (ctx , nil )
42- if err != nil {
43- t .Errorf ("BeginTx failed: %v" , err )
44- } else {
45- tx .Rollback ()
46- }
47-
48- // 3. GetSchemaVersion
49- version , err := db .GetSchemaVersion (ctx )
50- if err != nil {
51- t .Errorf ("GetSchemaVersion failed: %v" , err )
52- }
53- if version < 0 {
54- t .Errorf ("Invalid version %d" , version )
55- }
56-
57- db .Close ()
22+ })
23+ require .Error (t , err )
24+ require .Contains (t , err .Error (), "create database directory" )
25+ }
5826
59- // Double close should be fine or return an error gracefully
60- _ = db .Close ()
27+ func TestDatabase_Open_EnableWALModeError (t * testing.T ) {
28+ // A canceled context will cause enableWALMode (ExecContext) to fail
29+ ctx , cancel := context .WithCancel (context .Background ())
30+ cancel ()
6131
62- // 4. Invalid Path for MkdirAll to fail
63- // A file as a parent directory
64- invalidPathDir := filepath .Join (tempDir , "file_as_dir" )
65- os .WriteFile (invalidPathDir , []byte ("content" ), 0644 )
66- invalidConfig := Config {Path : filepath .Join (invalidPathDir , "test.db" )}
32+ dbPath := filepath .Join (t .TempDir (), "test.db" )
33+ _ , err := Open (ctx , Config {
34+ Path : dbPath ,
35+ WALMode : true ,
36+ })
37+ require .Error (t , err )
38+ require .Contains (t , err .Error (), "enable WAL mode" )
39+ }
6740
68- _ , err = Open ( ctx , invalidConfig )
69- if err == nil {
70- t . Error ( "Expected error opening db where parent is a file" )
71- }
41+ func TestDatabase_Close_NilConn ( t * testing. T ) {
42+ db := & DB { conn : nil }
43+ err := db . Close ( )
44+ require . NoError ( t , err )
7245}
7346
74- func TestMigrationManager_Rollback (t * testing.T ) {
75- tempDir := t .TempDir ()
47+ func TestDatabase_Path (t * testing.T ) {
7648 ctx := context .Background ()
77- config := Config {Path : filepath .Join (tempDir , "test.db" )}
78- db , err := Open (ctx , config )
79- if err != nil {
80- t .Fatalf ("Open failed: %v" , err )
81- }
49+ dbPath := filepath .Join (t .TempDir (), "test.db" )
50+ db , err := Open (ctx , Config {Path : dbPath , WALMode : false })
51+ require .NoError (t , err )
8252 defer db .Close ()
83-
84- m := NewMigrationManager (db .Conn ())
85-
86- // Open runs ApplyMigrations, so we have some migrations.
87- // Let's get current version
88- v , err := m .GetCurrentVersion (ctx )
89- if err != nil {
90- t .Fatalf ("GetCurrentVersion: %v" , err )
91- }
92-
93- if v == 0 {
94- // Insert a fake migration record if none
95- _ , err := db .Conn ().ExecContext (ctx , "CREATE TABLE IF NOT EXISTS schema_migrations (version INTEGER PRIMARY KEY, description TEXT)" )
96- if err == nil {
97- db .Conn ().ExecContext (ctx , "INSERT INTO schema_migrations (version, description) VALUES (9999, 'Test')" )
98- }
99- }
100-
101- err = m .Rollback (ctx )
102- if err != nil {
103- t .Logf ("Rollback error (expected if down sql is missing or migration not found): %v" , err )
104- }
105-
106- // Create a table schema_migrations manually if GetCurrentVersion fails
107- db2 , _ := Open (ctx , Config {Path : filepath .Join (tempDir , "test2.db" )})
108- defer db2 .Close ()
109- // test close connection cases
110- db2 .Close ()
111- m2 := NewMigrationManager (db2 .Conn ())
112- _ = m2 .ApplyMigrations (ctx )
113- _ , _ = m2 .GetCurrentVersion (ctx )
114- _ = m2 .Rollback (ctx )
53+ require .Equal (t , dbPath , db .Path ())
11554}
0 commit comments