source_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package query
  2. import (
  3. "bytes"
  4. "context"
  5. "io"
  6. "net/http"
  7. "testing"
  8. "github.com/Jguer/aur/rpc"
  9. "github.com/Jguer/yay/v12/pkg/db"
  10. "github.com/Jguer/yay/v12/pkg/db/mock"
  11. "github.com/Jguer/yay/v12/pkg/settings/parser"
  12. "github.com/Jguer/yay/v12/pkg/text"
  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: http.StatusOK,
  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. client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{}))
  94. require.NoError(t, err)
  95. queryBuilder := NewSourceQueryBuilder(client,
  96. text.NewLogger(io.Discard, bytes.NewBufferString(""), false, "test"),
  97. "votes", parser.ModeAny, "", tc.bottomUp, false, true)
  98. search := []string{"linux"}
  99. mockStore := &mockDB{}
  100. queryBuilder.Execute(context.Background(), mockStore, search)
  101. assert.Equal(t, 3, queryBuilder.Len())
  102. if tc.bottomUp {
  103. assert.Equal(t, "linux-ck", queryBuilder.results[0].name)
  104. assert.Equal(t, "linux-zen", queryBuilder.results[1].name)
  105. assert.Equal(t, "linux", queryBuilder.results[2].name)
  106. } else {
  107. assert.Equal(t, "linux-ck", queryBuilder.results[2].name)
  108. assert.Equal(t, "linux-zen", queryBuilder.results[1].name)
  109. assert.Equal(t, "linux", queryBuilder.results[0].name)
  110. }
  111. queryBuilder.Results(mockStore, Detailed)
  112. })
  113. }
  114. }