source_test.go 4.1 KB

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