entry_query_builder_integration_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package storage
  4. import (
  5. "slices"
  6. "testing"
  7. "time"
  8. "miniflux.app/v2/internal/model"
  9. )
  10. func newEntryQueryTestStorage(t *testing.T) *Storage {
  11. t.Helper()
  12. db := openTestDatabase(t)
  13. // Keep temporary tables on the same connection and leave the database unchanged.
  14. db.SetMaxOpenConns(1)
  15. _, err := db.Exec(`
  16. SET TIME ZONE 'UTC';
  17. CREATE TEMP TABLE users (LIKE users INCLUDING DEFAULTS);
  18. CREATE TEMP TABLE categories (LIKE categories INCLUDING DEFAULTS);
  19. CREATE TEMP TABLE feeds (LIKE feeds INCLUDING DEFAULTS);
  20. CREATE TEMP TABLE entries (LIKE entries INCLUDING DEFAULTS);
  21. CREATE TEMP TABLE feed_icons (LIKE feed_icons INCLUDING DEFAULTS);
  22. CREATE TEMP TABLE icons (LIKE icons INCLUDING DEFAULTS);
  23. INSERT INTO users (id, username) VALUES (1, 'entry-query-user'), (2, 'entry-query-other');
  24. INSERT INTO categories (id, user_id, title, hide_globally) VALUES
  25. (10, 1, 'Zulu category', false),
  26. (20, 1, 'Alpha category', false),
  27. (30, 1, 'Hidden category', true),
  28. (40, 2, 'Other category', false);
  29. INSERT INTO feeds (id, user_id, category_id, title, feed_url, site_url, hide_globally) VALUES
  30. (11, 1, 10, 'Alpha feed', 'https://example.org/11', 'https://example.org', false),
  31. (12, 1, 20, 'Zulu feed', 'https://example.org/12', 'https://example.org', false),
  32. (13, 1, 10, 'Hidden feed', 'https://example.org/13', 'https://example.org', true),
  33. (14, 1, 30, 'Visible feed in hidden category', 'https://example.org/14', 'https://example.org', false),
  34. (15, 1, 30, 'Hidden feed in hidden category', 'https://example.org/15', 'https://example.org', true),
  35. (16, 2, 40, 'Other feed', 'https://example.org/16', 'https://example.org', false);
  36. INSERT INTO entries (id, user_id, feed_id, hash, title, url, author, content, status, starred,
  37. published_at, created_at, changed_at, document_vectors, tags) VALUES
  38. (1, 1, 11, '1', 'Zulu entry', 'https://example.org/1', 'Bravo', 'shared content', 'unread', false,
  39. '2025-01-03', '2025-01-02', '2025-01-04', to_tsvector('shared'), ARRAY['one', 'two']),
  40. (2, 1, 12, '2', 'Alpha entry', 'https://example.org/2', 'Charlie', 'shared content', 'read', true,
  41. '2025-01-01', '2025-01-03', '2025-01-02', to_tsvector('shared'), ARRAY['one']),
  42. (3, 1, 11, '3', 'Alpha entry', 'https://example.org/3', 'Alpha', 'shared content', 'unread', false,
  43. '2025-01-02', '2025-01-01', '2025-01-03', to_tsvector('shared'), ARRAY['one', 'two']),
  44. (4, 1, 13, '4', 'Hidden feed entry', 'https://example.org/4', '', '', 'unread', true,
  45. '2025-01-01', '2025-01-01', '2025-01-01', to_tsvector('hidden'), ARRAY[]::text[]),
  46. (5, 1, 14, '5', 'Hidden category entry', 'https://example.org/5', '', '', 'read', false,
  47. '2025-01-01', '2025-01-01', '2025-01-01', to_tsvector('hidden'), ARRAY[]::text[]),
  48. (6, 1, 15, '6', 'Both hidden entry', 'https://example.org/6', '', '', 'unread', false,
  49. '2025-01-01', '2025-01-01', '2025-01-01', to_tsvector('hidden'), ARRAY[]::text[]),
  50. (7, 2, 16, '7', 'Other user entry', 'https://example.org/7', '', '', 'unread', true,
  51. '2025-01-01', '2025-01-01', '2025-01-01', to_tsvector('shared'), ARRAY['one', 'two']);
  52. `)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. return NewStorage(db)
  57. }
  58. func TestGetEntryIDsSorting(t *testing.T) {
  59. store := newEntryQueryTestStorage(t)
  60. for _, test := range []struct {
  61. order string
  62. ids []int64
  63. }{
  64. {"id", []int64{1, 2, 3}},
  65. {"status", []int64{1, 3, 2}},
  66. {"changed_at", []int64{2, 3, 1}},
  67. {"published_at", []int64{2, 3, 1}},
  68. {"created_at", []int64{3, 1, 2}},
  69. {"category_title", []int64{2, 1, 3}},
  70. {"category_id", []int64{1, 3, 2}},
  71. {"title", []int64{2, 3, 1}},
  72. {"author", []int64{3, 1, 2}},
  73. } {
  74. for _, direction := range []string{"asc", "desc"} {
  75. t.Run(test.order+"/"+direction, func(t *testing.T) {
  76. want := slices.Clone(test.ids)
  77. if direction == "desc" {
  78. slices.Reverse(want)
  79. }
  80. builder := store.NewEntryQueryBuilder(1).
  81. WithEntryIDs(1, 2, 3).
  82. WithSorting(test.order, direction).
  83. WithSorting("id", direction)
  84. ids, err := builder.GetEntryIDs()
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if !slices.Equal(ids, want) {
  89. t.Fatalf("Got IDs %v, want %v", ids, want)
  90. }
  91. entries, err := builder.GetEntries()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if len(entries) != len(ids) {
  96. t.Fatalf("Got %d entries, want %d", len(entries), len(ids))
  97. }
  98. for index, entry := range entries {
  99. if entry.ID != ids[index] {
  100. t.Fatalf("Entry %d has ID %d, want %d", index, entry.ID, ids[index])
  101. }
  102. }
  103. })
  104. }
  105. }
  106. }
  107. func TestGetEntryIDsFiltersAndCount(t *testing.T) {
  108. store := newEntryQueryTestStorage(t)
  109. for _, test := range []struct {
  110. name string
  111. configure func(*EntryQueryBuilder) *EntryQueryBuilder
  112. ids []int64
  113. }{
  114. {"all including hidden", func(builder *EntryQueryBuilder) *EntryQueryBuilder { return builder }, []int64{1, 2, 3, 4, 5, 6}},
  115. {"globally visible", func(builder *EntryQueryBuilder) *EntryQueryBuilder { return builder.WithGloballyVisible() }, []int64{1, 2, 3}},
  116. {"hidden feed", func(builder *EntryQueryBuilder) *EntryQueryBuilder {
  117. return builder.WithFeedID(13).WithGloballyVisible()
  118. }, nil},
  119. {"hidden category", func(builder *EntryQueryBuilder) *EntryQueryBuilder {
  120. return builder.WithCategoryID(30).WithGloballyVisible()
  121. }, nil},
  122. {"matching scope", func(builder *EntryQueryBuilder) *EntryQueryBuilder { return builder.WithFeedID(11).WithCategoryID(10) }, []int64{1, 3}},
  123. {"mismatched scope", func(builder *EntryQueryBuilder) *EntryQueryBuilder { return builder.WithFeedID(11).WithCategoryID(20) }, nil},
  124. {"unread", func(builder *EntryQueryBuilder) *EntryQueryBuilder {
  125. return builder.WithStatuses(model.EntryStatusUnread)
  126. }, []int64{1, 3, 4, 6}},
  127. {"read", func(builder *EntryQueryBuilder) *EntryQueryBuilder {
  128. return builder.WithStatuses(model.EntryStatusRead)
  129. }, []int64{2, 5}},
  130. {"both statuses", func(builder *EntryQueryBuilder) *EntryQueryBuilder {
  131. return builder.WithStatuses(model.EntryStatusRead, model.EntryStatusUnread)
  132. }, []int64{1, 2, 3, 4, 5, 6}},
  133. {"starred", func(builder *EntryQueryBuilder) *EntryQueryBuilder { return builder.WithStarred(true) }, []int64{2, 4}},
  134. {"not starred", func(builder *EntryQueryBuilder) *EntryQueryBuilder { return builder.WithStarred(false) }, []int64{1, 3, 5, 6}},
  135. {"combined", func(builder *EntryQueryBuilder) *EntryQueryBuilder {
  136. return builder.WithGloballyVisible().WithStatuses(model.EntryStatusUnread).WithTags("ONE", "two").WithSearchQuery("shared")
  137. }, []int64{1, 3}},
  138. {"strict ID boundaries", func(builder *EntryQueryBuilder) *EntryQueryBuilder { return builder.AfterEntryID(1).BeforeEntryID(3) }, []int64{2}},
  139. {"strict time boundaries", func(builder *EntryQueryBuilder) *EntryQueryBuilder {
  140. return builder.AfterPublishedDate(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)).
  141. BeforePublishedDate(time.Date(2025, 1, 3, 0, 0, 0, 0, time.UTC))
  142. }, []int64{3}},
  143. } {
  144. t.Run(test.name, func(t *testing.T) {
  145. builder := test.configure(store.NewEntryQueryBuilder(1).WithSorting("id", "asc"))
  146. ids, total, err := builder.GetEntryIDsWithCount()
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. if !slices.Equal(ids, test.ids) || total != len(test.ids) {
  151. t.Fatalf("Got IDs %v and total %d, want %v and %d", ids, total, test.ids, len(test.ids))
  152. }
  153. for offset := 0; offset <= len(test.ids); offset++ {
  154. builder.WithLimitAndMaximum(1, model.MaxEntryIDsLimit).WithOffset(offset)
  155. ids, total, err := builder.GetEntryIDsWithCount()
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. want := test.ids[offset:min(offset+1, len(test.ids))]
  160. if !slices.Equal(ids, want) || total != len(test.ids) {
  161. t.Fatalf("Offset %d: got IDs %v and total %d, want %v and %d", offset, ids, total, want, len(test.ids))
  162. }
  163. }
  164. })
  165. }
  166. }
  167. func TestGetEntryIDsStablePagination(t *testing.T) {
  168. const pageSize = 25
  169. store := newEntryQueryTestStorage(t)
  170. _, err := store.db.Exec(`
  171. DELETE FROM entries;
  172. INSERT INTO entries (id, user_id, feed_id, hash, title, url, author, content,
  173. published_at, created_at, changed_at, document_vectors)
  174. SELECT n, 1, 11, n::text, 'Shared title', 'https://example.org/' || n, 'Shared author', '',
  175. '2025-01-01', '2025-01-01', '2025-01-01', to_tsvector('shared')
  176. FROM generate_series(1, 120) AS n;
  177. `)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. for _, order := range []string{"id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author"} {
  182. for _, direction := range []string{"asc", "desc"} {
  183. want := make([]int64, 120)
  184. for index := range want {
  185. want[index] = int64(index + 1)
  186. }
  187. if direction == "desc" {
  188. slices.Reverse(want)
  189. }
  190. for _, search := range []string{"", "shared"} {
  191. t.Run(order+"/"+direction+"/search="+search, func(t *testing.T) {
  192. var got []int64
  193. for offset := 0; offset < len(want); offset += pageSize {
  194. ids, total, err := store.NewEntryQueryBuilder(1).
  195. WithSorting(order, direction).
  196. WithSearchQuery(search).
  197. WithSorting("id", direction).
  198. WithLimit(pageSize).
  199. WithOffset(offset).
  200. GetEntryIDsWithCount()
  201. if err != nil {
  202. t.Fatal(err)
  203. }
  204. if total != len(want) {
  205. t.Fatalf("Got total %d, want %d", total, len(want))
  206. }
  207. got = append(got, ids...)
  208. }
  209. if !slices.Equal(got, want) {
  210. t.Fatalf("Got IDs %v, want %v", got, want)
  211. }
  212. })
  213. }
  214. }
  215. }
  216. }
  217. func TestGetEntryIDsSearchSorting(t *testing.T) {
  218. store := newEntryQueryTestStorage(t)
  219. for _, test := range []struct {
  220. direction string
  221. ids []int64
  222. }{
  223. {"asc", []int64{3, 2, 1}},
  224. {"desc", []int64{1, 3, 2}},
  225. } {
  226. t.Run(test.direction, func(t *testing.T) {
  227. ids, err := store.NewEntryQueryBuilder(1).
  228. WithSorting("title", test.direction).
  229. WithSearchQuery("shared").
  230. WithSorting("id", test.direction).
  231. GetEntryIDs()
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. if !slices.Equal(ids, test.ids) {
  236. t.Fatalf("Got IDs %v, want %v", ids, test.ids)
  237. }
  238. })
  239. }
  240. }
  241. func TestGetEntryIDsWithoutEntryData(t *testing.T) {
  242. store := newEntryQueryTestStorage(t)
  243. if _, err := store.db.Exec(`ALTER TABLE entries DROP COLUMN content`); err != nil {
  244. t.Fatal(err)
  245. }
  246. ids, err := store.NewEntryQueryBuilder(1).
  247. WithEntryIDs(1, 2, 3).
  248. WithSorting("category_title", "asc").
  249. WithSorting("id", "asc").
  250. WithEnclosures().
  251. GetEntryIDs()
  252. if err != nil {
  253. t.Fatal(err)
  254. }
  255. if !slices.Equal(ids, []int64{2, 1, 3}) {
  256. t.Fatalf("Got IDs %v, want [2 1 3]", ids)
  257. }
  258. }
  259. func TestGetEntryIDsCompatibilityQueries(t *testing.T) {
  260. store := newEntryQueryTestStorage(t)
  261. // Fever fetches unread and starred IDs without sorting or pagination.
  262. for _, test := range []struct {
  263. builder *EntryQueryBuilder
  264. ids []int64
  265. }{
  266. {store.NewEntryQueryBuilder(1).WithStatuses(model.EntryStatusUnread), []int64{1, 3, 4, 6}},
  267. {store.NewEntryQueryBuilder(1).WithStarred(true), []int64{2, 4}},
  268. } {
  269. ids, err := test.builder.GetEntryIDs()
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. slices.Sort(ids)
  274. if !slices.Equal(ids, test.ids) {
  275. t.Fatalf("Got IDs %v, want %v", ids, test.ids)
  276. }
  277. }
  278. // Google Reader sorts IDs by publication date and uses limit/offset.
  279. ids, err := store.NewEntryQueryBuilder(1).
  280. WithFeedID(11).
  281. WithSorting(model.DefaultSortingOrder, "desc").
  282. WithLimitAndMaximum(1, model.MaxEntryIDsLimit).
  283. WithOffset(1).
  284. GetEntryIDs()
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. if !slices.Equal(ids, []int64{3}) {
  289. t.Fatalf("Got IDs %v, want [3]", ids)
  290. }
  291. }