Adding tests for deleting notes

This commit is contained in:
Micheal Wilkinson
2026-03-17 23:30:21 +00:00
parent c453f1582a
commit 4fc10c8a5b
2 changed files with 69 additions and 0 deletions

View File

@@ -155,6 +155,10 @@ func (s *SQLiteStore) GetAllNotes(ctx context.Context) ([]models.Note, error) {
return notes, nil
}
func (s *SQLiteStore) DeleteNoteByID(ctx context.Context, id int) error {
return fmt.Errorf("not implemented")
}
func (s *SQLiteStore) validateSchema(ctx context.Context) error {
_, err := s.write.ExecContext(ctx, `
CREATE TABLE IF NOT EXISTS notes (

View File

@@ -306,3 +306,68 @@ func TestGetAllNotes(t *testing.T) {
})
}
}
func TestDeleteNoteByID(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 1",
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
})
store.SaveNote(context.Background(), models.Note{
Content: "Test note 2",
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
})
store.SaveNote(context.Background(), models.Note{
Content: "Test note 3",
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
})
testcases := []struct {
name string
id int
expectedError bool
}{
{
name: "delete existing note",
id: 2,
expectedError: false,
},
{
name: "delete non-existent note",
id: 999,
expectedError: true,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
err := store.DeleteNoteByID(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)
} else {
_, err := store.GetNoteByID(context.Background(), tc.id)
if err == nil {
t.Errorf("expected an error when retrieving deleted note but got none")
}
}
}
})
}
}