yay/pkg/query/mixed_sources_test.go

62 lines
2.5 KiB
Go
Raw Normal View History

package query
import (
"context"
"strings"
"testing"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/aur"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMixedSourceQueryBuilder(t *testing.T) {
t.Parallel()
type testCase struct {
desc string
bottomUp bool
want string
}
testCases := []testCase{
{desc: "bottomup", bottomUp: true, want: "\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n The Linux-ck kernel and modules with ck's hrtimer patches\n\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n"},
{
desc: "topdown", bottomUp: false,
want: "\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\n The Linux-ck kernel and modules with ck's hrtimer patches\n",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
queryBuilder := NewMixedSourceQueryBuilder("votes", parser.ModeAny, "", tc.bottomUp, false)
search := []string{"linux"}
mockStore := &mockDB{}
client, err := aur.NewClient(aur.WithHTTPClient(&mockDoer{}))
require.NoError(t, err)
queryBuilder.Execute(context.Background(), mockStore, client, search)
assert.Len(t, queryBuilder.results, 3)
assert.Equal(t, 3, queryBuilder.Len())
if tc.bottomUp {
assert.Equal(t, "linux-ck", queryBuilder.results[0].name)
assert.Equal(t, "linux-zen", queryBuilder.results[1].name)
assert.Equal(t, "linux", queryBuilder.results[2].name)
} else {
assert.Equal(t, "linux-ck", queryBuilder.results[2].name)
assert.Equal(t, "linux-zen", queryBuilder.results[1].name)
assert.Equal(t, "linux", queryBuilder.results[0].name)
}
w := &strings.Builder{}
queryBuilder.Results(w, mockStore, Detailed)
wString := w.String()
require.GreaterOrEqual(t, len(wString), 1)
assert.Equal(t, tc.want, wString)
})
}
}