pagination_test.rb 9.8 KB

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