github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/resources/page/pagegroup_test.go (about) 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 "reflect" 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 pages = append(pages, p) 59 } 60 return pages 61 } 62 63 func TestGroupByWithFieldNameArg(t *testing.T) { 64 t.Parallel() 65 pages := preparePageGroupTestPages(t) 66 expect := PagesGroup{ 67 {Key: 1, Pages: Pages{pages[3], pages[4]}}, 68 {Key: 2, Pages: Pages{pages[2]}}, 69 {Key: 3, Pages: Pages{pages[0], pages[1]}}, 70 } 71 72 groups, err := pages.GroupBy(context.Background(), "Weight") 73 if err != nil { 74 t.Fatalf("Unable to make PagesGroup array: %s", err) 75 } 76 if !reflect.DeepEqual(groups, expect) { 77 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 78 } 79 } 80 81 func TestGroupByWithMethodNameArg(t *testing.T) { 82 t.Parallel() 83 pages := preparePageGroupTestPages(t) 84 expect := PagesGroup{ 85 {Key: "section1", Pages: Pages{pages[0], pages[1], pages[2]}}, 86 {Key: "section2", Pages: Pages{pages[3], pages[4]}}, 87 } 88 89 groups, err := pages.GroupBy(context.Background(), "Type") 90 if err != nil { 91 t.Fatalf("Unable to make PagesGroup array: %s", err) 92 } 93 if !reflect.DeepEqual(groups, expect) { 94 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 95 } 96 } 97 98 func TestGroupByWithSectionArg(t *testing.T) { 99 t.Parallel() 100 pages := preparePageGroupTestPages(t) 101 expect := PagesGroup{ 102 {Key: "section1", Pages: Pages{pages[0], pages[1], pages[2]}}, 103 {Key: "section2", Pages: Pages{pages[3], pages[4]}}, 104 } 105 106 groups, err := pages.GroupBy(context.Background(), "Section") 107 if err != nil { 108 t.Fatalf("Unable to make PagesGroup array: %s", err) 109 } 110 if !reflect.DeepEqual(groups, expect) { 111 t.Errorf("PagesGroup has unexpected groups. It should be\n%#v, got\n%#v", expect, groups) 112 } 113 } 114 115 func TestGroupByInReverseOrder(t *testing.T) { 116 t.Parallel() 117 pages := preparePageGroupTestPages(t) 118 expect := PagesGroup{ 119 {Key: 3, Pages: Pages{pages[0], pages[1]}}, 120 {Key: 2, Pages: Pages{pages[2]}}, 121 {Key: 1, Pages: Pages{pages[3], pages[4]}}, 122 } 123 124 groups, err := pages.GroupBy(context.Background(), "Weight", "desc") 125 if err != nil { 126 t.Fatalf("Unable to make PagesGroup array: %s", err) 127 } 128 if !reflect.DeepEqual(groups, expect) { 129 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 130 } 131 } 132 133 func TestGroupByCalledWithEmptyPages(t *testing.T) { 134 t.Parallel() 135 var pages Pages 136 groups, err := pages.GroupBy(context.Background(), "Weight") 137 if err != nil { 138 t.Fatalf("Unable to make PagesGroup array: %s", err) 139 } 140 if groups != nil { 141 t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups) 142 } 143 } 144 145 func TestGroupByParamCalledWithUnavailableKey(t *testing.T) { 146 t.Parallel() 147 pages := preparePageGroupTestPages(t) 148 _, err := pages.GroupByParam("UnavailableKey") 149 if err == nil { 150 t.Errorf("GroupByParam should return an error but didn't") 151 } 152 } 153 154 func TestReverse(t *testing.T) { 155 t.Parallel() 156 pages := preparePageGroupTestPages(t) 157 158 groups1, err := pages.GroupBy(context.Background(), "Weight", "desc") 159 if err != nil { 160 t.Fatalf("Unable to make PagesGroup array: %s", err) 161 } 162 163 groups2, err := pages.GroupBy(context.Background(), "Weight") 164 if err != nil { 165 t.Fatalf("Unable to make PagesGroup array: %s", err) 166 } 167 groups2 = groups2.Reverse() 168 169 if !reflect.DeepEqual(groups2, groups1) { 170 t.Errorf("PagesGroup is sorted in unexpected order. It should be %#v, got %#v", groups2, groups1) 171 } 172 } 173 174 func TestGroupByParam(t *testing.T) { 175 t.Parallel() 176 pages := preparePageGroupTestPages(t) 177 expect := PagesGroup{ 178 {Key: "bar", Pages: Pages{pages[1], pages[3]}}, 179 {Key: "baz", Pages: Pages{pages[4]}}, 180 {Key: "foo", Pages: Pages{pages[0], pages[2]}}, 181 } 182 183 groups, err := pages.GroupByParam("custom_param") 184 if err != nil { 185 t.Fatalf("Unable to make PagesGroup array: %s", err) 186 } 187 if !reflect.DeepEqual(groups, expect) { 188 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 189 } 190 } 191 192 func TestGroupByParamInReverseOrder(t *testing.T) { 193 t.Parallel() 194 pages := preparePageGroupTestPages(t) 195 expect := PagesGroup{ 196 {Key: "foo", Pages: Pages{pages[0], pages[2]}}, 197 {Key: "baz", Pages: Pages{pages[4]}}, 198 {Key: "bar", Pages: Pages{pages[1], pages[3]}}, 199 } 200 201 groups, err := pages.GroupByParam("custom_param", "desc") 202 if err != nil { 203 t.Fatalf("Unable to make PagesGroup array: %s", err) 204 } 205 if !reflect.DeepEqual(groups, expect) { 206 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 207 } 208 } 209 210 func TestGroupByParamCalledWithCapitalLetterString(t *testing.T) { 211 c := qt.New(t) 212 testStr := "TestString" 213 p := newTestPage() 214 p.params["custom_param"] = testStr 215 pages := Pages{p} 216 217 groups, err := pages.GroupByParam("custom_param") 218 219 c.Assert(err, qt.IsNil) 220 c.Assert(groups[0].Key, qt.Equals, testStr) 221 } 222 223 func TestGroupByParamCalledWithSomeUnavailableParams(t *testing.T) { 224 t.Parallel() 225 pages := preparePageGroupTestPages(t) 226 delete(pages[1].Params(), "custom_param") 227 delete(pages[3].Params(), "custom_param") 228 delete(pages[4].Params(), "custom_param") 229 230 expect := PagesGroup{ 231 {Key: "foo", Pages: Pages{pages[0], pages[2]}}, 232 } 233 234 groups, err := pages.GroupByParam("custom_param") 235 if err != nil { 236 t.Fatalf("Unable to make PagesGroup array: %s", err) 237 } 238 if !reflect.DeepEqual(groups, expect) { 239 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 240 } 241 } 242 243 func TestGroupByParamCalledWithEmptyPages(t *testing.T) { 244 t.Parallel() 245 var pages Pages 246 groups, err := pages.GroupByParam("custom_param") 247 if err != nil { 248 t.Fatalf("Unable to make PagesGroup array: %s", err) 249 } 250 if groups != nil { 251 t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups) 252 } 253 } 254 255 func TestGroupByParamCalledWithUnavailableParam(t *testing.T) { 256 t.Parallel() 257 pages := preparePageGroupTestPages(t) 258 _, err := pages.GroupByParam("unavailable_param") 259 if err == nil { 260 t.Errorf("GroupByParam should return an error but didn't") 261 } 262 } 263 264 func TestGroupByDate(t *testing.T) { 265 t.Parallel() 266 pages := preparePageGroupTestPages(t) 267 expect := PagesGroup{ 268 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}}, 269 {Key: "2012-03", Pages: Pages{pages[3]}}, 270 {Key: "2012-01", Pages: Pages{pages[1]}}, 271 } 272 273 groups, err := pages.GroupByDate("2006-01") 274 if err != nil { 275 t.Fatalf("Unable to make PagesGroup array: %s", err) 276 } 277 if !reflect.DeepEqual(groups, expect) { 278 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 279 } 280 } 281 282 func TestGroupByDateInReverseOrder(t *testing.T) { 283 t.Parallel() 284 pages := preparePageGroupTestPages(t) 285 expect := PagesGroup{ 286 {Key: "2012-01", Pages: Pages{pages[1]}}, 287 {Key: "2012-03", Pages: Pages{pages[3]}}, 288 {Key: "2012-04", Pages: Pages{pages[0], pages[2], pages[4]}}, 289 } 290 291 groups, err := pages.GroupByDate("2006-01", "asc") 292 if err != nil { 293 t.Fatalf("Unable to make PagesGroup array: %s", err) 294 } 295 if !reflect.DeepEqual(groups, expect) { 296 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 297 } 298 } 299 300 func TestGroupByPublishDate(t *testing.T) { 301 t.Parallel() 302 pages := preparePageGroupTestPages(t) 303 expect := PagesGroup{ 304 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}}, 305 {Key: "2012-03", Pages: Pages{pages[3]}}, 306 {Key: "2012-01", Pages: Pages{pages[1]}}, 307 } 308 309 groups, err := pages.GroupByPublishDate("2006-01") 310 if err != nil { 311 t.Fatalf("Unable to make PagesGroup array: %s", err) 312 } 313 if !reflect.DeepEqual(groups, expect) { 314 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 315 } 316 } 317 318 func TestGroupByPublishDateInReverseOrder(t *testing.T) { 319 t.Parallel() 320 pages := preparePageGroupTestPages(t) 321 expect := PagesGroup{ 322 {Key: "2012-01", Pages: Pages{pages[1]}}, 323 {Key: "2012-03", Pages: Pages{pages[3]}}, 324 {Key: "2012-04", Pages: Pages{pages[0], pages[2], pages[4]}}, 325 } 326 327 groups, err := pages.GroupByDate("2006-01", "asc") 328 if err != nil { 329 t.Fatalf("Unable to make PagesGroup array: %s", err) 330 } 331 if !reflect.DeepEqual(groups, expect) { 332 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 333 } 334 } 335 336 func TestGroupByPublishDateWithEmptyPages(t *testing.T) { 337 t.Parallel() 338 var pages Pages 339 groups, err := pages.GroupByPublishDate("2006-01") 340 if err != nil { 341 t.Fatalf("Unable to make PagesGroup array: %s", err) 342 } 343 if groups != nil { 344 t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups) 345 } 346 } 347 348 func TestGroupByExpiryDate(t *testing.T) { 349 t.Parallel() 350 pages := preparePageGroupTestPages(t) 351 expect := PagesGroup{ 352 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}}, 353 {Key: "2012-03", Pages: Pages{pages[3]}}, 354 {Key: "2012-01", Pages: Pages{pages[1]}}, 355 } 356 357 groups, err := pages.GroupByExpiryDate("2006-01") 358 if err != nil { 359 t.Fatalf("Unable to make PagesGroup array: %s", err) 360 } 361 if !reflect.DeepEqual(groups, expect) { 362 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 363 } 364 } 365 366 func TestGroupByParamDate(t *testing.T) { 367 t.Parallel() 368 pages := preparePageGroupTestPages(t) 369 expect := PagesGroup{ 370 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}}, 371 {Key: "2012-03", Pages: Pages{pages[3]}}, 372 {Key: "2012-01", Pages: Pages{pages[1]}}, 373 } 374 375 groups, err := pages.GroupByParamDate("custom_date", "2006-01") 376 if err != nil { 377 t.Fatalf("Unable to make PagesGroup array: %s", err) 378 } 379 if !reflect.DeepEqual(groups, expect) { 380 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 381 } 382 } 383 384 // https://github.com/gohugoio/hugo/issues/3983 385 func TestGroupByParamDateWithStringParams(t *testing.T) { 386 t.Parallel() 387 pages := preparePageGroupTestPages(t) 388 expect := PagesGroup{ 389 {Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}}, 390 {Key: "2012-03", Pages: Pages{pages[3]}}, 391 {Key: "2012-01", Pages: Pages{pages[1]}}, 392 } 393 394 groups, err := pages.GroupByParamDate("custom_string_date", "2006-01") 395 if err != nil { 396 t.Fatalf("Unable to make PagesGroup array: %s", err) 397 } 398 if !reflect.DeepEqual(groups, expect) { 399 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 400 } 401 } 402 403 func TestGroupByLastmod(t *testing.T) { 404 t.Parallel() 405 pages := preparePageGroupTestPages(t) 406 expect := PagesGroup{ 407 {Key: "2015-04", Pages: Pages{pages[4], pages[2], pages[0]}}, 408 {Key: "2015-03", Pages: Pages{pages[3]}}, 409 {Key: "2015-01", Pages: Pages{pages[1]}}, 410 } 411 412 groups, err := pages.GroupByLastmod("2006-01") 413 if err != nil { 414 t.Fatalf("Unable to make PagesGroup array: %s", err) 415 } 416 if !reflect.DeepEqual(groups, expect) { 417 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 418 } 419 } 420 421 func TestGroupByLastmodInReverseOrder(t *testing.T) { 422 t.Parallel() 423 pages := preparePageGroupTestPages(t) 424 expect := PagesGroup{ 425 {Key: "2015-01", Pages: Pages{pages[1]}}, 426 {Key: "2015-03", Pages: Pages{pages[3]}}, 427 {Key: "2015-04", Pages: Pages{pages[0], pages[2], pages[4]}}, 428 } 429 430 groups, err := pages.GroupByLastmod("2006-01", "asc") 431 if err != nil { 432 t.Fatalf("Unable to make PagesGroup array: %s", err) 433 } 434 if !reflect.DeepEqual(groups, expect) { 435 t.Errorf("PagesGroup has unexpected groups. It should be\n%#v, got\n%#v", expect, groups) 436 } 437 } 438 439 func TestGroupByParamDateInReverseOrder(t *testing.T) { 440 t.Parallel() 441 pages := preparePageGroupTestPages(t) 442 expect := PagesGroup{ 443 {Key: "2012-01", Pages: Pages{pages[1]}}, 444 {Key: "2012-03", Pages: Pages{pages[3]}}, 445 {Key: "2012-04", Pages: Pages{pages[0], pages[2], pages[4]}}, 446 } 447 448 groups, err := pages.GroupByParamDate("custom_date", "2006-01", "asc") 449 if err != nil { 450 t.Fatalf("Unable to make PagesGroup array: %s", err) 451 } 452 if !reflect.DeepEqual(groups, expect) { 453 t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups) 454 } 455 } 456 457 func TestGroupByParamDateWithEmptyPages(t *testing.T) { 458 t.Parallel() 459 var pages Pages 460 groups, err := pages.GroupByParamDate("custom_date", "2006-01") 461 if err != nil { 462 t.Fatalf("Unable to make PagesGroup array: %s", err) 463 } 464 if groups != nil { 465 t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups) 466 } 467 }