entry_query_builder_integration_test.go 11 KB

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