Files
gotes/internal/repository/notes_test.go
2026-03-17 21:41:56 +00:00

268 lines
7.8 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,
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), //LastUpdate 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,
LastUpdate: time.Date(2000, 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 {
t.Run(tc.name, func(t *testing.T) {
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.LastUpdate.Equal(tc.expectedNote.LastUpdate) {
t.Errorf("expected LastUpdate %v but got %v", tc.expectedNote.LastUpdate, note.LastUpdate)
}
fmt.Printf("Test case '%s' passed.\n", tc.name)
})
})
}
}
func TestGetNotes(t *testing.T) {
notes := []models.Note{
{ID: 1, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "First note"},
{ID: 2, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Second note"},
{ID: 3, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Third note"},
// 4th note was clearly deleted, :sadface:
{ID: 5, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Lorem ipsum note dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."},
}
store := &mockNoteStore{Notes: notes}
repo := repository.NewNoteRepository(store)
testCases := []struct {
name string
id int
expectedNote models.Note
expectedError bool
}{
{
name: "Existing note",
id: 1,
expectedNote: notes[0],
expectedError: false,
},
{
name: "Non-existing note",
id: 999,
expectedNote: models.Note{},
expectedError: true,
},
{
name: "Secret missing 4th note",
id: 4,
expectedNote: models.Note{},
expectedError: true,
},
{
name: "That long lorem ipsum note",
id: 5,
expectedNote: notes[3],
expectedError: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
note, err := repo.GetNote(tc.id)
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)
}
// Purposefully not checking time as it doesn't make sense in context
})
}
}
func TestListNotes(t *testing.T) {
testcases := []struct {
name string
notes []models.Note
expectedNotes []models.Note
expectedError bool
}{
{
name: "No notes",
notes: []models.Note{},
expectedNotes: []models.Note{},
expectedError: false,
},
{
name: "Multiple notes",
notes: []models.Note{
{ID: 1, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "First note"},
{ID: 2, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Second note"},
{ID: 3, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Third note"},
},
expectedNotes: []models.Note{
{ID: 1, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "First note"},
{ID: 2, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Second note"},
{ID: 3, LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Content: "Third note"},
},
expectedError: false,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
store := &mockNoteStore{Notes: tc.notes}
repo := repository.NewNoteRepository(store)
notes, err := repo.ListNotes()
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 len(notes) != len(tc.expectedNotes) {
t.Errorf("expected %d notes but got %d", len(tc.expectedNotes), len(notes))
}
for i, note := range notes {
if note.Content != tc.expectedNotes[i].Content {
t.Errorf("expected content %q but got %q", tc.expectedNotes[i].Content, note.Content)
}
if note.ID != tc.expectedNotes[i].ID {
t.Errorf("expected ID %d but got %d", tc.expectedNotes[i].ID, note.ID)
}
// Purposefully not checking time as it doesn't make sense in context
}
})
}
}
func TestUpdateNote(t *testing.T) {
notes := []models.Note{
{ID: 1, LastUpdate: time.Date(1984, 11, 20, 0, 0, 0, 0, time.UTC), Content: "First note"},
{ID: 2, LastUpdate: time.Date(2018, 6, 8, 0, 0, 0, 0, time.UTC), Content: "Second note"},
}
store := &mockNoteStore{Notes: notes}
repo := repository.NewNoteRepository(store)
testcases := []struct {
name string
id int
content string
expectedNote models.Note
expectedError bool
}{
{
name: "Update existing note",
id: 1,
content: "Updated first note",
expectedNote: models.Note{
ID: 1,
LastUpdate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
Content: "Updated first note",
},
expectedError: false,
},
{
name: "Update non-existing note",
id: 999,
content: "This note does not exist",
expectedNote: models.Note{},
expectedError: true,
},
{
name: "Update with empty content",
id: 2,
content: "",
expectedNote: models.Note{},
expectedError: true,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
note, err := repo.UpdateNote(tc.id, 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.LastUpdate.Equal(tc.expectedNote.LastUpdate) {
t.Errorf("expected LastUpdate %v but got %v", tc.expectedNote.LastUpdate, note.LastUpdate)
}
fmt.Printf("Test case '%s' passed.\n", tc.name)
})
})
}
}