From 4fc10c8a5bd552425675c187ca94a5e4ff7c7452 Mon Sep 17 00:00:00 2001 From: Micheal Wilkinson Date: Tue, 17 Mar 2026 23:30:21 +0000 Subject: [PATCH] Adding tests for deleting notes --- internal/store/sqlite/sqlite.go | 4 ++ internal/store/sqlite/sqlite_test.go | 65 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/internal/store/sqlite/sqlite.go b/internal/store/sqlite/sqlite.go index 414086a..b01f269 100644 --- a/internal/store/sqlite/sqlite.go +++ b/internal/store/sqlite/sqlite.go @@ -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 ( diff --git a/internal/store/sqlite/sqlite_test.go b/internal/store/sqlite/sqlite_test.go index 6842697..fee3d1d 100644 --- a/internal/store/sqlite/sqlite_test.go +++ b/internal/store/sqlite/sqlite_test.go @@ -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") + } + } + } + }) + } +}