Added NoteStore interface, mock and repository with Create method
This commit is contained in:
19
internal/repository/notes.go
Normal file
19
internal/repository/notes.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.hrafn.xyz/aether/notes/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NoteRepository struct {
|
||||||
|
store NoteStore
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNoteRepository(store NoteStore) *NoteRepository {
|
||||||
|
return &NoteRepository{store: store}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *NoteRepository) CreateNote(content string) (models.Note, error) {
|
||||||
|
return models.Note{}, fmt.Errorf("not implemented")
|
||||||
|
}
|
||||||
74
internal/repository/notes_test.go
Normal file
74
internal/repository/notes_test.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package repository_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"testing/synctest"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.hrafn.xyz/aether/notes/internal/models"
|
||||||
|
"git.hrafn.xyz/aether/notes/internal/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateNote(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
content string
|
||||||
|
expectedNote models.Note
|
||||||
|
expectedError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Good note",
|
||||||
|
content: "This is a good note with valid content.",
|
||||||
|
expectedNote: models.Note{
|
||||||
|
ID: 1,
|
||||||
|
CreatedAt: time.Date(0001, 1, 1, 0, 0, 0, 0, time.UTC), //CreatedAt is set by synctest to this specific time
|
||||||
|
Content: "This is a good note with valid content.",
|
||||||
|
},
|
||||||
|
expectedError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Empty content",
|
||||||
|
content: "",
|
||||||
|
expectedNote: models.Note{},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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,
|
||||||
|
CreatedAt: time.Date(0001, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
Content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
|
||||||
|
},
|
||||||
|
expectedError: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
repo := repository.NewNoteRepository(&mockNoteStore{})
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
8
internal/repository/notestore.go
Normal file
8
internal/repository/notestore.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.hrafn.xyz/aether/notes/internal/models"
|
||||||
|
)
|
||||||
|
type NoteStore interface {
|
||||||
|
SaveNote(models.Note) (models.Note, error)
|
||||||
|
}
|
||||||
15
internal/repository/notestore_test.go
Normal file
15
internal/repository/notestore_test.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package repository_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.hrafn.xyz/aether/notes/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mockNoteStore struct {
|
||||||
|
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)
|
||||||
|
return note, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user