nealrichardson / httptest2

Utilities for testing R 📦s that use httr2
https://enpiar.com/httptest2/
Other
26 stars 5 forks source link

mocking iterative requests #45

Open asadow opened 2 months ago

asadow commented 2 months ago

This is more for a discussion, and is a copy of an issue I thought might belong in httr2.

My function my_perform() uses httr2::req_perform_iterative() which has a max_reqs argument (default to 20). I'd like to test that my_perform() performs an infinite or very large number of iterative requests.

For now I am testing the argument:

test_that("my_perform() performs Inf requests by default", {
  expect_equal(formals(my_perform)$max_reqs, Inf)
})

Is this ideal or is it instead better to mock a large request? I created a fake response with data removed which allows for

with_mock_api({
  test_that("my_perform() returns API page_count responses", {
    responses<- my_req("my_endpoint") |> my_perform()
    expect_length(responses, 21)
  })
})

(And I don't understand why the above requires 2 mock files, endpoint.R and endpoint-6fdf2d.R.)

nealrichardson commented 1 month ago

(Moved this from httptest to httptest2)

I don't think you want to test that it actually makes an infinite number of requests. I would do what I gather you're doing with the fake responses, and test that your function does the iteration as expected, and you get the right results that would only get if it correctly paged through the API as you expect.

(And I don't understand why the above requires 2 mock files, endpoint.R and endpoint-6fdf2d.R.)

I can't say without seeing your code, but my guess is that the iterative next_req function you have is adding a query parameter to fetch the next page of results or whatever, and 6fdf2d is the hash of that added query string.