diff options
| author | Sam Scholten | 2026-06-14 20:00:15 +1000 |
|---|---|---|
| committer | Sam Scholten | 2026-06-14 20:00:15 +1000 |
| commit | decc46c876e7b5552f5f5ecac4ee4f1a64ad1d62 (patch) | |
| tree | 46875e236a062189115c0cd8ed8f1d82980c16b7 /db_test.go | |
| download | abvjt-decc46c876e7b5552f5f5ecac4ee4f1a64ad1d62.tar.gz abvjt-decc46c876e7b5552f5f5ecac4ee4f1a64ad1d62.zip | |
Diffstat (limited to 'db_test.go')
| -rw-r--r-- | db_test.go | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/db_test.go b/db_test.go new file mode 100644 index 0000000..8e507ff --- /dev/null +++ b/db_test.go @@ -0,0 +1,186 @@ +package main + +import ( + "path/filepath" + "testing" +) + +func TestOpenDB(t *testing.T) { + dir := t.TempDir() + dbPath := filepath.Join(dir, "test.db") + + db, err := OpenDB(dbPath) + if err != nil { + t.Fatalf("OpenDB failed: %v", err) + } + defer db.Close() + + count, err := db.Count() + if err != nil { + t.Fatalf("Count failed: %v", err) + } + if count != 0 { + t.Errorf("expected 0 journals, got %d", count) + } +} + +func TestInsertAndSearch(t *testing.T) { + dir := t.TempDir() + dbPath := filepath.Join(dir, "test.db") + + db, err := OpenDB(dbPath) + if err != nil { + t.Fatalf("OpenDB failed: %v", err) + } + defer db.Close() + + journals := []Journal{ + {FullName: "Journal of Applied Physics", Abbreviation: "J APPL PHYS"}, + {FullName: "Nature Medicine", Abbreviation: "NAT MED"}, + {FullName: "Physical Review Letters", Abbreviation: "PHYS REV LETT"}, + } + + if err := db.InsertJournals(journals); err != nil { + t.Fatalf("InsertJournals failed: %v", err) + } + + count, err := db.Count() + if err != nil { + t.Fatalf("Count failed: %v", err) + } + if count != 3 { + t.Errorf("expected 3 journals, got %d", count) + } + + results, err := db.SearchJournals("physics", 10) + if err != nil { + t.Fatalf("SearchJournals failed: %v", err) + } + if len(results) == 0 { + t.Error("expected at least one result for 'physics'") + } + + found := false + for _, r := range results { + if r.FullName == "Journal of Applied Physics" { + found = true + if r.Abbreviation != "J APPL PHYS" { + t.Errorf("expected abbreviation 'J APPL PHYS', got %q", r.Abbreviation) + } + } + } + if !found { + t.Error("expected to find 'Journal of Applied Physics' in results") + } +} + +func TestSearchPrefix(t *testing.T) { + dir := t.TempDir() + dbPath := filepath.Join(dir, "test.db") + + db, err := OpenDB(dbPath) + if err != nil { + t.Fatalf("OpenDB failed: %v", err) + } + defer db.Close() + + journals := []Journal{ + {FullName: "Journal of Applied Physics", Abbreviation: "J APPL PHYS"}, + {FullName: "Nature Medicine", Abbreviation: "NAT MED"}, + } + if err := db.InsertJournals(journals); err != nil { + t.Fatalf("InsertJournals failed: %v", err) + } + + results, err := db.SearchJournals("nat med", 10) + if err != nil { + t.Fatalf("SearchJournals failed: %v", err) + } + if len(results) == 0 { + t.Error("expected results for prefix query 'nat med', got none") + } +} + +func TestSearchLimit(t *testing.T) { + dir := t.TempDir() + dbPath := filepath.Join(dir, "test.db") + + db, err := OpenDB(dbPath) + if err != nil { + t.Fatalf("OpenDB failed: %v", err) + } + defer db.Close() + + journals := []Journal{ + {FullName: "Journal One", Abbreviation: "J ONE"}, + {FullName: "Journal Two", Abbreviation: "J TWO"}, + {FullName: "Journal Three", Abbreviation: "J THREE"}, + } + if err := db.InsertJournals(journals); err != nil { + t.Fatalf("InsertJournals failed: %v", err) + } + + results, err := db.SearchJournals("journal", 2) + if err != nil { + t.Fatalf("SearchJournals failed: %v", err) + } + if len(results) > 2 { + t.Errorf("expected at most 2 results, got %d", len(results)) + } +} + +func TestSanitizeFTS5(t *testing.T) { + cases := []struct { + input string + expected string + }{ + {"hello world", "hello* world*"}, + {"hello world", "hello* world*"}, + {"hello-world", "hello* world*"}, + {"hello&world", "hello* world*"}, + {"hello(world)", "hello* world*"}, + {"C++", "c*"}, + {"Nature & Science", "nature* science*"}, + {"phys*", "phys*"}, + {"a", "a*"}, + {"", ""}, + {"!!!", ""}, + } + + for _, c := range cases { + got := sanitizeFTS5(c.input) + if got != c.expected { + t.Errorf("sanitizeFTS5(%q) = %q, want %q", c.input, got, c.expected) + } + } +} + +func TestSearchWithSpecialChars(t *testing.T) { + dir := t.TempDir() + dbPath := filepath.Join(dir, "test.db") + + db, err := OpenDB(dbPath) + if err != nil { + t.Fatalf("OpenDB failed: %v", err) + } + defer db.Close() + + journals := []Journal{ + {FullName: "Nature & Science", Abbreviation: "NAT SCI"}, + {FullName: "C++ Weekly", Abbreviation: "C WEEKLY"}, + {FullName: "Physical Review (Letters)", Abbreviation: "PHYS REV LETT"}, + } + if err := db.InsertJournals(journals); err != nil { + t.Fatalf("InsertJournals failed: %v", err) + } + + for _, q := range []string{"Nature & Science", "C++", "Physical Review (Letters)"} { + results, err := db.SearchJournals(q, 10) + if err != nil { + t.Fatalf("SearchJournals(%q) failed: %v", q, err) + } + if len(results) == 0 { + t.Errorf("expected results for %q, got none", q) + } + } +} |
