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