inbo / etn

R package to access data from the European Tracking Network
https://inbo.github.io/etn/
MIT License
6 stars 5 forks source link

Test if API and local SQL queries result in same objects #330

Open PietrH opened 1 month ago

PietrH commented 1 month ago

During #317 I noticed 9 functions return a different result between the API and a local SQL query.

Some of these differences might be fixed, or at least different after merging #328

API - SQL mismatch

mismatches for:


          Maybe this is a good use case for a [custom expectation](https://testthat.r-lib.org/articles/custom-expectation.html).

For example, you could create a expect_call_agnostic(), with one argument: a function call. The helper could grab the function call (and its arguments), run it locally and over API and expect the result to be the same.

It could then be called as part of a regular test-function.R file:


testthat("get_animals() returns same result locally and via API", {
  expect_call_agnostic(get_animals(animal_id = "5"))
})

_Originally posted by @peterdesmet in https://github.com/inbo/etn/issues/317#issuecomment-2421841943_
PietrH commented 1 month ago

lil :penguin: script to check for these functions:

for(function_to_test in getNamespaceExports("etnservice")) {
  test_that(paste(
    function_to_test,
    "returns same result over api as over local db connection"
  ),
  {
    skip_if_offline()
    skip("not all functions work with no arguments")

    api_result <- do.call(function_to_test, list(api = TRUE))
    sql_result <- do.call(function_to_test, list(api = FALSE))
    expect_identical(api_result, sql_result, label = function_to_test)
  })
}

for(function_to_test in getNamespaceExports("etnservice")){
  print(function_to_test)
  api_result <- do.call(function_to_test, list(api = TRUE))
  sql_result <- do.call(function_to_test, list(api = FALSE))
  expect_identical(api_result, sql_result, label = function_to_test)
}

test_result <- purrr::map(
  getNamespaceExports("etnservice"),
  purrr::safely(function(function_to_test){
    api_result <- do.call(function_to_test, list(api = TRUE))
    sql_result <- do.call(function_to_test, list(api = FALSE))
    expect_identical(api_result, sql_result)
  }
)) %>% purrr::set_names(getNamespaceExports("etnservice"))

# only keep those with errors
test_result[purrr::map_lgl(test_result, ~!is.null(purrr::pluck(.x,"error")))]
PietrH commented 2 weeks ago
skip_if(Sys.info()["nodename"] != "rshiny", "Test is not running on Lifewatch RStudio Server")

Or even better:

skip_if("ETN" %in% odbc::odbcListDataSources()$name, "ETN is not a local database on this machine")