revamp pagegroup tests with quicktest

This commit is contained in:
n1xx1 2024-08-01 15:06:28 +02:00 committed by Bjørn Erik Pedersen
parent 51f09b17fd
commit 914fa13efd

View file

@ -15,7 +15,7 @@ package page
import ( import (
"context" "context"
"reflect" "github.com/google/go-cmp/cmp"
"strings" "strings"
"testing" "testing"
@ -65,7 +65,13 @@ func preparePageGroupTestPages(t *testing.T) Pages {
return pages return pages
} }
var comparePageGroup = qt.CmpEquals(cmp.Comparer(func(a, b Page) bool {
return a == b
}))
func TestGroupByWithFieldNameArg(t *testing.T) { func TestGroupByWithFieldNameArg(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -75,15 +81,13 @@ func TestGroupByWithFieldNameArg(t *testing.T) {
} }
groups, err := pages.GroupBy(context.Background(), "Weight") groups, err := pages.GroupBy(context.Background(), "Weight")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByWithMethodNameArg(t *testing.T) { func TestGroupByWithMethodNameArg(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -92,15 +96,13 @@ func TestGroupByWithMethodNameArg(t *testing.T) {
} }
groups, err := pages.GroupBy(context.Background(), "Type") groups, err := pages.GroupBy(context.Background(), "Type")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByWithSectionArg(t *testing.T) { func TestGroupByWithSectionArg(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -109,15 +111,13 @@ func TestGroupByWithSectionArg(t *testing.T) {
} }
groups, err := pages.GroupBy(context.Background(), "Section") groups, err := pages.GroupBy(context.Background(), "Section")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be\n%#v, got\n%#v", expect, groups)
}
} }
func TestGroupByInReverseOrder(t *testing.T) { func TestGroupByInReverseOrder(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -127,47 +127,39 @@ func TestGroupByInReverseOrder(t *testing.T) {
} }
groups, err := pages.GroupBy(context.Background(), "Weight", "desc") groups, err := pages.GroupBy(context.Background(), "Weight", "desc")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByCalledWithEmptyPages(t *testing.T) { func TestGroupByCalledWithEmptyPages(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
var pages Pages var pages Pages
groups, err := pages.GroupBy(context.Background(), "Weight") groups, err := pages.GroupBy(context.Background(), "Weight")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, qt.IsNil)
}
if groups != nil {
t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups)
}
} }
func TestReverse(t *testing.T) { func TestReverse(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
groups1, err := pages.GroupBy(context.Background(), "Weight", "desc") groups1, err := pages.GroupBy(context.Background(), "Weight", "desc")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err)
}
groups2, err := pages.GroupBy(context.Background(), "Weight") groups2, err := pages.GroupBy(context.Background(), "Weight")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err)
}
groups2 = groups2.Reverse()
if !reflect.DeepEqual(groups2, groups1) { groups2 = groups2.Reverse()
t.Errorf("PagesGroup is sorted in unexpected order. It should be %#v, got %#v", groups2, groups1) c.Assert(groups2, comparePageGroup, groups1)
}
} }
func TestGroupByParam(t *testing.T) { func TestGroupByParam(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -177,15 +169,13 @@ func TestGroupByParam(t *testing.T) {
} }
groups, err := pages.GroupByParam("custom_param") groups, err := pages.GroupByParam("custom_param")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByParamInReverseOrder(t *testing.T) { func TestGroupByParamInReverseOrder(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -195,12 +185,8 @@ func TestGroupByParamInReverseOrder(t *testing.T) {
} }
groups, err := pages.GroupByParam("custom_param", "desc") groups, err := pages.GroupByParam("custom_param", "desc")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByParamCalledWithCapitalLetterString(t *testing.T) { func TestGroupByParamCalledWithCapitalLetterString(t *testing.T) {
@ -213,10 +199,12 @@ func TestGroupByParamCalledWithCapitalLetterString(t *testing.T) {
groups, err := pages.GroupByParam("custom_param") groups, err := pages.GroupByParam("custom_param")
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
c.Assert(groups[0].Key, qt.Equals, testStr) c.Assert(groups[0].Key, qt.DeepEquals, testStr)
} }
func TestGroupByParamCalledWithSomeUnavailableParams(t *testing.T) { func TestGroupByParamCalledWithSomeUnavailableParams(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
delete(pages[1].Params(), "custom_param") delete(pages[1].Params(), "custom_param")
@ -228,36 +216,32 @@ func TestGroupByParamCalledWithSomeUnavailableParams(t *testing.T) {
} }
groups, err := pages.GroupByParam("custom_param") groups, err := pages.GroupByParam("custom_param")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByParamCalledWithEmptyPages(t *testing.T) { func TestGroupByParamCalledWithEmptyPages(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
var pages Pages var pages Pages
groups, err := pages.GroupByParam("custom_param") groups, err := pages.GroupByParam("custom_param")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, qt.IsNil)
}
if groups != nil {
t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups)
}
} }
func TestGroupByParamCalledWithUnavailableParam(t *testing.T) { func TestGroupByParamCalledWithUnavailableParam(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
_, err := pages.GroupByParam("unavailable_param") _, err := pages.GroupByParam("unavailable_param")
if err != nil { c.Assert(err, qt.IsNil)
t.Errorf("GroupByParam returned an error when it shouldn't")
}
} }
func TestGroupByParamNested(t *testing.T) { func TestGroupByParamNested(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
@ -268,15 +252,13 @@ func TestGroupByParamNested(t *testing.T) {
} }
groups, err := pages.GroupByParam("custom_object.param") groups, err := pages.GroupByParam("custom_object.param")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByDate(t *testing.T) { func TestGroupByDate(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -286,15 +268,13 @@ func TestGroupByDate(t *testing.T) {
} }
groups, err := pages.GroupByDate("2006-01") groups, err := pages.GroupByDate("2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByDateInReverseOrder(t *testing.T) { func TestGroupByDateInReverseOrder(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -304,15 +284,13 @@ func TestGroupByDateInReverseOrder(t *testing.T) {
} }
groups, err := pages.GroupByDate("2006-01", "asc") groups, err := pages.GroupByDate("2006-01", "asc")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByPublishDate(t *testing.T) { func TestGroupByPublishDate(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -322,15 +300,13 @@ func TestGroupByPublishDate(t *testing.T) {
} }
groups, err := pages.GroupByPublishDate("2006-01") groups, err := pages.GroupByPublishDate("2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByPublishDateInReverseOrder(t *testing.T) { func TestGroupByPublishDateInReverseOrder(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -340,27 +316,23 @@ func TestGroupByPublishDateInReverseOrder(t *testing.T) {
} }
groups, err := pages.GroupByDate("2006-01", "asc") groups, err := pages.GroupByDate("2006-01", "asc")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByPublishDateWithEmptyPages(t *testing.T) { func TestGroupByPublishDateWithEmptyPages(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
var pages Pages var pages Pages
groups, err := pages.GroupByPublishDate("2006-01") groups, err := pages.GroupByPublishDate("2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, qt.IsNil)
}
if groups != nil {
t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups)
}
} }
func TestGroupByExpiryDate(t *testing.T) { func TestGroupByExpiryDate(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -370,15 +342,13 @@ func TestGroupByExpiryDate(t *testing.T) {
} }
groups, err := pages.GroupByExpiryDate("2006-01") groups, err := pages.GroupByExpiryDate("2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByParamDate(t *testing.T) { func TestGroupByParamDate(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -388,15 +358,13 @@ func TestGroupByParamDate(t *testing.T) {
} }
groups, err := pages.GroupByParamDate("custom_date", "2006-01") groups, err := pages.GroupByParamDate("custom_date", "2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByParamDateNested(t *testing.T) { func TestGroupByParamDateNested(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -406,16 +374,14 @@ func TestGroupByParamDateNested(t *testing.T) {
} }
groups, err := pages.GroupByParamDate("custom_object.date", "2006-01") groups, err := pages.GroupByParamDate("custom_object.date", "2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
// https://github.com/gohugoio/hugo/issues/3983 // https://github.com/gohugoio/hugo/issues/3983
func TestGroupByParamDateWithStringParams(t *testing.T) { func TestGroupByParamDateWithStringParams(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -425,15 +391,13 @@ func TestGroupByParamDateWithStringParams(t *testing.T) {
} }
groups, err := pages.GroupByParamDate("custom_string_date", "2006-01") groups, err := pages.GroupByParamDate("custom_string_date", "2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByParamDateNestedWithStringParams(t *testing.T) { func TestGroupByParamDateNestedWithStringParams(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -443,15 +407,13 @@ func TestGroupByParamDateNestedWithStringParams(t *testing.T) {
} }
groups, err := pages.GroupByParamDate("custom_object.string_date", "2006-01") groups, err := pages.GroupByParamDate("custom_object.string_date", "2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByLastmod(t *testing.T) { func TestGroupByLastmod(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -461,15 +423,13 @@ func TestGroupByLastmod(t *testing.T) {
} }
groups, err := pages.GroupByLastmod("2006-01") groups, err := pages.GroupByLastmod("2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByLastmodInReverseOrder(t *testing.T) { func TestGroupByLastmodInReverseOrder(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -479,15 +439,13 @@ func TestGroupByLastmodInReverseOrder(t *testing.T) {
} }
groups, err := pages.GroupByLastmod("2006-01", "asc") groups, err := pages.GroupByLastmod("2006-01", "asc")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be\n%#v, got\n%#v", expect, groups)
}
} }
func TestGroupByParamDateInReverseOrder(t *testing.T) { func TestGroupByParamDateInReverseOrder(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
pages := preparePageGroupTestPages(t) pages := preparePageGroupTestPages(t)
expect := PagesGroup{ expect := PagesGroup{
@ -497,22 +455,16 @@ func TestGroupByParamDateInReverseOrder(t *testing.T) {
} }
groups, err := pages.GroupByParamDate("custom_date", "2006-01", "asc") groups, err := pages.GroupByParamDate("custom_date", "2006-01", "asc")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, comparePageGroup, expect)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
} }
func TestGroupByParamDateWithEmptyPages(t *testing.T) { func TestGroupByParamDateWithEmptyPages(t *testing.T) {
c := qt.New(t)
t.Parallel() t.Parallel()
var pages Pages var pages Pages
groups, err := pages.GroupByParamDate("custom_date", "2006-01") groups, err := pages.GroupByParamDate("custom_date", "2006-01")
if err != nil { c.Assert(err, qt.IsNil)
t.Fatalf("Unable to make PagesGroup array: %s", err) c.Assert(groups, qt.IsNil)
}
if groups != nil {
t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups)
}
} }