pagination_test.rb 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class PaginationTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. @version = ShopifyAPI::ApiVersion.find_version('2019-10')
  7. ShopifyAPI::Base.api_version = @version.to_s
  8. @next_page_info = "eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NDQwMDg5NDIzLCJsYXN0X3ZhbHVlIjoiNDQwMDg5NDIzIn0%3D"
  9. @previous_page_info = "eyJsYXN0X2lkIjoxMDg4MjgzMDksImxhc3RfdmFsdWUiOiIxMDg4MjgzMDkiLCJkaXJlY3Rpb24iOiJuZXh0In0%3D"
  10. @next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/" \
  11. "orders.json?page_info=#{@next_page_info}>; rel=\"next\""
  12. @previous_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/" \
  13. "orders.json?page_info=#{@previous_page_info}>; rel=\"previous\""
  14. end
  15. test "navigates using next and previous link headers with no original params" do
  16. link_header = "#{@previous_link_header}, #{@next_link_header}"
  17. fake('orders', method: :get, status: 200, api_version: @version, body: load_fixture('orders'), link: link_header)
  18. orders = ShopifyAPI::Order.all
  19. fake(
  20. 'orders',
  21. url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?page_info=#{@next_page_info}",
  22. method: :get,
  23. status: 200,
  24. body: load_fixture('orders')
  25. )
  26. next_page = orders.fetch_next_page
  27. assert_equal(450789469, next_page.first.id)
  28. fake(
  29. 'orders',
  30. url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?page_info=#{@previous_page_info}",
  31. method: :get,
  32. status: 200,
  33. body: load_fixture('orders').gsub("450789469", "1122334455")
  34. )
  35. previous_page = orders.fetch_previous_page
  36. assert_equal(1122334455, previous_page.first.id)
  37. end
  38. test "uses all passed in querystring parameters" do
  39. params = "page_info=#{@next_page_info}&limit=50&fields=#{CGI.escape('id,created_at')}"
  40. @next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/" \
  41. "orders.json?#{params}>; rel=\"next\""
  42. fake(
  43. 'orders',
  44. method: :get,
  45. status: 200,
  46. api_version: @version,
  47. url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?fields=id%2Cupdated_at&limit=100",
  48. body: load_fixture('orders'),
  49. link: @next_link_header
  50. )
  51. orders = ShopifyAPI::Order.where(fields: 'id,updated_at', limit: 100)
  52. fake(
  53. 'orders',
  54. method: :get,
  55. status: 200,
  56. api_version: @version,
  57. url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/" \
  58. "orders.json?fields=id%2Ccreated_at&limit=50&page_info=#{@next_page_info}",
  59. body: load_fixture('orders')
  60. )
  61. next_page = orders.fetch_next_page
  62. assert_equal(450789469, next_page.first.id)
  63. end
  64. test "returns empty next page if just the previous page is present" do
  65. fake(
  66. 'orders', method: :get, status: 200, api_version: @version,
  67. body: load_fixture('orders'), link: @previous_link_header
  68. )
  69. orders = ShopifyAPI::Order.all
  70. next_page = orders.fetch_next_page
  71. assert_empty(next_page)
  72. end
  73. test "returns an empty previous page if just the next page is present" do
  74. fake(
  75. 'orders', method: :get, status: 200, api_version: @version, body: load_fixture('orders'), link: @next_link_header
  76. )
  77. orders = ShopifyAPI::Order.all
  78. next_page = orders.fetch_previous_page
  79. assert_empty(next_page)
  80. end
  81. test "#next_page? returns true if next page is present" do
  82. fake(
  83. 'orders', method: :get, status: 200, api_version: @version, body: load_fixture('orders'), link: @next_link_header
  84. )
  85. orders = ShopifyAPI::Order.all
  86. assert(orders.next_page?)
  87. end
  88. test "#next_page? returns false if next page is not present" do
  89. fake(
  90. 'orders', method: :get, status: 200, api_version: @version,
  91. body: load_fixture('orders'), link: @previous_link_header
  92. )
  93. orders = ShopifyAPI::Order.all
  94. refute(orders.next_page?)
  95. end
  96. test "#next_page_info returns next_page_info if next page is present" do
  97. fake(
  98. 'orders', method: :get, status: 200, api_version: @version, body: load_fixture('orders'), link: @next_link_header
  99. )
  100. orders = ShopifyAPI::Order.all
  101. assert_equal(@next_page_info, orders.next_page_info)
  102. end
  103. test "#next_page_info returns nil if next page is not present" do
  104. fake(
  105. 'orders', method: :get, status: 200, api_version: @version,
  106. body: load_fixture('orders'), link: @previous_link_header
  107. )
  108. orders = ShopifyAPI::Order.all
  109. assert_nil(orders.next_page_info)
  110. end
  111. test "#previous_page? returns true if previous page is present" do
  112. fake(
  113. 'orders', method: :get, status: 200, api_version: @version,
  114. body: load_fixture('orders'), link: @previous_link_header
  115. )
  116. orders = ShopifyAPI::Order.all
  117. assert(orders.previous_page?)
  118. end
  119. test "#previous_page? returns false if next page is not present" do
  120. fake(
  121. 'orders', method: :get, status: 200, api_version: @version, body: load_fixture('orders'), link: @next_link_header
  122. )
  123. orders = ShopifyAPI::Order.all
  124. refute(orders.previous_page?)
  125. end
  126. test "#previous_page_info returns previous_page_info if next page is present" do
  127. fake(
  128. 'orders', method: :get, status: 200, api_version: @version,
  129. body: load_fixture('orders'), link: @previous_link_header
  130. )
  131. orders = ShopifyAPI::Order.all
  132. assert_equal(@previous_page_info, orders.previous_page_info)
  133. end
  134. test "#previous_page_info returns nil if next page is not present" do
  135. fake(
  136. 'orders', method: :get, status: 200, api_version: @version, body: load_fixture('orders'), link: @next_link_header
  137. )
  138. orders = ShopifyAPI::Order.all
  139. assert_nil(orders.previous_page_info)
  140. end
  141. test "pagination handles no link headers" do
  142. fake('orders', method: :get, status: 200, api_version: @version, body: load_fixture('orders'))
  143. orders = ShopifyAPI::Order.all
  144. refute(orders.next_page?)
  145. refute(orders.previous_page?)
  146. assert_empty(orders.fetch_next_page)
  147. assert_empty(orders.fetch_previous_page)
  148. end
  149. test "raises on invalid pagination links" do
  150. link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/" \
  151. "orders.json?page_info=#{@next_page_info}>;"
  152. fake('orders', method: :get, status: 200, api_version: @version, body: load_fixture('orders'), link: link_header)
  153. assert_raises(ShopifyAPI::InvalidPaginationLinksError) do
  154. ShopifyAPI::Order.all
  155. end
  156. end
  157. test "raises on an older API version" do
  158. version = ShopifyAPI::ApiVersion.find_version('2019-04')
  159. ShopifyAPI::Base.api_version = version.to_s
  160. fake('orders', method: :get, status: 200, api_version: version, body: load_fixture('orders'))
  161. orders = ShopifyAPI::Order.all
  162. assert_raises(NotImplementedError) do
  163. orders.fetch_next_page
  164. end
  165. end
  166. test "raises on 2019-07 API version for models that don't support new pagination yet" do
  167. version = ShopifyAPI::ApiVersion.find_version('2019-07')
  168. ShopifyAPI::Base.api_version = version.to_s
  169. fake('orders', method: :get, status: 200, api_version: version, body: load_fixture('orders'))
  170. orders = ShopifyAPI::Order.all
  171. assert_raises(NotImplementedError) do
  172. orders.fetch_next_page
  173. end
  174. end
  175. test "new pagination works on 2019-07 API version for select models" do
  176. version = ShopifyAPI::ApiVersion.find_version('2019-07')
  177. ShopifyAPI::Base.api_version = version.to_s
  178. fake('events', method: :get, status: 200, api_version: version, body: load_fixture('events'))
  179. events = ShopifyAPI::Event.all
  180. assert_empty(events.fetch_next_page)
  181. assert_empty(events.fetch_previous_page)
  182. end
  183. test "does not raise on the unstable version" do
  184. version = ShopifyAPI::ApiVersion.find_version('unstable')
  185. ShopifyAPI::Base.api_version = version.to_s
  186. @next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/" \
  187. "orders.json?page_info=#{@next_page_info}>; rel=\"next\""
  188. link_header = "#{@previous_link_header}, #{@next_link_header}"
  189. fake('orders', method: :get, status: 200, api_version: version, body: load_fixture('orders'), link: link_header)
  190. orders = ShopifyAPI::Order.all
  191. fake(
  192. 'orders',
  193. url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}",
  194. method: :get,
  195. status: 200,
  196. body: load_fixture('orders')
  197. )
  198. assert_nothing_raised do
  199. next_page = orders.fetch_next_page
  200. assert_equal(450789469, next_page.first.id)
  201. end
  202. end
  203. test "allows for multiple concurrent API collection objects" do
  204. first_request_params = "page_info=#{@next_page_info}&limit=5"
  205. fake(
  206. 'orders',
  207. method: :get,
  208. status: 200,
  209. api_version: @version,
  210. url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?limit=5",
  211. body: load_fixture('orders'),
  212. link: "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/" \
  213. "orders.json?#{first_request_params}>; rel=\"next\""
  214. )
  215. ShopifyAPI::Order.where(limit: 5)
  216. second_request_params = "page_info=#{@next_page_info}&limit=5"
  217. fake(
  218. 'orders',
  219. method: :get,
  220. status: 200,
  221. api_version: @version,
  222. url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?limit=10",
  223. body: load_fixture('orders'),
  224. link: "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/" \
  225. "orders.json?#{second_request_params}>; rel=\"next\""
  226. )
  227. orders = ShopifyAPI::Order.where(limit: 10)
  228. fake(
  229. 'orders',
  230. method: :get,
  231. status: 200,
  232. api_version: @version,
  233. url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/" \
  234. "orders.json?limit=5&page_info=#{@next_page_info}",
  235. body: load_fixture('orders')
  236. )
  237. next_page = orders.fetch_next_page
  238. assert_equal(450789469, next_page.first.id)
  239. end
  240. end