Adding some testing around updating notes
This commit is contained in:
@@ -33,3 +33,7 @@ func (r *NoteRepository) GetNote(id int) (models.Note, error) {
|
|||||||
func (r *NoteRepository) ListNotes() ([]models.Note, error) {
|
func (r *NoteRepository) ListNotes() ([]models.Note, error) {
|
||||||
return r.store.GetAllNotes()
|
return r.store.GetAllNotes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *NoteRepository) UpdateNote(id int, content string) (models.Note, error) {
|
||||||
|
return models.Note{}, fmt.Errorf("not implemented")
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,18 +11,36 @@ type mockNoteStore struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockNoteStore) SaveNote(note models.Note) (models.Note, error) {
|
func (m *mockNoteStore) SaveNote(note models.Note) (models.Note, error) {
|
||||||
|
// Save a note
|
||||||
|
if note.ID == 0 {
|
||||||
note.ID = len(m.Notes) + 1
|
note.ID = len(m.Notes) + 1
|
||||||
m.Notes = append(m.Notes, note)
|
m.Notes = append(m.Notes, note)
|
||||||
return note, nil
|
return note, nil
|
||||||
|
}
|
||||||
|
// Update a note
|
||||||
|
_, index, err := m.getNoteAndIndexByID(note.ID)
|
||||||
|
if err != nil {
|
||||||
|
return models.Note{}, err
|
||||||
|
}
|
||||||
|
m.Notes[index] = note
|
||||||
|
return note, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockNoteStore) GetNoteByID(id int) (models.Note, error) {
|
func (m *mockNoteStore) GetNoteByID(id int) (models.Note, error) {
|
||||||
for _, note := range m.Notes {
|
note, _, err := m.getNoteAndIndexByID(id)
|
||||||
if note.ID == id {
|
if err != nil {
|
||||||
|
return models.Note{}, err
|
||||||
|
}
|
||||||
return note, nil
|
return note, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockNoteStore) getNoteAndIndexByID(id int) (models.Note, int, error) {
|
||||||
|
for i, note := range m.Notes {
|
||||||
|
if note.ID == id {
|
||||||
|
return note, i, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return models.Note{}, fmt.Errorf("note with ID %d not found", id)
|
return models.Note{}, -1, fmt.Errorf("note with ID %d not found", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockNoteStore) GetAllNotes() ([]models.Note, error) {
|
func (m *mockNoteStore) GetAllNotes() ([]models.Note, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user