123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package query
- import (
- "bytes"
- "context"
- "io"
- "net/http"
- "strings"
- "testing"
- "github.com/Jguer/yay/v11/pkg/db"
- "github.com/Jguer/yay/v11/pkg/db/mock"
- "github.com/Jguer/yay/v11/pkg/settings/parser"
- "github.com/Jguer/aur"
- "github.com/Jguer/go-alpm/v2"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- const validPayload = `{
- "resultcount": 1,
- "results": [
- {
- "Description": "The Linux-ck kernel and modules with ck's hrtimer patches",
- "FirstSubmitted": 1311346274,
- "ID": 1045311,
- "LastModified": 1646250901,
- "Maintainer": "graysky",
- "Name": "linux-ck",
- "NumVotes": 450,
- "OutOfDate": null,
- "PackageBase": "linux-ck",
- "PackageBaseID": 50911,
- "Popularity": 1.511141,
- "URL": "https://wiki.archlinux.org/index.php/Linux-ck",
- "URLPath": "/cgit/aur.git/snapshot/linux-ck.tar.gz",
- "Version": "5.16.12-1"
- }
- ],
- "type": "search",
- "version": 5
- }
- `
- type mockDB struct {
- db.Executor
- }
- func (m *mockDB) LocalPackage(string) alpm.IPackage {
- return nil
- }
- func (m *mockDB) PackageGroups(pkg alpm.IPackage) []string {
- return []string{}
- }
- func (m *mockDB) SyncPackages(...string) []alpm.IPackage {
- mockDB := mock.NewDB("core")
- linuxRepo := &mock.Package{
- PName: "linux",
- PVersion: "5.16.0",
- PDescription: "The Linux kernel and modules",
- PSize: 1,
- PISize: 1,
- PDB: mockDB,
- }
- linuxZen := &mock.Package{
- PName: "linux-zen",
- PVersion: "5.16.0",
- PDescription: "The Linux ZEN kernel and modules",
- PSize: 1,
- PISize: 1,
- PDB: mockDB,
- }
- return []alpm.IPackage{linuxRepo, linuxZen}
- }
- type mockDoer struct{}
- func (m *mockDoer) Do(req *http.Request) (*http.Response, error) {
- return &http.Response{
- StatusCode: 200,
- Body: io.NopCloser(bytes.NewBufferString(validPayload)),
- }, nil
- }
- func TestSourceQueryBuilder(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 := NewSourceQueryBuilder("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.aurQuery, 1)
- assert.Len(t, queryBuilder.repoQuery, 2)
- assert.Equal(t, 3, queryBuilder.Len())
- assert.Equal(t, "linux-ck", queryBuilder.aurQuery[0].Name)
- if tc.bottomUp {
- assert.Equal(t, "linux-zen", queryBuilder.repoQuery[0].Name())
- assert.Equal(t, "linux", queryBuilder.repoQuery[1].Name())
- } else {
- assert.Equal(t, "linux-zen", queryBuilder.repoQuery[1].Name())
- assert.Equal(t, "linux", queryBuilder.repoQuery[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)
- })
- }
- }
|