From 2380373b87684ad79e748b8818d7cc91b6936ac8 Mon Sep 17 00:00:00 2001 From: kpiyush04 <04kpiyush@gmail.com> Date: Thu, 3 Jun 2021 18:11:43 +0530 Subject: [PATCH] Increased Code Coverage --- DESCRIPTION | 4 +- NAMESPACE | 1 - R/api.R | 1 - tests/fixtures/set_key.yml | 1 + tests/testthat/_snaps/api.md | 104 +++++++++++++++++++++++++++++++ tests/testthat/setup-bugRzilla.R | 64 +------------------ tests/testthat/test-api.R | 59 ++++++++++++++++++ 7 files changed, 169 insertions(+), 65 deletions(-) create mode 100644 tests/fixtures/set_key.yml create mode 100644 tests/testthat/_snaps/api.md create mode 100644 tests/testthat/test-api.R diff --git a/DESCRIPTION b/DESCRIPTION index e09841e..e7615a4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,14 +17,14 @@ Imports: httr, methods, utils, - tools, - httptest + tools Suggests: covr, knitr, reprex, rmarkdown, testthat (>= 3.0.0), + httptest, vcr VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index c05d995..8fb5035 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,7 +12,6 @@ export(get_history) export(post_bug) export(post_comment) export(set_key) -import(httptest) importFrom(cli,cat_rule) importFrom(cli,cli_alert) importFrom(cli,cli_alert_danger) diff --git a/R/api.R b/R/api.R index 5795bca..800fb98 100644 --- a/R/api.R +++ b/R/api.R @@ -17,7 +17,6 @@ missing_key <- function(key) { #' @importFrom cli cli_ul #' @importFrom utils browseURL #' @export -#' @import httptest create_bugzilla_key <- function(host) { host <- missing_host(host) url <- paste0(host, "userprefs.cgi?tab=apikey") diff --git a/tests/fixtures/set_key.yml b/tests/fixtures/set_key.yml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/fixtures/set_key.yml @@ -0,0 +1 @@ + diff --git a/tests/testthat/_snaps/api.md b/tests/testthat/_snaps/api.md new file mode 100644 index 0000000..cf26cfe --- /dev/null +++ b/tests/testthat/_snaps/api.md @@ -0,0 +1,104 @@ +# cli_alert_warning works [plain] + + Code + cli::cli_alert_warning( + "This package hasn't been tested with this Bugzilla version.") + Message + ! This package hasn't been tested with this Bugzilla version. + +# cli_alert_warning works [ansi] + + Code + cli::cli_alert_warning( + "This package hasn't been tested with this Bugzilla version.") + Message + ! This package hasn't been tested with this Bugzilla version. + +# cli_alert_warning works [unicode] + + Code + cli::cli_alert_warning( + "This package hasn't been tested with this Bugzilla version.") + Message + ! This package hasn't been tested with this Bugzilla version. + +# cli_alert_warning works [fancy] + + Code + cli::cli_alert_warning( + "This package hasn't been tested with this Bugzilla version.") + Message + ! This package hasn't been tested with this Bugzilla version. + +# cli_alert_danger [plain] + + Code + cli::cli_alert_danger("Not authenticated on this site.") + Message + x Not authenticated on this site. + Code + cli::cli_alert_success("Authenticated on this site!") + Message + v Authenticated on this site! + +# cli_alert_danger [ansi] + + Code + cli::cli_alert_danger("Not authenticated on this site.") + Message + x Not authenticated on this site. + Code + cli::cli_alert_success("Authenticated on this site!") + Message + v Authenticated on this site! + +# cli_alert_danger [unicode] + + Code + cli::cli_alert_danger("Not authenticated on this site.") + Message + ✖ Not authenticated on this site. + Code + cli::cli_alert_success("Authenticated on this site!") + Message + ✔ Authenticated on this site! + +# cli_alert_danger [fancy] + + Code + cli::cli_alert_danger("Not authenticated on this site.") + Message + ✖ Not authenticated on this site. + Code + cli::cli_alert_success("Authenticated on this site!") + Message + ✔ Authenticated on this site! + +# cli_alert_dangers [plain] + + Code + cli::cli_alert_danger("Key not valid or not set.") + Message + x Key not valid or not set. + +# cli_alert_dangers [ansi] + + Code + cli::cli_alert_danger("Key not valid or not set.") + Message + x Key not valid or not set. + +# cli_alert_dangers [unicode] + + Code + cli::cli_alert_danger("Key not valid or not set.") + Message + ✖ Key not valid or not set. + +# cli_alert_dangers [fancy] + + Code + cli::cli_alert_danger("Key not valid or not set.") + Message + ✖ Key not valid or not set. + diff --git a/tests/testthat/setup-bugRzilla.R b/tests/testthat/setup-bugRzilla.R index a48750b..4c9e171 100644 --- a/tests/testthat/setup-bugRzilla.R +++ b/tests/testthat/setup-bugRzilla.R @@ -1,5 +1,5 @@ library("vcr") # *Required* as vcr is set up on loading -library("httptest") +library("httptest") # *Required* as hhtptest is set up on loading vcr_dir <- vcr::vcr_test_path("fixtures") if (!nzchar(Sys.getenv("R_BUGZILLA"))) { @@ -13,68 +13,10 @@ if (!nzchar(Sys.getenv("R_BUGZILLA"))) { use_key() -test_that("create_bugzilla_key works", { - vcr::use_cassette("create_bugzilla_key", { - host <- "https://bugs.r-project.org/bugzilla/" - cbk <- create_bugzilla_key(host) - }) - expect_equal(a <- paste0(host, "userprefs.cgi?tab=apikey"), url) - cli::test_that_cli("cli_alert_warning works", { - expect_snapshot({ - cli::cli_alert_warning("This package hasn't been tested with this Bugzilla version.") - }) - }) -}) - - -test_that("check_authentication works", { - vcr::use_cassette("check_authentication", { - ca <- check_authentication(host) - }) - expect_equal(missing_host(host), "https://bugs.r-project.org/bugzilla/") - cli::test_that_cli("cli_alert_danger",{ - expect_snapshot({ - cli::cli_alert_danger("Not authenticated on this site.") - cli::cli_alert_success("Authenticated on this site!") - }) - }) -}) - -test_that("check_api_version works", { - vcr::use_cassette("check_api_version", { - cav <- check_api_version(host) - }) - expect_equal(missing_host(host), "https://bugs.r-project.org/bugzilla/") -}) - - -test_that("check_last_audit works", { - vcr::use_cassette("check_last_audit", { - product <- "R" - cla <- check_last_audit(product, host) - }) - expect_equal(missing_host(host), "https://bugs.r-project.org/bugzilla/") - httptest::expect_GET(paste0(host, "rest/last_audit_time"), "https://bugs.r-project.org/bugzilla/rest/last_audit_time") - expect_equal(missing_product(product), "R") -}) - - 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") )) vcr::check_cassette_names() - - -test_that("valid_key works", { - use_cassette("valid_key", { - vk <- valid_key("R_BUGZILLA") - }) - cli::test_that_cli("cli_alert_danger", { - expect_snapshot({ - cli::cli_alert_danger("Key not valid or not set.") - }) - }) -}) diff --git a/tests/testthat/test-api.R b/tests/testthat/test-api.R new file mode 100644 index 0000000..0d2ac24 --- /dev/null +++ b/tests/testthat/test-api.R @@ -0,0 +1,59 @@ +test_that("create_bugzilla_key works", { + vcr::use_cassette("create_bugzilla_key", { + cbk <- create_bugzilla_key(missing_host()) + url <- "https://bugs.r-project.org/bugzilla/userprefs.cgi?tab=apikey" + }) + expect_equal(paste0(missing_host(), "userprefs.cgi?tab=apikey"), url) + expect_condition() + cli::test_that_cli("cli_alert_warning works", { + expect_snapshot({ + cli::cli_alert_warning("This package hasn't been tested with this Bugzilla version.") + }) + }) +}) + + +test_that("check_authentication works", { + vcr::use_cassette("check_authentication", { + ca <- check_authentication(missing_host()) + }) + expect_equal(missing_host(), "https://bugs.r-project.org/bugzilla/") + cli::test_that_cli("cli_alert_danger",{ + expect_snapshot({ + cli::cli_alert_danger("Not authenticated on this site.") + cli::cli_alert_success("Authenticated on this site!") + }) + }) +}) + + +test_that("check_api_version works", { + vcr::use_cassette("check_api_version", { + cav <- check_api_version(missing_host()) + }) + expect_equal(missing_host(), "https://bugs.r-project.org/bugzilla/") +}) + + +test_that("check_last_audit works", { + vcr::use_cassette("check_last_audit", { + cla <- check_last_audit(missing_product(), missing_host()) + }) + 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") +}) + + +test_that("valid_key works", { + use_cassette("valid_key", { + vk <- valid_key(use_key()) + }) + cli::test_that_cli("cli_alert_dangers", { + expect_snapshot({ + cli::cli_alert_danger("Key not valid or not set.") + }) + }) +}) + +snapshot_accept('R/api.R')