From ee04d66fa9e1565626d098049c091d11553d479c Mon Sep 17 00:00:00 2001 From: Jacek Kutzmann Date: Mon, 14 Nov 2016 11:23:04 +0100 Subject: [PATCH] VEL-1521 fixed pagination links --- src/fhir/search_helpers.coffee | 10 +--- test/fhir/search/pagination_search.yaml | 73 ++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/fhir/search_helpers.coffee b/src/fhir/search_helpers.coffee index b213282..b376c63 100644 --- a/src/fhir/search_helpers.coffee +++ b/src/fhir/search_helpers.coffee @@ -14,11 +14,11 @@ exports.search_links = (query, expr, total)-> base_url = base_url + "&_page=0" requested_count = expr.count - requested_page = expr.page + requested_page = expr.page ? 0 res.push({relation: 'self', url: base_url}) - if requested_count && requested_count < total + if requested_count && requested_count * (requested_page + 1) < total next_url = base_url.replace /_page=\d+/, -> "_page=#{requested_page + 1}" res.push({relation: 'next', url: next_url}) @@ -27,11 +27,7 @@ exports.search_links = (query, expr, total)-> res.push({relation: 'previous', url: next_url}) if requested_count - last_page = if requested_count < total - Math.floor(total / requested_count) - else - requested_page - + last_page = Math.max(0, Math.ceil(total / requested_count) - 1) last_url = base_url.replace /_page=\d+/, -> "_page=#{last_page}" res.push({relation: 'last', url: last_url}) diff --git a/test/fhir/search/pagination_search.yaml b/test/fhir/search/pagination_search.yaml index ae5390a..d7e570b 100644 --- a/test/fhir/search/pagination_search.yaml +++ b/test/fhir/search/pagination_search.yaml @@ -62,7 +62,7 @@ queries: - path: ['link', 2, 'url'] result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=1" - path: ['link', 3, 'url'] - result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=3&_count=1" + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=2&_count=1" - query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=1&_count=1&_totalMethod=exact'} total: 3 @@ -76,10 +76,79 @@ queries: - path: ['link', 2, 'url'] result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=1&_totalMethod=exact" - path: ['link', 3, 'url'] - result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=3&_count=1&_totalMethod=exact" + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=2&_count=1&_totalMethod=exact" - query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=1&_count=1&_totalMethod=no'} total: _undefined probes: - path: ['link'] result: '_undefined' + + - query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_count=3'} + total: 3 + probes: + - path: ['entry', 'length'] + result: 3 + - path: ['link', 0, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_count=3&_page=0" + - path: ['link', 1, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_count=3&_page=0" + - path: ['link', 2, 'url'] + + - query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=0&_count=3'} + total: 3 + probes: + - path: ['entry', 'length'] + result: 3 + - path: ['link', 0, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=3" + - path: ['link', 1, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=3" + - path: ['link', 2, 'url'] + + - query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=2&_count=3'} + total: 3 + probes: + - path: ['entry', 'length'] + result: 0 + - path: ['link', 0, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=2&_count=3" + - path: ['link', 1, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=1&_count=3" + - path: ['link', 2, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=3" + - path: ['link', 3, 'url'] + + - query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=1&_count=2'} + total: 3 + probes: + - path: ['entry', 'length'] + result: 1 + - path: ['link', 0, 'relation'] + result: "self" + - path: ['link', 0, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=1&_count=2" + - path: ['link', 1, 'relation'] + result: "previous" + - path: ['link', 1, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=2" + - path: ['link', 2, 'relation'] + result: "last" + - path: ['link', 2, 'url'] + result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=1&_count=2" + - path: ['link', 3, 'url'] + + - query: {resourceType: 'Encounter', queryString: 'patient=Patient/nonexistent&_sort=patient&_page=0&_count=3'} + total: 0 + probes: + - path: ['entry', 'length'] + result: 0 + - path: ['link', 0, 'relation'] + result: "self" + - path: ['link', 0, 'url'] + result: "/Encounter?patient=Patient/nonexistent&_sort=patient&_page=0&_count=3" + - path: ['link', 1, 'relation'] + result: "last" + - path: ['link', 1, 'url'] + result: "/Encounter?patient=Patient/nonexistent&_sort=patient&_page=0&_count=3" + - path: ['link', 2, 'url']