source_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package query
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "testing"
  9. "github.com/Jguer/aur/rpc"
  10. "github.com/Jguer/yay/v12/pkg/db"
  11. "github.com/Jguer/yay/v12/pkg/db/mock"
  12. "github.com/Jguer/yay/v12/pkg/settings/parser"
  13. "github.com/Jguer/yay/v12/pkg/text"
  14. "github.com/Jguer/go-alpm/v2"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. )
  18. const validPayload = `{
  19. "resultcount": 1,
  20. "results": [
  21. {
  22. "Description": "The Linux-ck kernel and modules with ck's hrtimer patches",
  23. "FirstSubmitted": 1311346274,
  24. "ID": 1045311,
  25. "LastModified": 1646250901,
  26. "Maintainer": "graysky",
  27. "Name": "linux-ck",
  28. "NumVotes": 450,
  29. "OutOfDate": null,
  30. "PackageBase": "linux-ck",
  31. "PackageBaseID": 50911,
  32. "Popularity": 1.511141,
  33. "URL": "https://wiki.archlinux.org/index.php/Linux-ck",
  34. "URLPath": "/cgit/aur.git/snapshot/linux-ck.tar.gz",
  35. "Version": "5.16.12-1"
  36. }
  37. ],
  38. "type": "search",
  39. "version": 5
  40. }
  41. `
  42. type mockDB struct {
  43. db.Executor
  44. }
  45. func (m *mockDB) LocalPackage(string) alpm.IPackage {
  46. return nil
  47. }
  48. func (m *mockDB) PackageGroups(pkg alpm.IPackage) []string {
  49. return []string{}
  50. }
  51. func (m *mockDB) SyncPackages(...string) []alpm.IPackage {
  52. mockDB := mock.NewDB("core")
  53. linuxRepo := &mock.Package{
  54. PName: "linux",
  55. PVersion: "5.16.0",
  56. PDescription: "The Linux kernel and modules",
  57. PSize: 1,
  58. PISize: 1,
  59. PDB: mockDB,
  60. }
  61. linuxZen := &mock.Package{
  62. PName: "linux-zen",
  63. PVersion: "5.16.0",
  64. PDescription: "The Linux ZEN kernel and modules",
  65. PSize: 1,
  66. PISize: 1,
  67. PDB: mockDB,
  68. }
  69. return []alpm.IPackage{linuxRepo, linuxZen}
  70. }
  71. type mockDoer struct{}
  72. func (m *mockDoer) Do(req *http.Request) (*http.Response, error) {
  73. return &http.Response{
  74. StatusCode: http.StatusOK,
  75. Body: io.NopCloser(bytes.NewBufferString(validPayload)),
  76. }, nil
  77. }
  78. func TestSourceQueryBuilder(t *testing.T) {
  79. t.Parallel()
  80. type testCase struct {
  81. desc string
  82. bottomUp bool
  83. want string
  84. }
  85. testCases := []testCase{
  86. {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"},
  87. {
  88. desc: "topdown", bottomUp: false,
  89. 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",
  90. },
  91. }
  92. for _, tc := range testCases {
  93. t.Run(tc.desc, func(t *testing.T) {
  94. client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{}))
  95. require.NoError(t, err)
  96. queryBuilder := NewSourceQueryBuilder(client, client,
  97. text.NewLogger(io.Discard, bytes.NewBufferString(""), false, "test"),
  98. "votes", parser.ModeAny, "", tc.bottomUp, false, false)
  99. search := []string{"linux"}
  100. mockStore := &mockDB{}
  101. queryBuilder.Execute(context.Background(), mockStore, search)
  102. assert.Len(t, queryBuilder.aurQuery, 1)
  103. assert.Len(t, queryBuilder.repoQuery, 2)
  104. assert.Equal(t, 3, queryBuilder.Len())
  105. assert.Equal(t, "linux-ck", queryBuilder.aurQuery[0].Name)
  106. if tc.bottomUp {
  107. assert.Equal(t, "linux-zen", queryBuilder.repoQuery[0].Name())
  108. assert.Equal(t, "linux", queryBuilder.repoQuery[1].Name())
  109. } else {
  110. assert.Equal(t, "linux-zen", queryBuilder.repoQuery[1].Name())
  111. assert.Equal(t, "linux", queryBuilder.repoQuery[0].Name())
  112. }
  113. w := &strings.Builder{}
  114. queryBuilder.Results(w, mockStore, Detailed)
  115. wString := w.String()
  116. require.GreaterOrEqual(t, len(wString), 1)
  117. assert.Equal(t, tc.want, wString)
  118. })
  119. }
  120. }