-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_test.go
More file actions
452 lines (378 loc) · 10.7 KB
/
store_test.go
File metadata and controls
452 lines (378 loc) · 10.7 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package pubengine
import (
"database/sql"
"os"
"testing"
_ "modernc.org/sqlite"
)
func setupTestStore(t *testing.T) (*Store, func()) {
t.Helper()
path := "data/test_blog.db"
os.Remove(path) // clean up any existing test db
s, err := NewStore(path)
if err != nil {
t.Fatalf("failed to create store: %v", err)
}
cleanup := func() {
s.Close()
os.Remove(path)
}
return s, cleanup
}
func TestNewStore(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
if s == nil {
t.Fatal("store should not be nil")
}
if s.db == nil {
t.Fatal("db should not be nil")
}
}
func TestSaveAndGetPost(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
post := BlogPost{
Slug: "test-post",
Title: "Test Post",
Date: "2024-01-15",
Tags: []string{"go", "testing"},
Summary: "A test post summary",
Content: "# Test Content\n\nThis is test content.",
Published: true,
}
// Save post
if err := s.SavePost(post); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
// Get post
got, err := s.GetPost("test-post")
if err != nil {
t.Fatalf("GetPost failed: %v", err)
}
if got.Slug != post.Slug {
t.Errorf("Slug = %q, want %q", got.Slug, post.Slug)
}
if got.Title != post.Title {
t.Errorf("Title = %q, want %q", got.Title, post.Title)
}
if got.Date != post.Date {
t.Errorf("Date = %q, want %q", got.Date, post.Date)
}
if got.Summary != post.Summary {
t.Errorf("Summary = %q, want %q", got.Summary, post.Summary)
}
if got.Content != post.Content {
t.Errorf("Content = %q, want %q", got.Content, post.Content)
}
if got.Link != "/blog/test-post" {
t.Errorf("Link = %q, want %q", got.Link, "/blog/test-post")
}
if !got.Published {
t.Error("Published should be true")
}
if len(got.Tags) != 2 || got.Tags[0] != "go" || got.Tags[1] != "testing" {
t.Errorf("Tags = %v, want [go testing]", got.Tags)
}
}
func TestSavePostUpdate(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
post := BlogPost{
Slug: "update-test",
Title: "Original Title",
Date: "2024-01-01",
Tags: []string{"original"},
Summary: "Original summary",
Content: "Original content",
Published: true,
}
if err := s.SavePost(post); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
// Update post
post.Title = "Updated Title"
post.Tags = []string{"updated", "modified"}
if err := s.SavePost(post); err != nil {
t.Fatalf("SavePost update failed: %v", err)
}
got, err := s.GetPost("update-test")
if err != nil {
t.Fatalf("GetPost failed: %v", err)
}
if got.Title != "Updated Title" {
t.Errorf("Title = %q, want %q", got.Title, "Updated Title")
}
if len(got.Tags) != 2 {
t.Errorf("Tags count = %d, want 2", len(got.Tags))
}
}
func TestGetPostNotFound(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
_, err := s.GetPost("nonexistent")
if err != sql.ErrNoRows {
t.Errorf("expected sql.ErrNoRows, got %v", err)
}
}
func TestGetPostUnpublished(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
post := BlogPost{
Slug: "unpublished-post",
Title: "Unpublished Post",
Date: "2024-01-01",
Tags: []string{"draft"},
Summary: "Draft summary",
Content: "Draft content",
Published: false,
}
if err := s.SavePost(post); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
// GetPost should not find unpublished posts
_, err := s.GetPost("unpublished-post")
if err != sql.ErrNoRows {
t.Errorf("GetPost should return ErrNoRows for unpublished, got %v", err)
}
// GetPostAny should find unpublished posts
got, err := s.GetPostAny("unpublished-post")
if err != nil {
t.Fatalf("GetPostAny failed: %v", err)
}
if got.Published {
t.Error("Published should be false")
}
}
func TestListPosts(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
posts := []BlogPost{
{Slug: "post-1", Title: "Post 1", Date: "2024-01-01", Tags: []string{"go"}, Summary: "s1", Content: "c1", Published: true},
{Slug: "post-2", Title: "Post 2", Date: "2024-01-02", Tags: []string{"go", "web"}, Summary: "s2", Content: "c2", Published: true},
{Slug: "post-3", Title: "Post 3", Date: "2024-01-03", Tags: []string{"rust"}, Summary: "s3", Content: "c3", Published: true},
{Slug: "post-4", Title: "Post 4", Date: "2024-01-04", Tags: []string{"go"}, Summary: "s4", Content: "c4", Published: false}, // unpublished
}
for _, p := range posts {
if err := s.SavePost(p); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
}
// List all published posts
got, err := s.ListPosts("")
if err != nil {
t.Fatalf("ListPosts failed: %v", err)
}
if len(got) != 3 {
t.Errorf("ListPosts count = %d, want 3 (excluding unpublished)", len(got))
}
// Should be ordered by date DESC
if got[0].Slug != "post-3" {
t.Errorf("First post should be post-3 (latest), got %s", got[0].Slug)
}
}
func TestListPostsByTag(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
posts := []BlogPost{
{Slug: "go-post-1", Title: "Go Post 1", Date: "2024-01-01", Tags: []string{"go", "tutorial"}, Summary: "s1", Content: "c1", Published: true},
{Slug: "go-post-2", Title: "Go Post 2", Date: "2024-01-02", Tags: []string{"go", "web"}, Summary: "s2", Content: "c2", Published: true},
{Slug: "rust-post", Title: "Rust Post", Date: "2024-01-03", Tags: []string{"rust"}, Summary: "s3", Content: "c3", Published: true},
}
for _, p := range posts {
if err := s.SavePost(p); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
}
// Filter by "go" tag
got, err := s.ListPosts("go")
if err != nil {
t.Fatalf("ListPosts with tag failed: %v", err)
}
if len(got) != 2 {
t.Errorf("ListPosts(go) count = %d, want 2", len(got))
}
// Filter by "rust" tag
got, err = s.ListPosts("rust")
if err != nil {
t.Fatalf("ListPosts with tag failed: %v", err)
}
if len(got) != 1 {
t.Errorf("ListPosts(rust) count = %d, want 1", len(got))
}
// Filter by nonexistent tag
got, err = s.ListPosts("nonexistent")
if err != nil {
t.Fatalf("ListPosts with tag failed: %v", err)
}
if len(got) != 0 {
t.Errorf("ListPosts(nonexistent) count = %d, want 0", len(got))
}
}
func TestListPostsTagCaseInsensitive(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
post := BlogPost{
Slug: "case-test",
Title: "Case Test",
Date: "2024-01-01",
Tags: []string{"GoLang", "WEB"},
Summary: "s",
Content: "c",
Published: true,
}
if err := s.SavePost(post); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
// Should find with lowercase
got, err := s.ListPosts("golang")
if err != nil {
t.Fatalf("ListPosts failed: %v", err)
}
if len(got) != 1 {
t.Errorf("ListPosts(golang) should find post with GoLang tag, got %d", len(got))
}
// Should find with uppercase
got, err = s.ListPosts("WEB")
if err != nil {
t.Fatalf("ListPosts failed: %v", err)
}
if len(got) != 1 {
t.Errorf("ListPosts(WEB) should find post with web tag, got %d", len(got))
}
}
func TestListAllPosts(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
posts := []BlogPost{
{Slug: "published", Title: "Published", Date: "2024-01-01", Tags: []string{"a"}, Summary: "s1", Content: "c1", Published: true},
{Slug: "unpublished", Title: "Unpublished", Date: "2024-01-02", Tags: []string{"b"}, Summary: "s2", Content: "c2", Published: false},
}
for _, p := range posts {
if err := s.SavePost(p); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
}
got, err := s.ListAllPosts()
if err != nil {
t.Fatalf("ListAllPosts failed: %v", err)
}
if len(got) != 2 {
t.Errorf("ListAllPosts count = %d, want 2 (including unpublished)", len(got))
}
}
func TestListTags(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
posts := []BlogPost{
{Slug: "p1", Title: "P1", Date: "2024-01-01", Tags: []string{"Go", "Web"}, Summary: "s1", Content: "c1", Published: true},
{Slug: "p2", Title: "P2", Date: "2024-01-02", Tags: []string{"go", "api"}, Summary: "s2", Content: "c2", Published: true},
{Slug: "p3", Title: "P3", Date: "2024-01-03", Tags: []string{"rust"}, Summary: "s3", Content: "c3", Published: false}, // unpublished
}
for _, p := range posts {
if err := s.SavePost(p); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
}
got, err := s.ListTags()
if err != nil {
t.Fatalf("ListTags failed: %v", err)
}
// Should only include tags from published posts, deduplicated and lowercase
// Expected: [api, go, web] (sorted)
if len(got) != 3 {
t.Errorf("ListTags count = %d, want 3, got %v", len(got), got)
}
expected := []string{"api", "go", "web"}
for i, tag := range expected {
if i >= len(got) || got[i] != tag {
t.Errorf("ListTags[%d] = %q, want %q", i, got[i], tag)
}
}
}
func TestDeletePost(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
post := BlogPost{
Slug: "to-delete",
Title: "To Delete",
Date: "2024-01-01",
Tags: []string{"delete"},
Summary: "s",
Content: "c",
Published: true,
}
if err := s.SavePost(post); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
// Verify post exists
_, err := s.GetPost("to-delete")
if err != nil {
t.Fatalf("Post should exist before delete: %v", err)
}
// Delete post
if err := s.DeletePost("to-delete"); err != nil {
t.Fatalf("DeletePost failed: %v", err)
}
// Verify post is gone
_, err = s.GetPost("to-delete")
if err != sql.ErrNoRows {
t.Errorf("Post should not exist after delete, got err: %v", err)
}
}
func TestDeleteNonexistentPost(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
// Should not error when deleting nonexistent post
err := s.DeletePost("nonexistent")
if err != nil {
t.Errorf("DeletePost on nonexistent should not error, got: %v", err)
}
}
func TestParseTags(t *testing.T) {
tests := []struct {
input string
want []string
}{
{"", nil},
{",", nil},
{",go,", []string{"go"}},
{",go,web,", []string{"go", "web"}},
{",go, web ,rust,", []string{"go", "web", "rust"}},
}
for _, tt := range tests {
got := ParseTags(tt.input)
if len(got) != len(tt.want) {
t.Errorf("ParseTags(%q) = %v, want %v", tt.input, got, tt.want)
continue
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("ParseTags(%q)[%d] = %q, want %q", tt.input, i, got[i], tt.want[i])
}
}
}
}
func TestEmptyTags(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
post := BlogPost{
Slug: "no-tags",
Title: "No Tags",
Date: "2024-01-01",
Tags: []string{},
Summary: "s",
Content: "c",
Published: true,
}
if err := s.SavePost(post); err != nil {
t.Fatalf("SavePost failed: %v", err)
}
got, err := s.GetPost("no-tags")
if err != nil {
t.Fatalf("GetPost failed: %v", err)
}
if len(got.Tags) != 0 {
t.Errorf("Tags should be empty, got %v", got.Tags)
}
}