From 36843751f0b0d815c871cbbdd54b2c15f7205b45 Mon Sep 17 00:00:00 2001
From: Dorchies David <david.dorchies@inrae.fr>
Date: Thu, 5 Aug 2021 11:59:40 +0200
Subject: [PATCH] feat(doApiQuery): add check for the limitation of 20000
 records

- add functionnality and test

Closes ##5
---
 DESCRIPTION                      | 3 +++
 R/doApiQuery.R                   | 4 ++++
 man/doApiQuery.Rd                | 2 +-
 tests/testthat.R                 | 4 ++++
 tests/testthat/test-doApiQuery.R | 4 ++++
 5 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 tests/testthat.R
 create mode 100644 tests/testthat/test-doApiQuery.R

diff --git a/DESCRIPTION b/DESCRIPTION
index 5fd965a..1e57fc9 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -17,3 +17,6 @@ Imports:
     urltools
 RoxygenNote: 7.1.1
 Roxygen: list(markdown = TRUE)
+Suggests: 
+    testthat (>= 3.0.0)
+Config/testthat/edition: 3
diff --git a/R/doApiQuery.R b/R/doApiQuery.R
index 2b61f45..1b691b6 100644
--- a/R/doApiQuery.R
+++ b/R/doApiQuery.R
@@ -86,6 +86,10 @@ doApiQuery <- function(api,
       }
     } else {
       l <- httr::content(resp, "parsed")
+      if (as.numeric(l$count) > 20000) {
+        stop("The request reach the API limitation of 20000 records.\n",
+             "Use filter arguments to reduce the number of records of your request.")
+      }
       data <- c(data, l$data)
       if (resp$status_code == 206) {
         query <- l$`next`
diff --git a/man/doApiQuery.Rd b/man/doApiQuery.Rd
index afd4a33..ee6c9e9 100644
--- a/man/doApiQuery.Rd
+++ b/man/doApiQuery.Rd
@@ -38,7 +38,7 @@ Pagination of the queries is handled automatically and the returned \link{list}
 \details{
 The function \code{get_available_params} returns the list of available query parameters for a given operation in an API.
 
-The functions \verb{get_[api]_[operation]} call the function \code{doQueryApi} and convert the response in a convenient format for the user (\link{data.frame} or \link{tibble})
+The functions \verb{get_[api]_[operation]} call the function \code{doQueryApi} and convert the response in a convenient format for the user (\link{data.frame} or \link[tibble:tibble]{tibble::tibble})
 }
 \examples{
 # To get the available APIs in the package
diff --git a/tests/testthat.R b/tests/testthat.R
new file mode 100644
index 0000000..510a4f1
--- /dev/null
+++ b/tests/testthat.R
@@ -0,0 +1,4 @@
+library(testthat)
+library(hubeau)
+
+test_check("hubeau")
diff --git a/tests/testthat/test-doApiQuery.R b/tests/testthat/test-doApiQuery.R
new file mode 100644
index 0000000..1508a2a
--- /dev/null
+++ b/tests/testthat/test-doApiQuery.R
@@ -0,0 +1,4 @@
+test_that("A query of more than 20000 records", {
+  expect_error(doApiQuery("indicateurs_services", "communes", params = list()),
+               regexp = "The request reach the API limitation of 20000 records")
+})
-- 
GitLab