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