Files
gotes/internal/repository/notes_test.go

75 lines
2.0 KiB
Go

package repository_test
import (
"testing"
"testing/synctest"
"fmt"
"time"
"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)
})
}
}