Testing List notes
This commit is contained in:
@@ -27,3 +27,7 @@ func (r *NoteRepository) CreateNote(content string) (models.Note, error) {
|
||||
func (r *NoteRepository) GetNote(id int) (models.Note, error) {
|
||||
return r.store.GetNoteByID(id)
|
||||
}
|
||||
|
||||
func (r *NoteRepository) ListNotes() ([]models.Note, error) {
|
||||
return []models.Note{}, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
@@ -138,3 +138,61 @@ func TestGetNotes(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestListNotes(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
notes []models.Note
|
||||
expectedNotes []models.Note
|
||||
expectedError bool
|
||||
}{
|
||||
{
|
||||
name: "No notes",
|
||||
notes: []models.Note{},
|
||||
expectedNotes: []models.Note{},
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "Multiple notes",
|
||||
notes: []models.Note{
|
||||
{ID: 1, CreatedAt: time.Date(0001, 1, 1, 0, 0, 0, 0, time.UTC), Content: "First note"},
|
||||
{ID: 2, CreatedAt: time.Date(0001, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Second note"},
|
||||
{ID: 3, CreatedAt: time.Date(0001, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Third note"},
|
||||
},
|
||||
expectedNotes: []models.Note{
|
||||
{ID: 1, CreatedAt: time.Date(0001, 1, 1, 0, 0, 0, 0, time.UTC), Content: "First note"},
|
||||
{ID: 2, CreatedAt: time.Date(0001, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Second note"},
|
||||
{ID: 3, CreatedAt: time.Date(0001, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Third note"},
|
||||
},
|
||||
expectedError: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store := &mockNoteStore{Notes: tc.notes}
|
||||
repo := repository.NewNoteRepository(store)
|
||||
notes, err := repo.ListNotes()
|
||||
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))
|
||||
}
|
||||
for i, note := range notes {
|
||||
if note.Content != tc.expectedNotes[i].Content {
|
||||
t.Errorf("expected content %q but got %q", tc.expectedNotes[i].Content, note.Content)
|
||||
}
|
||||
if note.ID != tc.expectedNotes[i].ID {
|
||||
t.Errorf("expected ID %d but got %d", tc.expectedNotes[i].ID, note.ID)
|
||||
}
|
||||
// Purposefully not checking time as it doesn't make sense in context
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,5 @@ import (
|
||||
type NoteStore interface {
|
||||
SaveNote(models.Note) (models.Note, error)
|
||||
GetNoteByID(int) (models.Note, error)
|
||||
GetAllNotes() ([]models.Note, error)
|
||||
}
|
||||
|
||||
@@ -24,3 +24,7 @@ func (m *mockNoteStore) GetNoteByID(id int) (models.Note, error) {
|
||||
}
|
||||
return models.Note{}, fmt.Errorf("note with ID %d not found", id)
|
||||
}
|
||||
|
||||
func (m *mockNoteStore) GetAllNotes() ([]models.Note, error) {
|
||||
return m.Notes, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user