Added NoteStore interface, mock and repository with Create method
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
)
|
||||
|
||||
type Note struct {
|
||||
ID int `json:"id"`
|
||||
ID int `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
@@ -16,4 +16,4 @@ func NewNoteRepository(store NoteStore) *NoteRepository {
|
||||
|
||||
func (r *NoteRepository) CreateNote(content string) (models.Note, error) {
|
||||
return models.Note{}, fmt.Errorf("not implemented")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
|
||||
"time"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.hrafn.xyz/aether/notes/internal/models"
|
||||
"git.hrafn.xyz/aether/notes/internal/repository"
|
||||
@@ -13,13 +13,13 @@ import (
|
||||
|
||||
func TestCreateNote(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
content string
|
||||
expectedNote models.Note
|
||||
name string
|
||||
content string
|
||||
expectedNote models.Note
|
||||
expectedError bool
|
||||
}{
|
||||
{
|
||||
name: "Good note",
|
||||
name: "Good note",
|
||||
content: "This is a good note with valid content.",
|
||||
expectedNote: models.Note{
|
||||
ID: 1,
|
||||
@@ -29,13 +29,13 @@ func TestCreateNote(t *testing.T) {
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "Empty content",
|
||||
content: "",
|
||||
expectedNote: models.Note{},
|
||||
name: "Empty content",
|
||||
content: "",
|
||||
expectedNote: models.Note{},
|
||||
expectedError: true,
|
||||
},
|
||||
{
|
||||
name: "Long content",
|
||||
name: "Long content",
|
||||
content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
|
||||
expectedNote: models.Note{
|
||||
ID: 2,
|
||||
@@ -46,29 +46,29 @@ func TestCreateNote(t *testing.T) {
|
||||
},
|
||||
}
|
||||
repo := repository.NewNoteRepository(&mockNoteStore{})
|
||||
for _, tc := range testCases {
|
||||
for _, tc := range testCases {
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
note, err := repo.CreateNote(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)
|
||||
}
|
||||
note, err := repo.CreateNote(tc.content)
|
||||
if tc.expectedError {
|
||||
if err == nil {
|
||||
t.Errorf("expected an error but got none")
|
||||
}
|
||||
if note.Content != tc.expectedNote.Content {
|
||||
t.Errorf("expected content %q but got %q", tc.expectedNote.Content, note.Content)
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if note.ID != tc.expectedNote.ID {
|
||||
t.Errorf("expected ID %d but got %d", tc.expectedNote.ID, note.ID)
|
||||
}
|
||||
if !note.CreatedAt.Equal(tc.expectedNote.CreatedAt) {
|
||||
t.Errorf("expected CreatedAt %v but got %v", tc.expectedNote.CreatedAt, note.CreatedAt)
|
||||
}
|
||||
fmt.Printf("Test case '%s' passed.\n", tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
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.CreatedAt.Equal(tc.expectedNote.CreatedAt) {
|
||||
t.Errorf("expected CreatedAt %v but got %v", tc.expectedNote.CreatedAt, note.CreatedAt)
|
||||
}
|
||||
fmt.Printf("Test case '%s' passed.\n", tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"git.hrafn.xyz/aether/notes/internal/models"
|
||||
)
|
||||
|
||||
type NoteStore interface {
|
||||
SaveNote(models.Note) (models.Note, error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@ import (
|
||||
)
|
||||
|
||||
type mockNoteStore struct {
|
||||
notes []models.Note
|
||||
Notes []models.Note
|
||||
}
|
||||
|
||||
func (m *mockNoteStore) SaveNote(note models.Note) (models.Note, error) {
|
||||
note.ID = len(m.notes) + 1
|
||||
m.notes = append(m.notes, note)
|
||||
note.ID = len(m.Notes) + 1
|
||||
m.Notes = append(m.Notes, note)
|
||||
return note, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user