diff --git a/internal/store/sqlite/sqlite.go b/internal/store/sqlite/sqlite.go index 3537f9b..cd2fc1f 100644 --- a/internal/store/sqlite/sqlite.go +++ b/internal/store/sqlite/sqlite.go @@ -128,6 +128,10 @@ func (s *SQLiteStore) GetNoteByID(ctx context.Context, id int) (models.Note, err return note, nil } +func (s *SQLiteStore) GetAllNotes(ctx context.Context) ([]models.Note, error) { + return nil, fmt.Errorf("not implemented") +} + func (s *SQLiteStore) validateSchema(ctx context.Context) error { _, err := s.write.ExecContext(ctx, ` CREATE TABLE IF NOT EXISTS notes ( @@ -154,4 +158,4 @@ func (s *SQLiteStore) scanNote(results *sql.Row) (models.Note, error) { return models.Note{}, fmt.Errorf("failed to scan note: %w", err) } return note, nil -} \ No newline at end of file +} diff --git a/internal/store/sqlite/sqlite_test.go b/internal/store/sqlite/sqlite_test.go index 8fd1d55..6842697 100644 --- a/internal/store/sqlite/sqlite_test.go +++ b/internal/store/sqlite/sqlite_test.go @@ -167,7 +167,7 @@ func TestGetNoteByID(t *testing.T) { } }() store.SaveNote(context.Background(), models.Note{ - Content: "Test note", + Content: "Test note", LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), }) @@ -212,3 +212,97 @@ func TestGetNoteByID(t *testing.T) { }) } } + +func TestGetAllNotes(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 + expectedNotes []models.Note + expectedError bool + }{ + { + name: "get all notes when empty", + expectedNotes: []models.Note{}, + expectedError: false, + }, + } + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + notes, err := store.GetAllNotes(context.Background()) + 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 len(notes) != len(tc.expectedNotes) { + t.Errorf("expected %d notes but got %d", len(tc.expectedNotes), len(notes)) + } + } + }) + } + + 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, 2, 0, 0, 0, 0, time.UTC), + }) + + testcases = []struct { + name string + expectedNotes []models.Note + expectedError bool + }{ + { + name: "get all notes when not empty", + expectedNotes: []models.Note{ + { + ID: 1, + Content: "Test note 1", + LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + ID: 2, + Content: "Test note 2", + LastUpdate: time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), + }, + }, + expectedError: false, + }, + } + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + notes, err := store.GetAllNotes(context.Background()) + 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 len(notes) != len(tc.expectedNotes) { + t.Errorf("expected %d notes but got %d", len(tc.expectedNotes), len(notes)) + } + } + }) + } +}