-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature/webmockr #161
feature/webmockr #161
Conversation
This looks extremely promising, @shaun-jacks! Thank you so much for working on this. 🙌 I am trying to figure out what is going on with this mocking to see why the tests are not passing. First, did the tests pass for you locally? Second, check out what I am seeing locally: library(qualtRics)
library(webmockr)
webmockr::enable()
#> CrulAdapter enabled!
#> HttrAdapter enabled!
expected <- list(
verb = "GET",
base_url = "test.qualtrics.com",
url = "test.qualtrics.com/API/v3/surveys/",
api_key = "1234",
headers = list(
'X-API-TOKEN' = "1234",
'Content-Type' = 'application/json',
'Accept' = '*/*',
'accept-encoding' = 'gzip, deflate'
)
)
qualtrics_api_credentials(expected$api_key, expected$base_url)
#> To install your credentials for use in future sessions, run this function with `install = TRUE`.
stub_request(expected$verb, expected$url) %>%
wi_th(headers = expected$headers)
#> <webmockr stub>
#> method: get
#> uri: test.qualtrics.com/API/v3/surveys/
#> with:
#> query:
#> body:
#> request_headers: X-API-TOKEN=1234, Content-Type=application/json, Accept=*/*, accept-encoding=gzi
#> to_return:
#> status:
#> body:
#> response_headers:
#> should_timeout: FALSE
#> should_raise: FALSE
## the stub is there
stub_registry()
#> <webmockr stub registry>
#> Registered Stubs
#> GET: test.qualtrics.com/API/v3/surveys/ with headers {"X-API-TOKEN":"1234","Content-Type":"application/json","Accept":"*/*","accept-encoding":"gzip, deflate"}
## why is this request unregistered???
all_surveys()
#> Error: Real HTTP connections are disabled.
#> Unregistered request:
#> GET: test.qualtrics.com/API/v3/surveys/ with headers {X-API-TOKEN: 1234, Content-Type: application/json, Accept: */*, accept-encoding: gzip, deflate}
#>
#> You can stub this request with the following snippet:
#>
#> stub_request('get', uri = 'test.qualtrics.com/API/v3/surveys/') %>%
#> wi_th(
#> headers = list('X-API-TOKEN' = '1234', 'Content-Type' = 'application/json', 'Accept' = '*/*', 'accept-encoding' = 'gzip, deflate')
#> )
#>
#> registered request stubs:
#>
#> GET: test.qualtrics.com/API/v3/surveys/ with headers {"X-API-TOKEN":"1234","Content-Type":"application/json","Accept":"*/*","accept-encoding":"gzip, deflate"}
#> ============================================================
## I could register it again...
stub_request('get', uri = 'test.qualtrics.com/API/v3/surveys/') %>%
wi_th(
headers = list('X-API-TOKEN' = '1234', 'Content-Type' = 'application/json', 'Accept' = '*/*', 'accept-encoding' = 'gzip, deflate')
)
#> <webmockr stub>
#> method: get
#> uri: test.qualtrics.com/API/v3/surveys/
#> with:
#> query:
#> body:
#> request_headers: X-API-TOKEN=1234, Content-Type=application/json, Accept=*/*, accept-encoding=gzi
#> to_return:
#> status:
#> body:
#> response_headers:
#> should_timeout: FALSE
#> should_raise: FALSE
## but then there are 2!
stub_registry()
#> <webmockr stub registry>
#> Registered Stubs
#> GET: test.qualtrics.com/API/v3/surveys/ with headers {"X-API-TOKEN":"1234","Content-Type":"application/json","Accept":"*/*","accept-encoding":"gzip, deflate"}
#> GET: test.qualtrics.com/API/v3/surveys/ with headers {"X-API-TOKEN":"1234","Content-Type":"application/json","Accept":"*/*","accept-encoding":"gzip, deflate"}
## still doesn't work :(
all_surveys()
#> Error: Real HTTP connections are disabled.
#> Unregistered request:
#> GET: test.qualtrics.com/API/v3/surveys/ with headers {X-API-TOKEN: 1234, Content-Type: application/json, Accept: */*, accept-encoding: gzip, deflate}
#>
#> You can stub this request with the following snippet:
#>
#> stub_request('get', uri = 'test.qualtrics.com/API/v3/surveys/') %>%
#> wi_th(
#> headers = list('X-API-TOKEN' = '1234', 'Content-Type' = 'application/json', 'Accept' = '*/*', 'accept-encoding' = 'gzip, deflate')
#> )
#>
#> registered request stubs:
#>
#> GET: test.qualtrics.com/API/v3/surveys/ with headers {"X-API-TOKEN":"1234","Content-Type":"application/json","Accept":"*/*","accept-encoding":"gzip, deflate"}
#> GET: test.qualtrics.com/API/v3/surveys/ with headers {"X-API-TOKEN":"1234","Content-Type":"application/json","Accept":"*/*","accept-encoding":"gzip, deflate"}
#> ============================================================
webmockr::disable()
#> CrulAdapter disabled!
#> HttrAdapter disabled! Created on 2020-05-06 by the reprex package (v0.3.0) The exact stub that it says it needs appears to be registered but it doesn't seem to find it or use it or something. Did you see anything like this? |
Hi @juliasilge, all my tests do pass locally, but yes this has happened to me as well while developing. Normally I would do a |
@juliasilge I also noticed that this may work in some cases:
Notice how the verb is in all caps, I think this happened to me once, and making the verb all CAPS got the stub to work |
Hmmmm, I have tried those steps and I have yet to get any of the mocked requests to succeed, caps, no caps, defined as strings or passed in as variables, etc etc etc. You can see that
Something clearly is not quite right, but I think this PR is on the right track and we should get it moving forward. |
Aww I see :/ well here is my sessionInfo anyway, thanks for looking into this! And for opening an issue with webmockr as well.
|
If there's anymore I could do I'll be happy to help |
OK @shaun-jacks this is good to go now thanks to the help I got on using webmockr!! Would you like to add yourself as a contributor ( |
Wow I'm really glad we were able to get it working! Completely forgot to add https to the urls! And I would like to, thank you! :) |
Done! |
Between this and #140 we went from ~50% to ~80% testing coverage today! 💥 |
Hello @juliasilge,
This PR replaces httptest with webmockr. I added unit tests to check http requests on Qualtrics APIs for various functions in the package. And also added other misc. tests.
I went with webmockr because mocking the http requests and testing the http request structure felt like it made the most sense for this package. It could be difficult authenticating with Qualtrics and caching actual requests during CI, and I don't think it's necessary to do this / I am not really sure how to go about that to be honest. This PR also has tests for additional qualtRics functions such as
qualtrics_api_request
,metadata
, anddownload_qualtrics_export
, where the http request structure is tested via stubbing the expected request.I did my best not to modify any of the library code, and only add tests code, to ensure backwards compatibility. I only removed the package code that was related to httptest within
download_qualtrics_export
.It was a little challenging structuring the tests for these functions, so a future idea could be to refactor some of the code to make it more test friendly! I had to mock specific functions in order to get the tests to both run and test specific things.
I have more experience with tests in other languages, but not much experience within R, so any feedback would be much appreciated :) One thing is there is a lot of setup code per test, wish R had a beforeEach type of function... I hope this helps at least provide a base to integrating webmockr and making the testing more robust! Also would like to know if there are any other library or continuous integration modifications needed to integrate this?