Test deleting the notes

This commit is contained in:
Micheal Wilkinson
2026-03-17 21:47:44 +00:00
parent c97afbd95d
commit 0d2cb5efd9
2 changed files with 74 additions and 1 deletions

View File

@@ -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
}
})
}
}