Adding save tests

This commit is contained in:
Micheal Wilkinson
2026-03-17 23:09:18 +00:00
parent 89853e3582
commit 6b2badd036
2 changed files with 99 additions and 0 deletions

View File

@@ -7,7 +7,9 @@ import (
"context"
"os"
"path/filepath"
"time"
"git.hrafn.xyz/aether/notes/internal/models"
"git.hrafn.xyz/aether/notes/internal/store/sqlite"
)
@@ -58,3 +60,94 @@ func TestCreateSQLiteStore(t *testing.T) {
}
}
func TestSaveNote(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "test.db")
store, err := sqlite.NewSQLiteStore(context.Background(), dbPath)
if err != nil {
t.Fatalf("failed to create SQLite store: %v", err)
}
defer func() {
store.Close()
if _, err := os.Stat(dbPath); !errors.Is(err, os.ErrNotExist) {
if err := os.Remove(dbPath); err != nil {
t.Errorf("failed to clean up database file: %v", err)
}
}
}()
testcases := []struct {
name string
note models.Note
expectedNote models.Note
expectedError bool
}{
{
name: "new note",
note: models.Note{
Content: "Test note",
LastUpdate: time.Now(),
},
expectedNote: models.Note{
ID: 1,
Content: "Test note",
LastUpdate: time.Now(),
},
expectedError: false,
}, {
name: "new 2 note",
note: models.Note{
Content: "Test note 2!",
LastUpdate: time.Now(),
},
expectedNote: models.Note{
ID: 2,
Content: "Test note 2!",
LastUpdate: time.Now(),
},
expectedError: false,
},
{
name: "update note",
note: models.Note{
ID: 1,
Content: "Updated note",
LastUpdate: time.Now(),
},
expectedNote: models.Note{
ID: 1,
Content: "Updated note",
LastUpdate: time.Now(),
},
expectedError: false,
},
{
name: "update non-existent note",
note: models.Note{
ID: 999,
Content: "This note does not exist",
LastUpdate: time.Now(),
},
expectedNote: models.Note{},
expectedError: true,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
savedNote, err := store.SaveNote(context.Background(), tc.note)
if tc.expectedError {
if err == nil {
t.Errorf("expected an error but got none")
}
}
if !tc.expectedError {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if savedNote.Content != tc.note.Content {
t.Errorf("expected content %q but got %q", tc.note.Content, savedNote.Content)
}
}
})
}
}