Debian Codegraph @

resources/page/pagegroup_test.go

raw
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package page
15
16 import (
17 "context"
18 "github.com/google/go-cmp/cmp"
19 "strings"
20 "testing"
21
22 qt "github.com/frankban/quicktest"
23 "github.com/spf13/cast"
24 )
25
26 type pageGroupTestObject struct {
27 path string
28 weight int
29 date string
30 param string
31 }
32
33 var pageGroupTestSources = []pageGroupTestObject{
34 {"/section1/testpage1.md", 3, "2012-04-06", "foo"},
35 {"/section1/testpage2.md", 3, "2012-01-01", "bar"},
36 {"/section1/testpage3.md", 2, "2012-04-06", "foo"},
37 {"/section2/testpage4.md", 1, "2012-03-02", "bar"},
38 // date might also be a full datetime:
39 {"/section2/testpage5.md", 1, "2012-04-06T00:00:00Z", "baz"},
40 }
41
42 func preparePageGroupTestPages(t *testing.T) Pages {
43 var pages Pages
44 for _, src := range pageGroupTestSources {
45 p := newTestPage()
46 p.path = src.path
47 if p.path != "" {
48 p.section = strings.Split(strings.TrimPrefix(p.path, "/"), "/")[0]
49 }
50 p.weight = src.weight
51 p.date = cast.ToTime(src.date)
52 p.pubDate = cast.ToTime(src.date)
53 p.expiryDate = cast.ToTime(src.date)
54 p.lastMod = cast.ToTime(src.date).AddDate(3, 0, 0)
55 p.params["custom_param"] = src.param
56 p.params["custom_date"] = cast.ToTime(src.date)
57 p.params["custom_string_date"] = src.date
58 p.params["custom_object"] = map[string]any{
59 "param": src.param,
60 "date": cast.ToTime(src.date),
61 "string_date": src.date,
62 }
63 pages = append(pages, p)
64 }
65 return pages
66 }
67
68 var comparePageGroup = qt.CmpEquals(cmp.Comparer(func(a, b Page) bool {
69 return a == b
70 }))
71
72 func TestGroupByWithFieldNameArg(t *testing.T) {
73 c := qt.New(t)
74
75 t.Parallel()
76 pages := preparePageGroupTestPages(t)
77 expect := PagesGroup{
78 {Key: 1, Pages: Pages{pages[3], pages[4]}},
79 {Key: 2, Pages: Pages{pages[2]}},
80 {Key: 3, Pages: Pages{pages[0], pages[1]}},
81 }
82
83 groups, err := pages.GroupBy(context.Background(), "Weight")
84 c.Assert(err, qt.IsNil)
85 c.Assert(groups, comparePageGroup, expect)
86 }
87
88 func TestGroupByWithMethodNameArg(t *testing.T) {
89 c := qt.New(t)
90
91 t.Parallel()
92 pages := preparePageGroupTestPages(t)
93 expect := PagesGroup{
94 {Key: "section1", Pages: Pages{pages[0], pages[1], pages[2]}},
95 {Key: "section2", Pages: Pages{pages[3], pages[4]}},
96 }
97
98 groups, err := pages.GroupBy(context.Background(), "Type")
99 c.Assert(err, qt.IsNil)
100 c.Assert(groups, comparePageGroup, expect)
101 }
102
103 func TestGroupByWithSectionArg(t *testing.T) {
104 c := qt.New(t)
105
106 t.Parallel()
107 pages := preparePageGroupTestPages(t)
108 expect := PagesGroup{
109 {Key: "section1", Pages: Pages{pages[0], pages[1], pages[2]}},
110 {Key: "section2", Pages: Pages{pages[3], pages[4]}},
111 }
112
113 groups, err := pages.GroupBy(context.Background(), "Section")
114 c.Assert(err, qt.IsNil)
115 c.Assert(groups, comparePageGroup, expect)
116 }
117
118 func TestGroupByInReverseOrder(t *testing.T) {
119 c := qt.New(t)
120
121 t.Parallel()
122 pages := preparePageGroupTestPages(t)
123 expect := PagesGroup{
124 {Key: 3, Pages: Pages{pages[0], pages[1]}},
125 {Key: 2, Pages: Pages{pages[2]}},
126 {Key: 1, Pages: Pages{pages[3], pages[4]}},
127 }
128
129 groups, err := pages.GroupBy(context.Background(), "Weight", "desc")
130 c.Assert(err, qt.IsNil)
131 c.Assert(groups, comparePageGroup, expect)
132 }
133
134 func TestGroupByCalledWithEmptyPages(t *testing.T) {
135 c := qt.New(t)
136
137 t.Parallel()
138 var pages Pages
139 groups, err := pages.GroupBy(context.Background(), "Weight")
140 c.Assert(err, qt.IsNil)
141 c.Assert(groups, qt.IsNil)
142 }
143
144 func TestReverse(t *testing.T) {
145 c := qt.New(t)
146
147 t.Parallel()
148 pages := preparePageGroupTestPages(t)
149
150 groups1, err := pages.GroupBy(context.Background(), "Weight", "desc")
151 c.Assert(err, qt.IsNil)
152
153 groups2, err := pages.GroupBy(context.Background(), "Weight")
154 c.Assert(err, qt.IsNil)
155
156 groups2 = groups2.Reverse()
157 c.Assert(groups2, comparePageGroup, groups1)
158 }
159
160 func TestGroupByParam(t *testing.T) {
161 c := qt.New(t)
162
163 t.Parallel()
164 pages := preparePageGroupTestPages(t)
165 expect := PagesGroup{
166 {Key: "bar", Pages: Pages{pages[1], pages[3]}},
167 {Key: "baz", Pages: Pages{pages[4]}},
168 {Key: "foo", Pages: Pages{pages[0], pages[2]}},
169 }
170
171 groups, err := pages.GroupByParam("custom_param")
172 c.Assert(err, qt.IsNil)
173 c.Assert(groups, comparePageGroup, expect)
174 }
175
176 func TestGroupByParamInReverseOrder(t *testing.T) {
177 c := qt.New(t)
178
179 t.Parallel()
180 pages := preparePageGroupTestPages(t)
181 expect := PagesGroup{
182 {Key: "foo", Pages: Pages{pages[0], pages[2]}},
183 {Key: "baz", Pages: Pages{pages[4]}},
184 {Key: "bar", Pages: Pages{pages[1], pages[3]}},
185 }
186
187 groups, err := pages.GroupByParam("custom_param", "desc")
188 c.Assert(err, qt.IsNil)
189 c.Assert(groups, comparePageGroup, expect)
190 }
191
192 func TestGroupByParamCalledWithCapitalLetterString(t *testing.T) {
193 c := qt.New(t)
194 testStr := "TestString"
195 p := newTestPage()
196 p.params["custom_param"] = testStr
197 pages := Pages{p}
198
199 groups, err := pages.GroupByParam("custom_param")
200
201 c.Assert(err, qt.IsNil)
202 c.Assert(groups[0].Key, qt.DeepEquals, testStr)
203 }
204
205 func TestGroupByParamCalledWithSomeUnavailableParams(t *testing.T) {
206 c := qt.New(t)
207
208 t.Parallel()
209 pages := preparePageGroupTestPages(t)
210 delete(pages[1].Params(), "custom_param")
211 delete(pages[3].Params(), "custom_param")
212 delete(pages[4].Params(), "custom_param")
213
214 expect := PagesGroup{
215 {Key: "foo", Pages: Pages{pages[0], pages[2]}},
216 }
217
218 groups, err := pages.GroupByParam("custom_param")
219 c.Assert(err, qt.IsNil)
220 c.Assert(groups, comparePageGroup, expect)
221 }
222
223 func TestGroupByParamCalledWithEmptyPages(t *testing.T) {
224 c := qt.New(t)
225
226 t.Parallel()
227 var pages Pages
228 groups, err := pages.GroupByParam("custom_param")
229 c.Assert(err, qt.IsNil)
230 c.Assert(groups, qt.IsNil)
231 }
232
233 func TestGroupByParamCalledWithUnavailableParam(t *testing.T) {
234 c := qt.New(t)
235
236 t.Parallel()
237 pages := preparePageGroupTestPages(t)
238 _, err := pages.GroupByParam("unavailable_param")
239 c.Assert(err, qt.IsNil)
240 }
241
242 func TestGroupByParamNested(t *testing.T) {
243 c := qt.New(t)
244
245 t.Parallel()
246 pages := preparePageGroupTestPages(t)
247
248 expect := PagesGroup{
249 {Key: "bar", Pages: Pages{pages[1], pages[3]}},
250 {Key: "baz", Pages: Pages{pages[4]}},
251 {Key: "foo", Pages: Pages{pages[0], pages[2]}},
252 }
253
254 groups, err := pages.GroupByParam("custom_object.param")
255 c.Assert(err, qt.IsNil)
256 c.Assert(groups, comparePageGroup, expect)
257 }
258
259 func TestGroupByDate(t *testing.T) {
260 c := qt.New(t)
261
262 t.Parallel()
263 pages := preparePageGroupTestPages(t)
264 expect := PagesGroup{
265 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
266 {Key: "2012-03", Pages: Pages{pages[3]}},
267 {Key: "2012-01", Pages: Pages{pages[1]}},
268 }
269
270 groups, err := pages.GroupByDate("2006-01")
271 c.Assert(err, qt.IsNil)
272 c.Assert(groups, comparePageGroup, expect)
273 }
274
275 func TestGroupByDateInReverseOrder(t *testing.T) {
276 c := qt.New(t)
277
278 t.Parallel()
279 pages := preparePageGroupTestPages(t)
280 expect := PagesGroup{
281 {Key: "2012-01", Pages: Pages{pages[1]}},
282 {Key: "2012-03", Pages: Pages{pages[3]}},
283 {Key: "2012-04", Pages: Pages{pages[0], pages[2], pages[4]}},
284 }
285
286 groups, err := pages.GroupByDate("2006-01", "asc")
287 c.Assert(err, qt.IsNil)
288 c.Assert(groups, comparePageGroup, expect)
289 }
290
291 func TestGroupByPublishDate(t *testing.T) {
292 c := qt.New(t)
293
294 t.Parallel()
295 pages := preparePageGroupTestPages(t)
296 expect := PagesGroup{
297 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
298 {Key: "2012-03", Pages: Pages{pages[3]}},
299 {Key: "2012-01", Pages: Pages{pages[1]}},
300 }
301
302 groups, err := pages.GroupByPublishDate("2006-01")
303 c.Assert(err, qt.IsNil)
304 c.Assert(groups, comparePageGroup, expect)
305 }
306
307 func TestGroupByPublishDateInReverseOrder(t *testing.T) {
308 c := qt.New(t)
309
310 t.Parallel()
311 pages := preparePageGroupTestPages(t)
312 expect := PagesGroup{
313 {Key: "2012-01", Pages: Pages{pages[1]}},
314 {Key: "2012-03", Pages: Pages{pages[3]}},
315 {Key: "2012-04", Pages: Pages{pages[0], pages[2], pages[4]}},
316 }
317
318 groups, err := pages.GroupByDate("2006-01", "asc")
319 c.Assert(err, qt.IsNil)
320 c.Assert(groups, comparePageGroup, expect)
321 }
322
323 func TestGroupByPublishDateWithEmptyPages(t *testing.T) {
324 c := qt.New(t)
325
326 t.Parallel()
327 var pages Pages
328 groups, err := pages.GroupByPublishDate("2006-01")
329 c.Assert(err, qt.IsNil)
330 c.Assert(groups, qt.IsNil)
331 }
332
333 func TestGroupByExpiryDate(t *testing.T) {
334 c := qt.New(t)
335
336 t.Parallel()
337 pages := preparePageGroupTestPages(t)
338 expect := PagesGroup{
339 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
340 {Key: "2012-03", Pages: Pages{pages[3]}},
341 {Key: "2012-01", Pages: Pages{pages[1]}},
342 }
343
344 groups, err := pages.GroupByExpiryDate("2006-01")
345 c.Assert(err, qt.IsNil)
346 c.Assert(groups, comparePageGroup, expect)
347 }
348
349 func TestGroupByParamDate(t *testing.T) {
350 c := qt.New(t)
351
352 t.Parallel()
353 pages := preparePageGroupTestPages(t)
354 expect := PagesGroup{
355 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
356 {Key: "2012-03", Pages: Pages{pages[3]}},
357 {Key: "2012-01", Pages: Pages{pages[1]}},
358 }
359
360 groups, err := pages.GroupByParamDate("custom_date", "2006-01")
361 c.Assert(err, qt.IsNil)
362 c.Assert(groups, comparePageGroup, expect)
363 }
364
365 func TestGroupByParamDateNested(t *testing.T) {
366 c := qt.New(t)
367
368 t.Parallel()
369 pages := preparePageGroupTestPages(t)
370 expect := PagesGroup{
371 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
372 {Key: "2012-03", Pages: Pages{pages[3]}},
373 {Key: "2012-01", Pages: Pages{pages[1]}},
374 }
375
376 groups, err := pages.GroupByParamDate("custom_object.date", "2006-01")
377 c.Assert(err, qt.IsNil)
378 c.Assert(groups, comparePageGroup, expect)
379 }
380
381 // https://github.com/gohugoio/hugo/issues/3983
382 func TestGroupByParamDateWithStringParams(t *testing.T) {
383 c := qt.New(t)
384
385 t.Parallel()
386 pages := preparePageGroupTestPages(t)
387 expect := PagesGroup{
388 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
389 {Key: "2012-03", Pages: Pages{pages[3]}},
390 {Key: "2012-01", Pages: Pages{pages[1]}},
391 }
392
393 groups, err := pages.GroupByParamDate("custom_string_date", "2006-01")
394 c.Assert(err, qt.IsNil)
395 c.Assert(groups, comparePageGroup, expect)
396 }
397
398 func TestGroupByParamDateNestedWithStringParams(t *testing.T) {
399 c := qt.New(t)
400
401 t.Parallel()
402 pages := preparePageGroupTestPages(t)
403 expect := PagesGroup{
404 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
405 {Key: "2012-03", Pages: Pages{pages[3]}},
406 {Key: "2012-01", Pages: Pages{pages[1]}},
407 }
408
409 groups, err := pages.GroupByParamDate("custom_object.string_date", "2006-01")
410 c.Assert(err, qt.IsNil)
411 c.Assert(groups, comparePageGroup, expect)
412 }
413
414 func TestGroupByLastmod(t *testing.T) {
415 c := qt.New(t)
416
417 t.Parallel()
418 pages := preparePageGroupTestPages(t)
419 expect := PagesGroup{
420 {Key: "2015-04", Pages: Pages{pages[4], pages[2], pages[0]}},
421 {Key: "2015-03", Pages: Pages{pages[3]}},
422 {Key: "2015-01", Pages: Pages{pages[1]}},
423 }
424
425 groups, err := pages.GroupByLastmod("2006-01")
426 c.Assert(err, qt.IsNil)
427 c.Assert(groups, comparePageGroup, expect)
428 }
429
430 func TestGroupByLastmodInReverseOrder(t *testing.T) {
431 c := qt.New(t)
432
433 t.Parallel()
434 pages := preparePageGroupTestPages(t)
435 expect := PagesGroup{
436 {Key: "2015-01", Pages: Pages{pages[1]}},
437 {Key: "2015-03", Pages: Pages{pages[3]}},
438 {Key: "2015-04", Pages: Pages{pages[0], pages[2], pages[4]}},
439 }
440
441 groups, err := pages.GroupByLastmod("2006-01", "asc")
442 c.Assert(err, qt.IsNil)
443 c.Assert(groups, comparePageGroup, expect)
444 }
445
446 func TestGroupByParamDateInReverseOrder(t *testing.T) {
447 c := qt.New(t)
448
449 t.Parallel()
450 pages := preparePageGroupTestPages(t)
451 expect := PagesGroup{
452 {Key: "2012-01", Pages: Pages{pages[1]}},
453 {Key: "2012-03", Pages: Pages{pages[3]}},
454 {Key: "2012-04", Pages: Pages{pages[0], pages[2], pages[4]}},
455 }
456
457 groups, err := pages.GroupByParamDate("custom_date", "2006-01", "asc")
458 c.Assert(err, qt.IsNil)
459 c.Assert(groups, comparePageGroup, expect)
460 }
461
462 func TestGroupByParamDateWithEmptyPages(t *testing.T) {
463 c := qt.New(t)
464
465 t.Parallel()
466 var pages Pages
467 groups, err := pages.GroupByParamDate("custom_date", "2006-01")
468 c.Assert(err, qt.IsNil)
469 c.Assert(groups, qt.IsNil)
470 }