Test deleting the notes
This commit is contained in:
@@ -20,7 +20,7 @@ func (r *NoteRepository) CreateNote(content string) (models.Note, error) {
|
|||||||
return models.Note{}, fmt.Errorf("content cannot be empty")
|
return models.Note{}, fmt.Errorf("content cannot be empty")
|
||||||
}
|
}
|
||||||
note := models.Note{
|
note := models.Note{
|
||||||
Content: content,
|
Content: content,
|
||||||
LastUpdate: time.Now(),
|
LastUpdate: time.Now(),
|
||||||
}
|
}
|
||||||
return r.store.SaveNote(note)
|
return r.store.SaveNote(note)
|
||||||
@@ -46,3 +46,7 @@ func (r *NoteRepository) UpdateNote(id int, content string) (models.Note, error)
|
|||||||
note.LastUpdate = time.Now()
|
note.LastUpdate = time.Now()
|
||||||
return r.store.SaveNote(note)
|
return r.store.SaveNote(note)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *NoteRepository) DeleteNote(id int) error {
|
||||||
|
return fmt.Errorf("not implemented")
|
||||||
|
}
|
||||||
|
|||||||
@@ -265,3 +265,72 @@ func TestUpdateNote(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeleteNote(t *testing.T) {
|
||||||
|
testcases := []struct {
|
||||||
|
name string
|
||||||
|
id int
|
||||||
|
setupNotes []models.Note
|
||||||
|
expectedNotes []models.Note
|
||||||
|
expectedError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Delete existing note",
|
||||||
|
id: 1,
|
||||||
|
setupNotes: []models.Note{
|
||||||
|
{ID: 1, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "First note"},
|
||||||
|
{ID: 2, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Second note"},
|
||||||
|
},
|
||||||
|
expectedNotes: []models.Note{
|
||||||
|
{ID: 2, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Second note"},
|
||||||
|
},
|
||||||
|
expectedError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Delete non-existing note",
|
||||||
|
id: 999,
|
||||||
|
setupNotes: []models.Note{
|
||||||
|
{ID: 1, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "First note"},
|
||||||
|
{ID: 2, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Second note"},
|
||||||
|
},
|
||||||
|
expectedNotes: []models.Note{
|
||||||
|
{ID: 1, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "First note"},
|
||||||
|
{ID: 2, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Second note"},
|
||||||
|
},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range testcases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
store := &mockNoteStore{Notes: tc.setupNotes}
|
||||||
|
repo := repository.NewNoteRepository(store)
|
||||||
|
err := repo.DeleteNote(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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
notes, _ := repo.ListNotes()
|
||||||
|
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 len(tc.expectedNotes) <= i {
|
||||||
|
t.Errorf("unexpected extra note: %v", note)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user