107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
|
|
package storage
|
||
|
|
|
||
|
|
import (
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func notesTestStore(t *testing.T) *Store {
|
||
|
|
t.Helper()
|
||
|
|
s, err := Open(filepath.Join(t.TempDir(), "notes.db"))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
t.Cleanup(func() { s.Close() })
|
||
|
|
return s
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNotes_SeededOnFirstOpen(t *testing.T) {
|
||
|
|
s := notesTestStore(t)
|
||
|
|
notes, err := s.ListActiveNotes()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(notes) != len(defaultNotes) {
|
||
|
|
t.Fatalf("seeded %d notes, want %d", len(notes), len(defaultNotes))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNotes_DeletedDefaultDoesNotResurrect(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
path := filepath.Join(dir, "n.db")
|
||
|
|
|
||
|
|
s, err := Open(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
all, _ := s.ListActiveNotes()
|
||
|
|
if err := s.DeleteNote(all[0].ID); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
s.Close()
|
||
|
|
|
||
|
|
// Reopen: seeding must NOT run again (table is non-empty thanks to history).
|
||
|
|
s2, err := Open(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
defer s2.Close()
|
||
|
|
notes, _ := s2.ListActiveNotes()
|
||
|
|
if len(notes) != len(defaultNotes)-1 {
|
||
|
|
t.Errorf("after delete+reopen got %d active notes, want %d", len(notes), len(defaultNotes)-1)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNotes_EditKeepsHistory(t *testing.T) {
|
||
|
|
s := notesTestStore(t)
|
||
|
|
id, err := s.AddNote("original")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
newID, err := s.EditNote(id, "revised")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if newID == id {
|
||
|
|
t.Error("edit should create a new row id")
|
||
|
|
}
|
||
|
|
active, _ := s.ListActiveNotes()
|
||
|
|
var texts []string
|
||
|
|
for _, n := range active {
|
||
|
|
texts = append(texts, n.Text)
|
||
|
|
}
|
||
|
|
if contains(texts, "original") {
|
||
|
|
t.Error("old text should no longer be active")
|
||
|
|
}
|
||
|
|
if !contains(texts, "revised") {
|
||
|
|
t.Error("revised text should be active")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNotes_CreatedAfter(t *testing.T) {
|
||
|
|
s := notesTestStore(t)
|
||
|
|
cutoff := time.Now().UTC()
|
||
|
|
// created_at has second resolution (RFC3339); make sure the new note sorts after.
|
||
|
|
time.Sleep(1100 * time.Millisecond)
|
||
|
|
if _, err := s.AddNote("late note"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
after, err := s.NotesCreatedAfter(cutoff)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(after) != 1 || after[0].Text != "late note" {
|
||
|
|
t.Fatalf("NotesCreatedAfter = %v, want just the late note", after)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func contains(ss []string, want string) bool {
|
||
|
|
for _, s := range ss {
|
||
|
|
if s == want {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|