github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/resources/page/pages_related_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  	"testing"
    19  	"time"
    20  
    21  	"github.com/gohugoio/hugo/common/types"
    22  
    23  	qt "github.com/frankban/quicktest"
    24  )
    25  
    26  func TestRelated(t *testing.T) {
    27  	c := qt.New(t)
    28  
    29  	t.Parallel()
    30  
    31  	pages := Pages{
    32  		&testPage{
    33  			title:   "Page 1",
    34  			pubDate: mustParseDate("2017-01-03"),
    35  			params: map[string]any{
    36  				"keywords": []string{"hugo", "says"},
    37  			},
    38  		},
    39  		&testPage{
    40  			title:   "Page 2",
    41  			pubDate: mustParseDate("2017-01-02"),
    42  			params: map[string]any{
    43  				"keywords": []string{"hugo", "rocks"},
    44  			},
    45  		},
    46  		&testPage{
    47  			title:   "Page 3",
    48  			pubDate: mustParseDate("2017-01-01"),
    49  			params: map[string]any{
    50  				"keywords": []string{"bep", "says"},
    51  			},
    52  		},
    53  	}
    54  
    55  	ctx := context.Background()
    56  	opts := map[string]any{
    57  		"namedSlices": types.NewKeyValuesStrings("keywords", "hugo", "rocks"),
    58  	}
    59  	result, err := pages.Related(ctx, opts)
    60  
    61  	c.Assert(err, qt.IsNil)
    62  	c.Assert(len(result), qt.Equals, 2)
    63  	c.Assert(result[0].Title(), qt.Equals, "Page 2")
    64  	c.Assert(result[1].Title(), qt.Equals, "Page 1")
    65  
    66  	result, err = pages.Related(ctx, pages[0])
    67  	c.Assert(err, qt.IsNil)
    68  	c.Assert(len(result), qt.Equals, 2)
    69  	c.Assert(result[0].Title(), qt.Equals, "Page 2")
    70  	c.Assert(result[1].Title(), qt.Equals, "Page 3")
    71  
    72  	opts = map[string]any{
    73  		"document": pages[0],
    74  		"indices":  []string{"keywords"},
    75  	}
    76  	result, err = pages.Related(ctx, opts)
    77  	c.Assert(err, qt.IsNil)
    78  	c.Assert(len(result), qt.Equals, 2)
    79  	c.Assert(result[0].Title(), qt.Equals, "Page 2")
    80  	c.Assert(result[1].Title(), qt.Equals, "Page 3")
    81  
    82  	opts = map[string]any{
    83  		"namedSlices": []types.KeyValues{
    84  			{
    85  				Key:    "keywords",
    86  				Values: []any{"bep", "rocks"},
    87  			},
    88  		},
    89  	}
    90  	result, err = pages.Related(context.Background(), opts)
    91  	c.Assert(err, qt.IsNil)
    92  	c.Assert(len(result), qt.Equals, 2)
    93  	c.Assert(result[0].Title(), qt.Equals, "Page 2")
    94  	c.Assert(result[1].Title(), qt.Equals, "Page 3")
    95  }
    96  
    97  func mustParseDate(s string) time.Time {
    98  	d, err := time.Parse("2006-01-02", s)
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  	return d
   103  }