Adding some testing around updating notes
This commit is contained in:
@@ -53,7 +53,7 @@ func TestCreateNote(t *testing.T) {
|
||||
if tc.expectedError {
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but got none")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
@@ -197,3 +197,71 @@ func TestListNotes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateNote(t *testing.T) {
|
||||
notes := []models.Note{
|
||||
{ID: 1, LastUpdate: time.Date(1984, 11, 20, 0, 0, 0, 0, time.UTC), Content: "First note"},
|
||||
{ID: 2, LastUpdate: time.Date(2018, 6, 8, 0, 0, 0, 0, time.UTC), Content: "Second note"},
|
||||
}
|
||||
store := &mockNoteStore{Notes: notes}
|
||||
repo := repository.NewNoteRepository(store)
|
||||
testcases := []struct {
|
||||
name string
|
||||
id int
|
||||
content string
|
||||
expectedNote models.Note
|
||||
expectedError bool
|
||||
}{
|
||||
{
|
||||
name: "Update existing note",
|
||||
id: 1,
|
||||
content: "Updated first note",
|
||||
expectedNote: models.Note{
|
||||
ID: 1,
|
||||
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
Content: "Updated first note",
|
||||
},
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "Update non-existing note",
|
||||
id: 999,
|
||||
content: "This note does not exist",
|
||||
expectedNote: models.Note{},
|
||||
expectedError: true,
|
||||
},
|
||||
{
|
||||
name: "Update with empty content",
|
||||
id: 2,
|
||||
content: "",
|
||||
expectedNote: models.Note{},
|
||||
expectedError: true,
|
||||
},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
note, err := repo.UpdateNote(tc.id, tc.content)
|
||||
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 note.Content != tc.expectedNote.Content {
|
||||
t.Errorf("expected content %q but got %q", tc.expectedNote.Content, note.Content)
|
||||
}
|
||||
if note.ID != tc.expectedNote.ID {
|
||||
t.Errorf("expected ID %d but got %d", tc.expectedNote.ID, note.ID)
|
||||
}
|
||||
if !note.LastUpdate.Equal(tc.expectedNote.LastUpdate) {
|
||||
t.Errorf("expected LastUpdate %v but got %v", tc.expectedNote.LastUpdate, note.LastUpdate)
|
||||
}
|
||||
fmt.Printf("Test case '%s' passed.\n", tc.name)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user