pages_related_test.go (2338B)
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 "testing" 18 "time" 19 20 "github.com/gohugoio/hugo/common/types" 21 22 qt "github.com/frankban/quicktest" 23 ) 24 25 func TestRelated(t *testing.T) { 26 c := qt.New(t) 27 28 t.Parallel() 29 30 pages := Pages{ 31 &testPage{ 32 title: "Page 1", 33 pubDate: mustParseDate("2017-01-03"), 34 params: map[string]any{ 35 "keywords": []string{"hugo", "says"}, 36 }, 37 }, 38 &testPage{ 39 title: "Page 2", 40 pubDate: mustParseDate("2017-01-02"), 41 params: map[string]any{ 42 "keywords": []string{"hugo", "rocks"}, 43 }, 44 }, 45 &testPage{ 46 title: "Page 3", 47 pubDate: mustParseDate("2017-01-01"), 48 params: map[string]any{ 49 "keywords": []string{"bep", "says"}, 50 }, 51 }, 52 } 53 54 result, err := pages.RelatedTo(types.NewKeyValuesStrings("keywords", "hugo", "rocks")) 55 56 c.Assert(err, qt.IsNil) 57 c.Assert(len(result), qt.Equals, 2) 58 c.Assert(result[0].Title(), qt.Equals, "Page 2") 59 c.Assert(result[1].Title(), qt.Equals, "Page 1") 60 61 result, err = pages.Related(pages[0]) 62 c.Assert(err, qt.IsNil) 63 c.Assert(len(result), qt.Equals, 2) 64 c.Assert(result[0].Title(), qt.Equals, "Page 2") 65 c.Assert(result[1].Title(), qt.Equals, "Page 3") 66 67 result, err = pages.RelatedIndices(pages[0], "keywords") 68 c.Assert(err, qt.IsNil) 69 c.Assert(len(result), qt.Equals, 2) 70 c.Assert(result[0].Title(), qt.Equals, "Page 2") 71 c.Assert(result[1].Title(), qt.Equals, "Page 3") 72 73 result, err = pages.RelatedTo(types.NewKeyValuesStrings("keywords", "bep", "rocks")) 74 c.Assert(err, qt.IsNil) 75 c.Assert(len(result), qt.Equals, 2) 76 c.Assert(result[0].Title(), qt.Equals, "Page 2") 77 c.Assert(result[1].Title(), qt.Equals, "Page 3") 78 } 79 80 func mustParseDate(s string) time.Time { 81 d, err := time.Parse("2006-01-02", s) 82 if err != nil { 83 panic(err) 84 } 85 return d 86 }