Giter VIP home page Giter VIP logo

callthat's Introduction

callthat

R-CMD-check Lifecycle: experimental CRAN status Codecov test coverage

callthat is meant for plumber API developers who plan to distribute their APIs inside an R package. This package enables the testing of the API’s endpoints within the testthat framework. This allows the R Checks to also confirm that the endpoints still behave as expected.

The ultimate goal of callthat is to ensure that the package’s automated testing catches issues with the API even if the developer did not perform unit testing.

Installation

The development version from GitHub with:

# install.packages("devtools")
devtools::install_github("edgararuiz/callthat")

Usage

plumber API in your package

Place your API in the inst/plumber folder of your package. That way plumber::available_apis("MyPakcageName") can locate the APIs, after your package is installed.

In MyPakcageName/inst/plumber/sample-api:

library(plumber)

#* Runs a prediction of the Weight
#* @get /predict
function(weight) {
  wtg <- as.numeric(weight)
  model <- stats::lm(mpg ~ wt, data = datasets::mtcars)
  stats::predict(model, newdata = data.frame(wt = wtg))
}

Inside the testthat folder

library(testthat)
library(callthat)
library(httr)

Create the test using the same name as the name of the API. Add the prefix test-plumber.

In MyPakcageName/tests/testthat/test-plumber-sample-api.R:

test_that("Show how to use test", {
  expect_silent({
    
    # ------------- Start plumber API ------------------- 
    local_api <- call_that_plumber_start(
      system.file("plumber/sample-api", package = "MyPackageName")
      )
    
    # ------------- Start test session ------------------ 
    api_session <- call_that_session_start(local_api)
  })
  expect_s3_class(
    
    # ---------------- Make API call --------------------
    get_predict <- call_that_api_get(
      api_session, 
      endpoint = "predict", 
      query = list(weight = 1)
      ),
    "response"
  )
  
  # ---------- Run tests against response ---------------
  ## Test to confirm that the response was a success
  expect_equal(
    get_predict$status_code,
    200
  )
  ## Test to confirm the output of the API is correct
  expect_equal(
    round(content(get_predict)[[1]]),
    32
  )
  
  # ----- Close the session, and the plumber API --------
  expect_null(
    call_that_session_stop(api_session)
  )
})

Managing the API tests

callthat matches the API to the test by name. The call_that_available_tests() returns a table with the available APIs in your package, and it displays if there is a matching test.

call_that_available_tests()
#> # A tibble: 1 x 4
#>   api        api_path              test_exists test_path                        
#>   <chr>      <fs::path>            <lgl>       <fs::path>                       
#> 1 sample-api inst/plumber/sample-… TRUE        inst/tests/test-plumber-sample-a…

Run tests against the published API

callthat makes it possible to run the exact same tests used for unit testing, for integration testing.

Open a connection with the published API. If that API is running in RStudio Connect, then use call_that_rsc_connection(), otherwise use the generic call_that_connection().

published_api <- call_that_rsc_connection(
  url = "https://colorado.rstudio.com/rsc/callthat/testapi",
  key = Sys.getenv("RSC_TOKEN") # Optional, only if access to your API is restricted
  )

published_api
#> RStudio Connect API:  https://colorado.rstudio.com/rsc/callthat/testapi
#> API Key: XXXXXXXXXXXXXX

The call_that_test_remote() functions runs the tests for the API, but it is possible to also pass the published API’s connection so that the tests run remotely.

call_that_test_remote("sample-api", published_api)
#> ✓ |  OK F W S | Context
#> ✓ |   5       | plumber-sample-api
#> 
#> ══ Results ═════════════════════════════════════════════════════════════════════
#> [ FAIL 0 | WARN 0 | SKIP 0 | PASS 5 ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.