Adding tests for getting notes by id

This commit is contained in:
Micheal Wilkinson
2026-03-17 23:14:09 +00:00
parent 48bf71c5ec
commit bdc7b5b1b6
2 changed files with 66 additions and 1 deletions

View File

@@ -151,3 +151,64 @@ func TestSaveNote(t *testing.T) {
})
}
}
func TestGetNoteByID(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)
}
}
}()
store.SaveNote(context.Background(), models.Note{
Content: "Test note",
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
})
testcases := []struct {
name string
id int
expectedNote models.Note
expectedError bool
}{
{
name: "get existing note",
id: 1,
expectedNote: models.Note{
ID: 1,
Content: "Test note",
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
},
expectedError: false,
},
{
name: "get non-existent note",
id: 999,
expectedNote: models.Note{},
expectedError: true,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
note, err := store.GetNoteByID(context.Background(), tc.id)
if tc.expectedError {
if err == nil {
t.Errorf("expected an error but got none")
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if note.ID != tc.expectedNote.ID || note.Content != tc.expectedNote.Content || !note.LastUpdate.Equal(tc.expectedNote.LastUpdate) {
t.Errorf("expected note %+v but got %+v", tc.expectedNote, note)
}
}
})
}
}