Skip to content

Commit

Permalink
starting in on mongo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brycefrank committed Sep 29, 2024
1 parent cc6b056 commit 5d83744
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/json.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ covariate_definitions_to_json <- function(covt_def_data) {
#' @return A list containing a parsable form of the model object
model_to_json <- function(model) {
proxy_id <- get_model_hash(
model@predict_fn_populated, model@descriptors
model@predict_fn_populated, allometric:::descriptors(model)
)

model_id <- substr(proxy_id, 1, 8)
model_descriptors <- model@descriptors
model_descriptors <- allometric:::descriptors(model)
model_class <- as.character(class(model))

response_definition <- ifelse(
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-json.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,6 @@ test_that("publication_to_json_runs", {
names(parsed) == c("pub_json", "models_json")

expect_equal(parsed$models_json[[1]][["model_type"]], jsonlite::unbox("site index"))

expect_true("taxa" %in% names(parsed$models_json[[1]][["descriptors"]]))
})
58 changes: 58 additions & 0 deletions tests/testthat/test-mongo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Create the test publication
test_pub_path <- system.file("testdata/test_publications/a_e/barrett_1978.R", package = "models")
source(test_pub_path)
test_pub <- barrett_1978

is_github_action <- function() {
return(tolower(Sys.getenv("GITHUB_ACTIONS")) == "true")
}



with_mocked_database <- function(code, ...) {
if(!is_github_action()) {
# Populates .env into system variables
dot_env_path <- system.file(".env", package = "models")
#dotenv::load_dot_env(dot_env_path)
}

pub_conn <- mongolite::mongo(
collection = "publications",
db = "test",
url = Sys.getenv("MONGODB_URL_DEV")
)

model_conn <- mongolite::mongo(
collection = "models",
db = "test",
url = Sys.getenv("MONGODB_URL_DEV")
)

on.exit({
model_conn$drop()
pub_conn$drop()
}, add = TRUE)

force(code(pub_conn, model_conn, ...))
}

test_that("upsert_publication inserts a document when it does not exist", {
with_mocked_database(function(pub_conn, model_conn) {
pub_parsed <- publication_to_json(test_pub)

upsert_publication(
pub_conn, model_conn, pub_parsed$pub_json, pub_parsed$models_json,
verbose = FALSE
)

pub_res <- pub_conn$find('{}')
models_res <- model_conn$find('{}', fields = '{}')

expect_true(nrow(pub_res) == 1)
expect_equal(pub_res$models[[1]], "cc2078aa")
expect_equal(pub_res$citation$pub_id, "barrett_1978")

expect_true(nrow(models_res) == 1)
expect_equal(models_res$`_id`, "cc2078aa")
})
})

0 comments on commit 5d83744

Please sign in to comment.