68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"RenewCMS/internal/domain/image"
|
|
)
|
|
|
|
func sortedIDs(order image.Sort, imgs []image.Image) []uint32 {
|
|
image.SortImages(imgs, order)
|
|
ids := make([]uint32, len(imgs))
|
|
for i, img := range imgs {
|
|
ids[i] = img.ID
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func equalIDs(a, b []uint32) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func TestImageSortTieBreaks(t *testing.T) {
|
|
now := time.Now()
|
|
build := func() []image.Image {
|
|
return []image.Image{
|
|
{ID: 2, Filename: "a.png", Size: 10, CreatedAt: now},
|
|
{ID: 1, Filename: "a.png", Size: 10, CreatedAt: now},
|
|
{ID: 3, Filename: "b.png", Size: 20, CreatedAt: now.Add(time.Hour)},
|
|
}
|
|
}
|
|
cases := []struct {
|
|
order image.Sort
|
|
want []uint32
|
|
}{
|
|
{image.SortOldest, []uint32{1, 2, 3}},
|
|
{image.SortNewest, []uint32{3, 2, 1}},
|
|
{image.SortName, []uint32{1, 2, 3}},
|
|
{image.SortLargest, []uint32{3, 1, 2}},
|
|
}
|
|
for _, c := range cases {
|
|
if got := sortedIDs(c.order, build()); !equalIDs(got, c.want) {
|
|
t.Errorf("%s: got %v want %v", c.order, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestImageSortByTotalSizeTieBreak(t *testing.T) {
|
|
imgs := []image.Image{
|
|
{ID: 2, Path: "/u/x.png", Size: 10},
|
|
{ID: 1, Path: "/u/y.png", Size: 10},
|
|
{ID: 3, Path: "/u/z.png", Size: 11},
|
|
}
|
|
image.SortImagesByTotalSize(imgs, nil)
|
|
got := []uint32{imgs[0].ID, imgs[1].ID, imgs[2].ID}
|
|
if !equalIDs(got, []uint32{3, 1, 2}) {
|
|
t.Fatalf("got %v", got)
|
|
}
|
|
}
|