Skip to content

Commit

Permalink
Merge pull request #604 from Shopify/relative-pagination-api-version
Browse files Browse the repository at this point in the history
Change cursor pagination API version to 2019-10
  • Loading branch information
garethson authored Aug 13, 2019
2 parents a287396 + 4e4ed0b commit fc620af
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/shopify_api/collection_pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def fetch_previous_page

private

AVAILABLE_IN_VERSION = ShopifyAPI::ApiVersion::Unstable.new
AVAILABLE_IN_VERSION = ShopifyAPI::ApiVersion::Release.new('2019-10')

def fetch_page(url_params)
ensure_available
Expand Down
51 changes: 37 additions & 14 deletions test/pagination_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ class PaginationTest < Test::Unit::TestCase
def setup
super

@version = ShopifyAPI::ApiVersion::Unstable.new
@version = ShopifyAPI::ApiVersion::Release.new('2019-10')
ShopifyAPI::Base.api_version = @version.to_s
@next_page_info = "eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NDQwMDg5NDIzLCJsYXN0X3ZhbHVlIjoiNDQwMDg5NDIzIn0%3D"
@previous_page_info = "eyJsYXN0X2lkIjoxMDg4MjgzMDksImxhc3RfdmFsdWUiOiIxMDg4MjgzMDkiLCJkaXJlY3Rpb24iOiJuZXh0In0%3D"

@next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}>; rel=\"next\""
@previous_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@previous_page_info}>; rel=\"previous\""
@next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?page_info=#{@next_page_info}>; rel=\"next\""
@previous_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?page_info=#{@previous_page_info}>; rel=\"previous\""
end

test "navigates using next and previous link headers with no original params" do
Expand All @@ -21,7 +21,7 @@ def setup

fake(
'orders',
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}",
url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?page_info=#{@next_page_info}",
method: :get,
status: 200,
body: load_fixture('orders')
Expand All @@ -31,7 +31,7 @@ def setup

fake(
'orders',
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@previous_page_info}",
url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?page_info=#{@previous_page_info}",
method: :get,
status: 200,
body: load_fixture('orders').gsub("450789469", "1122334455")
Expand All @@ -43,13 +43,13 @@ def setup

test "uses all passed in querystring parameters" do
params = "page_info=#{@next_page_info}&limit=50&fields=#{CGI.escape('id,created_at')}"
@next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?#{params}>; rel=\"next\""
@next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?#{params}>; rel=\"next\""
fake(
'orders',
method: :get,
status: 200,
api_version: @version,
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Cupdated_at&limit=100",
url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?fields=id%2Cupdated_at&limit=100",
body: load_fixture('orders'),
link: @next_link_header
)
Expand All @@ -60,7 +60,7 @@ def setup
method: :get,
status: 200,
api_version: @version,
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?fields=id%2Ccreated_at&limit=50&page_info=#{@next_page_info}",
url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?fields=id%2Ccreated_at&limit=50&page_info=#{@next_page_info}",
body: load_fixture('orders')
)
next_page = orders.fetch_next_page
Expand Down Expand Up @@ -122,7 +122,7 @@ def setup
end

test "raises on invalid pagination links" do
link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}>;"
link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?page_info=#{@next_page_info}>;"
fake 'orders', :method => :get, :status => 200, api_version: @version, :body => load_fixture('orders'), :link => link_header

assert_raises ShopifyAPI::InvalidPaginationLinksError do
Expand All @@ -142,16 +142,39 @@ def setup
end
end

test "does not raise on the unstable version" do
version = ShopifyAPI::ApiVersion::Unstable.new
ShopifyAPI::Base.api_version = version.to_s
@next_link_header = "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}>; rel=\"next\""

link_header ="#{@previous_link_header}, #{@next_link_header}"

fake 'orders', :method => :get, :status => 200, api_version: version, :body => load_fixture('orders'), :link => link_header
orders = ShopifyAPI::Order.all

fake(
'orders',
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?page_info=#{@next_page_info}",
method: :get,
status: 200,
body: load_fixture('orders')
)
assert_nothing_raised do
next_page = orders.fetch_next_page
assert_equal 450789469, next_page.first.id
end
end

test "allows for multiple concurrent API collection objects" do
first_request_params = "page_info=#{@next_page_info}&limit=5"
fake(
'orders',
method: :get,
status: 200,
api_version: @version,
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?limit=5",
url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?limit=5",
body: load_fixture('orders'),
link: "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?#{first_request_params}>; rel=\"next\""
link: "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?#{first_request_params}>; rel=\"next\""
)
orders = ShopifyAPI::Order.where(limit: 5)

Expand All @@ -161,9 +184,9 @@ def setup
method: :get,
status: 200,
api_version: @version,
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?limit=10",
url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?limit=10",
body: load_fixture('orders'),
link: "<https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?#{second_request_params}>; rel=\"next\""
link: "<https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?#{second_request_params}>; rel=\"next\""
)

orders2 = ShopifyAPI::Order.where(limit: 10)
Expand All @@ -173,7 +196,7 @@ def setup
method: :get,
status: 200,
api_version: @version,
url: "https://this-is-my-test-shop.myshopify.com/admin/api/unstable/orders.json?limit=5&page_info=#{@next_page_info}",
url: "https://this-is-my-test-shop.myshopify.com/admin/api/2019-10/orders.json?limit=5&page_info=#{@next_page_info}",
body: load_fixture('orders')
)
next_page = orders.fetch_next_page
Expand Down

0 comments on commit fc620af

Please sign in to comment.