ropensci / vcr

Record HTTP calls and replay them
https://docs.ropensci.org/vcr
Other
77 stars 12 forks source link

Testing API calls to write into the database #223

Closed llrs closed 3 years ago

llrs commented 3 years ago

I come this other issue https://github.com/ropensci-books/http-testing/issues/84.

I would like to test that writing using the API works. So far I tested retrieving data from the API (of the R bug tracker), bu obviously I don't want to write superfluous issues as it is "on production".

What would be the best way to test it using vcr, but without writing to the database?

Session Info ```r ? Session info ???????????????????????????????????????????????????????????????????????? setting value version R version 4.0.1 (2020-06-06) os Ubuntu 20.04.2 LTS system x86_64, linux-gnu ui RStudio language (EN) collate en_US.UTF-8 ctype en_US.UTF-8 tz Europe/Madrid date 2021-02-14 ? Packages ???????????????????????????????????????????????????????????????????????????? ! package * version date lib source assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.1) backports 1.2.1 2020-12-09 [1] CRAN (R 4.0.1) base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.0.1) BiocManager * 1.30.10 2019-11-16 [1] CRAN (R 4.0.1) R bugRzilla * 0.0.90001 [?] cachem 1.0.3 2021-02-04 [1] CRAN (R 4.0.1) callr 3.5.1 2020-10-13 [1] CRAN (R 4.0.1) cli 2.3.0 2021-01-31 [1] CRAN (R 4.0.1) covr 3.5.1 2020-09-16 [1] CRAN (R 4.0.1) crayon 1.4.1 2021-02-08 [1] CRAN (R 4.0.1) crosstalk 1.1.1 2021-01-12 [1] CRAN (R 4.0.1) crul 1.0.0 2020-07-30 [1] CRAN (R 4.0.1) curl 4.3 2019-12-02 [1] CRAN (R 4.0.1) cyclocomp * 1.1.0 2016-09-10 [1] CRAN (R 4.0.1) desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.1) devtools * 2.3.2 2020-09-18 [1] CRAN (R 4.0.1) diffobj 0.3.3 2021-01-07 [1] CRAN (R 4.0.1) digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.1) DT 0.17 2021-01-06 [1] CRAN (R 4.0.1) ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.1) evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.1) fansi 0.4.2 2021-01-15 [1] CRAN (R 4.0.1) fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.0.1) fauxpas 0.5.0 2020-04-13 [1] CRAN (R 4.0.1) fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.1) glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.1) htmltools 0.5.1.1 2021-01-22 [1] CRAN (R 4.0.1) htmlwidgets 1.5.3 2020-12-10 [1] CRAN (R 4.0.1) httpcode 0.3.0 2020-04-10 [1] CRAN (R 4.0.1) httr 1.4.2 2020-07-20 [1] CRAN (R 4.0.1) jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.0.1) knitr 1.31 2021-01-27 [1] CRAN (R 4.0.1) lazyeval 0.2.2 2019-03-15 [1] CRAN (R 4.0.1) lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.1) magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.1) memoise 2.0.0 2021-01-26 [1] CRAN (R 4.0.1) pillar 1.4.7 2020-11-20 [1] CRAN (R 4.0.1) pkgbuild 1.2.0 2020-12-15 [1] CRAN (R 4.0.1) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.1) pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.1) prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.1) processx 3.4.5 2020-11-30 [1] CRAN (R 4.0.1) ps 1.5.0 2020-12-05 [1] CRAN (R 4.0.1) purrr 0.3.4 2020-04-17 [1] CRAN (R 4.0.1) R.cache 0.14.0 2019-12-06 [1] CRAN (R 4.0.1) R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.0.1) R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.0.1) R.utils 2.10.1 2020-08-26 [1] CRAN (R 4.0.1) R6 2.5.0 2020-10-28 [1] CRAN (R 4.0.1) Rcpp 1.0.6 2021-01-15 [1] CRAN (R 4.0.1) rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.0.1) remotes 2.2.0 2020-07-21 [1] CRAN (R 4.0.1) rex 1.2.0 2020-04-21 [1] CRAN (R 4.0.1) rlang 0.4.10 2020-12-30 [1] CRAN (R 4.0.1) rmarkdown 2.6 2020-12-14 [1] CRAN (R 4.0.1) rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.0.1) rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.0.1) sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.1) styler 1.3.2 2020-02-23 [1] CRAN (R 4.0.1) testthat * 3.0.1 2020-12-17 [1] CRAN (R 4.0.1) tibble 3.0.6 2021-01-29 [1] CRAN (R 4.0.1) triebeard 0.3.0 2016-08-04 [1] CRAN (R 4.0.1) urltools 1.7.3 2019-04-14 [1] CRAN (R 4.0.1) usethis * 2.0.1 2021-02-10 [1] CRAN (R 4.0.1) vcr 0.6.0 2020-12-12 [1] CRAN (R 4.0.1) vctrs 0.3.6 2020-12-17 [1] CRAN (R 4.0.1) waldo 0.2.3 2020-11-09 [1] CRAN (R 4.0.1) webmockr 0.7.4 2020-12-09 [1] CRAN (R 4.0.1) whisker 0.4 2019-08-28 [1] CRAN (R 4.0.1) withr 2.4.1 2021-01-26 [1] CRAN (R 4.0.1) xfun 0.20 2021-01-06 [1] CRAN (R 4.0.1) xml2 1.3.2 2020-04-23 [1] CRAN (R 4.0.1) yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.1) [1] /home/lluis/bin/R/4.0.1/lib/R/library R ?? Package was removed from disk. ```
sckott commented 3 years ago

I'm a little confused here. What does "writing to the database" mean? The database is local on your computer? Or there's a database behind the API referenced?

Most likely the answer is to use webmockr, but will wait for more details

llrs commented 3 years ago

Sorry :sweat: to write to a remote computer on the database behind the API referenced. The API given the right authentication allows to write/use POST to insert new records to the database behind it.

sckott commented 3 years ago

Okay, thanks.

If you can write once to the API that accepts a POST request inside of a use_cassette code block, then you can from there on use that cassette created on that one call.

If you don't even want to one real request then you could construct a cassette from scratch - there's not really tools to do that in vcr, but you could just copy/paste then edit another cassette.

Or webmockr is an option - in which you could match on the http request - but don't allow any real request - and you can set the body to be returned (json, list, whatever)

llrs commented 3 years ago

Great! I didn't know how to proceed from the book. Will report back there. One again thanks! In this case the API is R bugzilla, so I would rather avoid sending a mock issue. Will see how I manage copying/building a cassette.

sckott commented 3 years ago

okay, hope something works out well