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
|
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)
}
}
}
|