source_test.go 4.1 KB

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