aboutsummaryrefslogtreecommitdiff
path: root/internal/gallery/query_photos_test.go
blob: 45e7149ef54c82dce2aa3bfde9323b6bf14f7565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package gallery

import (
	"strconv"
	"testing"
)

func makePhotos(n int) []Photo {
	photos := make([]Photo, n)

	for i := range n {
		photos[i] = Photo{
			ID: strconv.Itoa(i),
		}
	}

	return photos
}

func assertIDs(
	t *testing.T,
	photos []Photo,
	want ...string,
) {
	t.Helper()

	if len(photos) != len(want) {
		t.Fatalf(
			"expected %d photos, got %d",
			len(want),
			len(photos),
		)
	}

	for i := range want {
		if photos[i].ID != want[i] {
			t.Fatalf(
				"photo %d: expected %q, got %q",
				i,
				want[i],
				photos[i].ID,
			)
		}
	}
}

func TestQueryPhotos(t *testing.T) {
	photos := makePhotos(10)

	t.Run("default", func(t *testing.T) {
		got := QueryPhotos(photos, Query{})

		assertIDs(
			t,
			got,
			"0", "1", "2", "3", "4",
			"5", "6", "7", "8", "9",
		)
	})

	t.Run("limit", func(t *testing.T) {
		got := QueryPhotos(
			photos,
			Query{
				Limit: 3,
			},
		)

		assertIDs(t, got, "0", "1", "2")
	})

	t.Run("offset", func(t *testing.T) {
		got := QueryPhotos(
			photos,
			Query{
				Offset: 5,
			},
		)

		assertIDs(
			t,
			got,
			"5", "6", "7", "8", "9",
		)
	})

	t.Run("offset and limit", func(t *testing.T) {
		got := QueryPhotos(
			photos,
			Query{
				Offset: 5,
				Limit:  2,
			},
		)

		assertIDs(t, got, "5", "6")
	})

	t.Run("offset beyond end", func(t *testing.T) {
		got := QueryPhotos(
			photos,
			Query{
				Offset: 100,
			},
		)

		assertIDs(t, got)
	})

	t.Run("limit larger than gallery", func(t *testing.T) {
		got := QueryPhotos(
			photos,
			Query{
				Limit: 100,
			},
		)

		assertIDs(
			t,
			got,
			"0", "1", "2", "3", "4",
			"5", "6", "7", "8", "9",
		)
	})
}