aboutsummaryrefslogtreecommitdiffstats
path: root/models/db_test.go
blob: 08ceb448b9a0a28f369bdede9add4d2a326f6a80 (plain) (blame)
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
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()
		}
	})
}