Test Note Retrieval
This commit is contained in:
@@ -23,3 +23,7 @@ func (r *NoteRepository) CreateNote(content string) (models.Note, error) {
|
|||||||
}
|
}
|
||||||
return r.store.SaveNote(note)
|
return r.store.SaveNote(note)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *NoteRepository) GetNote(id int) (models.Note, error) {
|
||||||
|
return models.Note{}, fmt.Errorf("not implemented")
|
||||||
|
}
|
||||||
|
|||||||
@@ -72,3 +72,69 @@ func TestCreateNote(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetNotes(t *testing.T) {
|
||||||
|
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"},
|
||||||
|
// 4th note was clearly deleted, :sadface:
|
||||||
|
{ID: 5, CreatedAt: time.Date(0001, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Lorem ipsum note dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."},
|
||||||
|
}
|
||||||
|
store := &mockNoteStore{Notes: notes}
|
||||||
|
repo := repository.NewNoteRepository(store)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
id int
|
||||||
|
expectedNote models.Note
|
||||||
|
expectedError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Existing note",
|
||||||
|
id: 1,
|
||||||
|
expectedNote: notes[0],
|
||||||
|
expectedError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Non-existing note",
|
||||||
|
id: 999,
|
||||||
|
expectedNote: models.Note{},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Secret missing 4th note",
|
||||||
|
id: 4,
|
||||||
|
expectedNote: models.Note{},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "That long lorem ipsum note",
|
||||||
|
id: 5,
|
||||||
|
expectedNote: notes[3],
|
||||||
|
expectedError: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
note, err := repo.GetNote(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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
// Purposefully not checking time as it doesn't make sense in context
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ import (
|
|||||||
|
|
||||||
type NoteStore interface {
|
type NoteStore interface {
|
||||||
SaveNote(models.Note) (models.Note, error)
|
SaveNote(models.Note) (models.Note, error)
|
||||||
|
GetNoteByID(int) (models.Note, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package repository_test
|
package repository_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"git.hrafn.xyz/aether/notes/internal/models"
|
"git.hrafn.xyz/aether/notes/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,3 +15,12 @@ func (m *mockNoteStore) SaveNote(note models.Note) (models.Note, error) {
|
|||||||
m.Notes = append(m.Notes, note)
|
m.Notes = append(m.Notes, note)
|
||||||
return note, nil
|
return note, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mockNoteStore) GetNoteByID(id int) (models.Note, error) {
|
||||||
|
for _, note := range m.Notes {
|
||||||
|
if note.ID == id {
|
||||||
|
return note, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return models.Note{}, fmt.Errorf("note with ID %d not found", id)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user