Files
mtusi2026lab2/internal/storage/memory/store_test.go
T
2026-10-01 12:59:06 +03:00

176 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package memory
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"mtusi-messenger/internal/domain"
)
type entry struct {
createdAt time.Time
id uuid.UUID
}
func entryKey(e entry) (time.Time, uuid.UUID) {
return e.createdAt, e.id
}
func TestCompareKeyset(t *testing.T) {
t.Parallel()
earlier := time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC)
later := earlier.Add(time.Second)
lowID := uuid.MustParse("11111111-1111-4111-8111-111111111111")
highID := uuid.MustParse("22222222-2222-4222-8222-222222222222")
tests := map[string]struct {
a entry
b entry
want int
}{
"время решает": {
a: entry{createdAt: earlier, id: highID},
b: entry{createdAt: later, id: lowID},
want: -1,
},
"при равном времени решает id": {
a: entry{createdAt: earlier, id: lowID},
b: entry{createdAt: earlier, id: highID},
want: -1,
},
"полное совпадение": {
a: entry{createdAt: earlier, id: lowID},
b: entry{createdAt: earlier, id: lowID},
want: 0,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, compareKeyset(tt.a.createdAt, tt.a.id, tt.b.createdAt, tt.b.id))
assert.Equal(t, -tt.want, compareKeyset(tt.b.createdAt, tt.b.id, tt.a.createdAt, tt.a.id),
"сравнение должно быть антисимметричным")
})
}
}
func makeEntries(n int) []entry {
base := time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC)
items := make([]entry, 0, n)
for i := range n {
items = append(items, entry{
createdAt: base.Add(time.Duration(i) * time.Second),
id: uuid.New(),
})
}
return items
}
func TestPaginateFirstPage(t *testing.T) {
t.Parallel()
scope := uuid.New()
items := makeEntries(5)
page := paginate(items, scope, entryKey, nil, 2)
require.Len(t, page.Items, 2)
assert.Equal(t, items[:2], page.Items)
require.NotNil(t, page.Next)
assert.Equal(t, items[1].id, page.Next.ID)
assert.Equal(t, scope, page.Next.Scope, "курсор должен нести привязку к последовательности")
}
func TestPaginateLastPageHasNoCursor(t *testing.T) {
t.Parallel()
items := makeEntries(3)
page := paginate(items, uuid.New(), entryKey, nil, 3)
assert.Equal(t, items, page.Items)
assert.Nil(t, page.Next, "на последней странице next_cursor равен null")
}
func TestPaginateEmptyResultIsNotNil(t *testing.T) {
t.Parallel()
page := paginate([]entry{}, uuid.New(), entryKey, nil, 10)
assert.NotNil(t, page.Items, `контракт требует "items": [], а не null`)
assert.Empty(t, page.Items)
assert.Nil(t, page.Next)
}
func TestPaginateWalkWithoutDuplicates(t *testing.T) {
t.Parallel()
const (
total = 10
limit = 3
)
scope := uuid.New()
items := makeEntries(total)
var (
seen []entry
cursor *domain.Cursor
)
for range total {
page := paginate(items, scope, entryKey, cursor, limit)
seen = append(seen, page.Items...)
if page.Next == nil {
break
}
cursor = page.Next
}
assert.Equal(t, items, seen)
}
func TestPaginateCursorBeyondEnd(t *testing.T) {
t.Parallel()
scope := uuid.New()
items := makeEntries(3)
last := items[len(items)-1]
page := paginate(items, scope, entryKey, &domain.Cursor{
Scope: scope,
CreatedAt: last.createdAt,
ID: last.id,
}, 10)
assert.Empty(t, page.Items)
assert.Nil(t, page.Next)
}
func TestPaginateChangedLimitOnContinuation(t *testing.T) {
t.Parallel()
scope := uuid.New()
items := makeEntries(6)
first := paginate(items, scope, entryKey, nil, 2)
require.NotNil(t, first.Next)
second := paginate(items, scope, entryKey, first.Next, 4)
assert.Equal(t, items[2:], second.Items)
assert.Nil(t, second.Next, "изменение limit при продолжении допустимо")
}