Skip to content
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

Check api functions to increase Code Coverage #6

Merged
merged 8 commits into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Suggests:
reprex,
rmarkdown,
testthat (>= 3.0.0),
httptest,
kpiyush04 marked this conversation as resolved.
Show resolved Hide resolved
vcr
VignetteBuilder:
knitr
Expand Down
35 changes: 35 additions & 0 deletions tests/fixtures/check_last_audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
http_interactions:
- request:
method: get
uri: https://bugs.r-project.org/bugzilla/rest/last_audit_time
body:
encoding: ''
string: ''
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: My bearer token is safe
X-BUGZILLA-API-KEY: Removing this header too just in case
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
date: Sat, 12 Jun 2021 22:38:08 GMT
server: gazelle
etag: /kF0g2q98GJLV5YGfBdm7Q
content-length: '42'
content-type: application/json; charset=UTF-8
access-control-allow-headers: accept, content-type, origin, user-agent, x-requested-with,
x-bugzilla-api-key, x-bugzilla-login, x-bugzilla-token, x-bugzilla-password
access-control-allow-origin: '*'
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
body:
encoding: UTF-8
file: no
string: '{"last_audit_time":"2021-06-09T23:47:55Z"}'
recorded_at: 2021-06-12 22:38:08 GMT
recorded_with: vcr/0.6.0, webmockr/0.8.0
1 change: 1 addition & 0 deletions tests/fixtures/set_key.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

84 changes: 84 additions & 0 deletions tests/testthat/_snaps/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# create_bugzilla_key() works [plain]

Code
create_bugzilla_key(host = missing_host())
Message <cliMessage>
i Reading cached keys on '/home/data/.cache/R/bugRzilla/.Renviron'.
v Found key `R_BUGZILLA`.
v Using key `R_BUGZILLA`.
x Not authenticated on this site.
v Authenticated on this site!

# create_bugzilla_key() works [unicode]

Code
create_bugzilla_key(host = missing_host())
Message <cliMessage>
ℹ Reading cached keys on '/home/data/.cache/R/bugRzilla/.Renviron'.
✔ Found key `R_BUGZILLA`.
✔ Using key `R_BUGZILLA`.
✖ Not authenticated on this site.
✔ Authenticated on this site!

# check_key() works [plain]

Code
check_key(key_name = missing_key(), verbose = TRUE)
Message <cliMessage>
i Reading cached keys on '/home/data/.cache/R/bugRzilla/.Renviron'.
v Found key `R_BUGZILLA`.
Output
[1] TRUE

# check_key() works [unicode]

Code
check_key(key_name = missing_key(), verbose = TRUE)
Message <cliMessage>
ℹ Reading cached keys on '/home/data/.cache/R/bugRzilla/.Renviron'.
✔ Found key `R_BUGZILLA`.
Output
[1] TRUE

# use_key() works [plain]

Code
use_key(missing_key())
Message <cliMessage>
v Using key `R_BUGZILLA`.

# use_key() works [unicode]

Code
use_key(missing_key())
Message <cliMessage>
✔ Using key `R_BUGZILLA`.

# valid_key() works [plain]

Code
valid_key(key = missing_key())
Output
[1] TRUE

# valid_key() works [ansi]

Code
valid_key(key = missing_key())
Output
[1] TRUE

# valid_key() works [unicode]

Code
valid_key(key = missing_key())
Output
[1] TRUE

# valid_key() works [fancy]

Code
valid_key(key = missing_key())
Output
[1] TRUE

4 changes: 2 additions & 2 deletions tests/testthat/setup-bugRzilla.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use_key()
invisible(vcr::vcr_configure(
dir = vcr_dir,
filter_request_headers = list(
"Authorization" = "My bearer token is safe",
"X-BUGZILLA-API-KEY" = "Removing this header too just in case")
"Authorization" = "My bearer token is safe",
"X-BUGZILLA-API-KEY" = "Removing this header too just in case")
kpiyush04 marked this conversation as resolved.
Show resolved Hide resolved
))
vcr::check_cassette_names()
56 changes: 56 additions & 0 deletions tests/testthat/test-api.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This is to check the create_bugzilla_key function
cli::test_that_cli(configs = c("plain", "unicode"), "create_bugzilla_key() works", {
expect_snapshot({
create_bugzilla_key(host = missing_host())
kpiyush04 marked this conversation as resolved.
Show resolved Hide resolved
})
})


# This is to check the set_key function
test_that("set_key works", {
vcr::use_cassette("set_key", {
kpiyush04 marked this conversation as resolved.
Show resolved Hide resolved
sk <- set_key()
})
expect_equal(write_renviron(key = sk, value = sk, file = app_file()), NULL)
})


# This is to check the check_key function
cli::test_that_cli(configs = c("plain", "unicode"), "check_key() works", {
expect_snapshot({
check_key(key_name = missing_key(), verbose = TRUE)
kpiyush04 marked this conversation as resolved.
Show resolved Hide resolved
})
})

# cli::test_that_cli(configs = c("plain", "unicode"), "check_key() fails", {
# expect_snapshot({
# !check_key(key_name = missing_key(), verbose = TRUE)
# })
# })


# This is to check the use_key function
cli::test_that_cli(configs = c("plain", "unicode"), "use_key() works", {
expect_snapshot({
use_key(missing_key())
})
})


# This is to check the check_last_audit function
test_that("check_last_audit works", {
vcr::use_cassette("check_last_audit", {
cla <- check_last_audit(missing_product(), missing_host())
kpiyush04 marked this conversation as resolved.
Show resolved Hide resolved
})
expect_equal(missing_host(), "https://bugs.r-project.org/bugzilla/")
expect_equal(missing_product(), "R")
expect_s3_class(GET(paste0(missing_host(), "rest/last_audit_time")), "response")
kpiyush04 marked this conversation as resolved.
Show resolved Hide resolved
})


# This is to check the valid_key function
cli::test_that_cli("valid_key() works", {
expect_snapshot({
valid_key(key = missing_key())
kpiyush04 marked this conversation as resolved.
Show resolved Hide resolved
})
})
21 changes: 21 additions & 0 deletions tests/testthat/test-bug_search.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
valid_parameters <- c("alias", "assigned_to", "component", "creation_time",
"creator", "id", "last_change_time", "limit",
"longdescs.count", "offset", "op_sys", "platform",
"priority", "product", "resolution", "severity",
"status", "summary", "tags", "target_milestone",
"qa_contact", "url", "version", "whiteboard",
"quicksearch")

# test_that("bug_search works", {
# vcr::use_cassette("bug_search", {
# bs <- bug_search(id=1)
# url <- "https://bugs.r-project.org/bugzilla/rest/bug?id=1"
# })
# expect_equal(paste0(missing_host(), "rest/bug?", params(id=1)), url)
# })

test_that("params works", {
par <- params()
expect_true(all(names(par) %in% valid_parameters))
expect_error(params(ids=1))
})
18 changes: 18 additions & 0 deletions tests/testthat/test-post_help.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# yes <- c("Yes", "Definitely", "Positive", "For sure", "Yup", "Yeah",
# "Absolutely")
# no <- c("Not sure", "Not now", "Negative", "No", "Nope", "Absolutely not")
#
# cli::test_that_cli(configs = c("plain"), "read_documentation() works",{
# expect_snapshot({
# expect_equal(read_documentation(), "Cancel")
# # expect_equal(read_documentation(), no)
# # expect_equal(read_documentation(), yes)
# })
# })

# test_that("ask_confirmation works", {
# vcr::use_cassette("ask_confirmation", {
# ac <- ask_confirmation(title = NULL, positive = yes, negative = no)
# })
# expect_equal(sample(positive, 1), sample(negative, 2))
# })
kpiyush04 marked this conversation as resolved.
Show resolved Hide resolved