Added NoteStore interface, mock and repository with Create method

This commit is contained in:
Micheal Wilkinson
2026-03-17 21:13:11 +00:00
parent c05603d624
commit 2f035ee485
6 changed files with 42 additions and 41 deletions

View File

@@ -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"`
} }

View File

@@ -16,4 +16,4 @@ func NewNoteRepository(store NoteStore) *NoteRepository {
func (r *NoteRepository) CreateNote(content string) (models.Note, error) { func (r *NoteRepository) CreateNote(content string) (models.Note, error) {
return models.Note{}, fmt.Errorf("not implemented") return models.Note{}, fmt.Errorf("not implemented")
} }

View File

@@ -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)
})
}
}

View File

@@ -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)
} }

View File

@@ -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
} }

View File

@@ -6,4 +6,4 @@ import (
func main() { func main() {
fmt.Println("Hello, World!") fmt.Println("Hello, World!")
} }