diff options
Diffstat (limited to 'models/db_test.go')
| -rw-r--r-- | models/db_test.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/models/db_test.go b/models/db_test.go new file mode 100644 index 0000000..08ceb44 --- /dev/null +++ b/models/db_test.go @@ -0,0 +1,47 @@ +package models + +import ( + "testing" + + "adammathes.com/neko/config" +) + +func TestInitDB(t *testing.T) { + config.Config.DBFile = ":memory:" + InitDB() + defer DB.Close() + + if DB == nil { + t.Fatal("DB should not be nil after InitDB") + } + + err := DB.Ping() + if err != nil { + t.Fatalf("DB.Ping() should succeed: %v", err) + } + + // Verify schema was created by checking tables exist + var name string + err = DB.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name='feed'").Scan(&name) + if err != nil { + t.Fatalf("feed table should exist: %v", err) + } + + err = DB.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name='item'").Scan(&name) + if err != nil { + t.Fatalf("item table should exist: %v", err) + } +} + +// SetupTestDB initializes an in-memory SQLite database for testing. +// Call this from other packages' tests to get a working DB. +func SetupTestDB(t *testing.T) { + t.Helper() + config.Config.DBFile = ":memory:" + InitDB() + t.Cleanup(func() { + if DB != nil { + DB.Close() + } + }) +} |
