Skip to content

Commit

Permalink
fix: pass check
Browse files Browse the repository at this point in the history
- Fix tests that were failing since V0 alpha
- Fix tests within template project mariobox

Issue #6 and #7
  • Loading branch information
ALanguillaume committed Jan 4, 2023
1 parent f7df478 commit 4b30f8e
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 37 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ importFrom(rstudioapi,openProject)
importFrom(usethis,create_project)
importFrom(usethis,use_r)
importFrom(usethis,use_test)
importFrom(utils,file.edit)
importFrom(utils,getFromNamespace)
importFrom(yaml,read_yaml)
importFrom(yaml,write_yaml)
4 changes: 4 additions & 0 deletions R/dep_port.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#' @noRd
#' @importFrom utils file.edit
usethis_use_r <- function(
name,
pkg = ".",
Expand All @@ -24,6 +26,8 @@ usethis_use_r <- function(
file.edit(name)
}
}
#' @noRd
#' @importFrom utils file.edit
usethis_use_test <- function(
name = NULL,
pkg = ".",
Expand Down
13 changes: 13 additions & 0 deletions dev/dev_history.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ devtools::check()

devtools::test()


## Project template

marioboxexample_path <- normalizePath("inst/marioboxexample/")

attachment::att_amend_desc(
path = marioboxexample_path
)

devtools::test(
pkg = marioboxexample_path
)

### Setup ----------------------------------------------------------------------

.use_r_with_test("create_mariobox")
Expand Down
12 changes: 3 additions & 9 deletions inst/marioboxexample/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ Package: marioboxexample
Title: A cunning plumber API
Version: 0.0.0.9000
Authors@R:
person(given = "firstname",
family = "lastname",
role = c("aut", "cre"),
email = "[email protected]")
person("firstname", "lastname", , "[email protected]", role = c("aut", "cre"))
Description: What the package does (one paragraph).
License: What license is it under?
Imports:
mariobox,
plumber,
yaml
mariobox
Suggests:
httr,
testthat
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
3 changes: 1 addition & 2 deletions inst/marioboxexample/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(get_health)
export(run_api)
importFrom(plumber,Plumber)
importFrom(yaml,read_yaml)
2 changes: 1 addition & 1 deletion inst/marioboxexample/R/get_health.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
get_health <- function(req, res) {
mariobox::mario_log(
method = "GET",
name = "health"
name = "health"
)
get_health_f()
}
Expand Down
8 changes: 6 additions & 2 deletions inst/marioboxexample/R/run_plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
#'
#' @export
run_api <- function() {
generate_api()$run()
}

generate_api <- function() {
mariobox::new_api(
yaml_file = system.file(
"mariobox.yml",
package = "marioboxexample"
)
)$run()
}
)
}
14 changes: 14 additions & 0 deletions inst/marioboxexample/man/get_health.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inst/marioboxexample/tests/testthat/test-health.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("health_f() works", {
expect_equal(
health_f(),
get_health_f(),
"ok"
)
})
4 changes: 2 additions & 2 deletions inst/marioboxexample/tests/testthat/test-run_plumber.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("new_api() works", {
api <- new_api()
test_that("generate_api() works", {
api <- generate_api()
expect_s3_class(
api,
c("Plumber", "Hookable", "R6")
Expand Down
11 changes: 7 additions & 4 deletions tests/testthat/test-create_pipework.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ is_properly_populated_mariobox <- function(path) {
"LICENSE.md",
"dev/run_dev.R",
"inst/mariobox.yml",
"man/get_health.Rd",
"man/run_api.Rd",
"NAMESPACE",
"R/fct_health.R",
"R/get_health.R",
"R/run_plumber.R",
"tests/testthat.R",
"tests/testthat/test-health.R",
Expand All @@ -25,6 +26,8 @@ is_properly_populated_mariobox <- function(path) {

actual_files <- list.files(path, recursive = TRUE)

# browser()
# waldo::compare(sort(expected_files), sort(actual_files))
identical(sort(expected_files), sort(actual_files))
}

Expand All @@ -46,12 +49,12 @@ path_pkg <- create_mariobox(
open = FALSE
)

usethis::with_project(dummy_mariobox_path, {
test_that("create_mariobox() works", {
test_that("create_mariobox() works", {
usethis::with_project(dummy_mariobox_path, {
usethis::use_mit_license(copyright_holder = "Babar")

check_output <- rcmdcheck::rcmdcheck(
# path = dummy_mariobox_path,
path = dummy_mariobox_path,
quiet = TRUE,
args = c("--no-manual")
)
Expand Down
60 changes: 44 additions & 16 deletions tests/testthat/test-manage_endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@ test_that("Managing endpoints", {
# Adding endpoint
mariobox_yaml_path <- "inst/mariobox.yml"
endpoint_name <- "michel"
r_file <- sprintf("R/fct_%s.R", endpoint_name)
test_file <- sprintf("tests/testthat/test-fct_%s.R", endpoint_name)
endpoint_fct_code_str <- sprintf("%s <- function(req){ return(\"ok\")}", endpoint_name)
r_file <- sprintf("R/get_%s.R", endpoint_name)
test_file <- sprintf("tests/testthat/test-get_%s.R", endpoint_name)
default_yaml <- list(
metadata = list(
title = "mariobox API"
),
handles = list(
health = list(
health_get = list(
methods = "GET",
path = "/health",
handler = "health"
handler = "get_health"
)
)
)
expected_yaml <- default_yaml
expected_yaml[["handles"]][[endpoint_name]] <- list(
expected_yaml[["handles"]][[paste0(endpoint_name, "_get")]] <- list(
methods = "GET",
path = paste0("/", endpoint_name),
handler = endpoint_name
handler = paste0("get_", endpoint_name)
)

add_endpoint(
Expand All @@ -38,16 +40,29 @@ test_that("Managing endpoints", {

expect_true(file.exists(r_file))
expect_true(file.exists(test_file))
r_file_text <- readLines(r_file)
# The end point function exists
expect_equal(
paste0(readLines(r_file), collapse = ""),
endpoint_fct_code_str
sum(
grepl("^get_michel <- function", r_file_text)
),
1
)
# The business logic function exists
expect_equal(
sum(
grepl("^get_michel_f <- function", r_file_text)
),
1
)
expect_equal(
paste0(readLines(test_file), collapse = ""),
"test_that(\"multiplication works\", { expect_equal(2 * 2, 4)})"
)

actual_yaml <- yaml::read_yaml(mariobox_yaml_path)
expect_equal(
yaml::read_yaml(mariobox_yaml_path),
actual_yaml,
expected_yaml
)

Expand All @@ -57,13 +72,24 @@ test_that("Managing endpoints", {
name = endpoint_name,
open = FALSE
),
regexp = sprintf("Endpoint '%s' already created", endpoint_name)
regexp = sprintf("Endpoint '%s' already exists", endpoint_name)
)
expect_true(file.exists(r_file))
expect_true(file.exists(test_file))
r_file_text <- readLines(r_file)
# The end point function exists
expect_equal(
paste0(readLines(r_file), collapse = ""),
endpoint_fct_code_str
sum(
grepl("^get_michel <- function", r_file_text)
),
1
)
# The business logic function exists
expect_equal(
sum(
grepl("^get_michel_f <- function", r_file_text)
),
1
)
expect_equal(
paste0(readLines(test_file), collapse = ""),
Expand All @@ -76,7 +102,8 @@ test_that("Managing endpoints", {

# Remove endpoint
remove_endpoint(
name = endpoint_name
name = endpoint_name,
method = "GET"
)
expect_false(file.exists(r_file))
expect_false(file.exists(test_file))
Expand All @@ -88,9 +115,10 @@ test_that("Managing endpoints", {
# Idempotence: Remove endpoint
expect_message(
remove_endpoint(
name = endpoint_name
name = endpoint_name,
method = "GET"
),
regexp = sprintf("There is no endpoint '%s' to delete", endpoint_name)
regexp = sprintf("There is no endpoint '%s' with method 'GET' to delete", endpoint_name)
)
expect_false(file.exists(r_file))
expect_false(file.exists(test_file))
Expand Down

0 comments on commit 4b30f8e

Please sign in to comment.