Skip to content

Commit

Permalink
finished tests for mongo.R
Browse files Browse the repository at this point in the history
  • Loading branch information
brycefrank committed Sep 30, 2024
1 parent ab48f3d commit 0d254c2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
8 changes: 5 additions & 3 deletions R/mongo.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,23 @@ delete_pub <- function(pub_con, model_con, pub_id, verbose = TRUE) {
pub_con$remove(pub_find)
}

#' Get a vector of pub_ids that do not exist in the set of currently maintained
#' pub ids
get_nonexistent_pubs <- function(pub_con, verbose = TRUE) {
pub_find <- list(
"_id" = list(
"$nin" = get_current_pub_ids()
)
) |> jsonlite::toJSON()

nonexistent_pubs <- pub_con$find(pub_find)
nonexistent_pubs <- pub_con$find(pub_find, fields = '{}')

if(verbose) {
print(paste("Found", nrow(nonexistent_pubs), "nonexistent publications"))
}

if(nrow(nonexistent_pubs) > 0) {
return(nonexistent_pubs$pub_id)
return(nonexistent_pubs$`_id`)
} else {
return(NULL)
}
Expand Down Expand Up @@ -110,7 +112,7 @@ write_update_commit <- function(update_con) {
#'
#' This is the main function for updating the database. It proceeds in five
#' phases, meant to remove old/"hangnail" publications and models.
#' 1. Delete all nonexistent publications from db (i.e., in publications
#' 1. Delete all nonexistent publications from db (i.e., publications
#' no longer in directory) and their models
#' 2. Determine which publications have been modified since last commit
#' 3. Generate JSON for modified publications+models, retain newly generated
Expand Down
88 changes: 87 additions & 1 deletion tests/testthat/test-mongo.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
test_pub_path <- system.file("testdata/test_publications/a_e/barrett_1978.R", package = "models")
source(test_pub_path)
test_pub <- barrett_1978
pub_parsed <- publication_to_json(test_pub)

is_github_action <- function() {
return(tolower(Sys.getenv("GITHUB_ACTIONS")) == "true")
Expand Down Expand Up @@ -36,9 +37,22 @@ with_mocked_database <- function(code, ...) {
force(code(pub_conn, model_conn, ...))
}

with_mocked_update_conn <- function(code) {
update_conn <- mongolite::mongo(
collection = "update",
db = "test",
url = Sys.getenv("MONGODB_URL_DEV")
)

on.exit({
update_conn$drop()
})

force(code(update_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,
Expand All @@ -56,3 +70,75 @@ test_that("upsert_publication inserts a document when it does not exist", {
expect_equal(models_res$`_id`, "cc2078aa")
})
})

test_that("upsert_publication updates a document when it does exist", {
with_mocked_database(function(pub_conn, model_conn) {
# insert
upsert_publication(
pub_conn, model_conn, pub_parsed$pub_json, pub_parsed$models_json,
verbose = FALSE
)

pub_parsed$pub_json$citation$bibtype <- jsonlite::unbox("Article")

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

pub_res <- pub_conn$find('{}')

expect_equal(pub_res$citation$bibtype, "Article")
})
})

test_that("delete_pubs deletes the publication and its member models", {
with_mocked_database(function(pub_conn, model_conn) {
upsert_publication(
pub_conn, model_conn, pub_parsed$pub_json, pub_parsed$models_json,
verbose = FALSE
)

delete_pubs(pub_conn, model_conn, "barrett_1978", verbose = FALSE)

pub_res <- pub_conn$find('{}')
model_res <- model_conn$find('{}')

expect_equal(nrow(pub_res), 0)
expect_equal(nrow(model_res), 0)
})
})

test_that("get_nonexistent_pubs returns correct set of pub_ids", {
with_mocked_database(function(pub_conn, model_conn) {
nonexist_ids_first <- get_nonexistent_pubs(pub_conn)
expect_true(is.null(nonexist_ids_first))

pub_conn$insert('{"_id": "nonexist_1987"}')
nonexist_ids <- get_nonexistent_pubs(pub_conn)

expect_equal(nonexist_ids, "nonexist_1987")
})
})

test_that("get_last_update_commit gets most recent commit", {
with_mocked_update_conn(function(update_conn) {
update_conn$insert('{"datetime": 1727726535, "commit_id": "bbbbbbb"}')
update_conn$insert('{"datetime": 1727726735, "commit_id": "aaaaaaa"}')

last_commit <- get_last_update_commit(update_conn)

expect_equal(last_commit, "aaaaaaa")
})
})

test_that("write_update_commit writes an update", {
with_mocked_update_conn(function(update_conn) {
write_update_commit(update_conn)

update_res <- update_conn$find('{}')

expect_equal(nrow(update_res), 1)
})
})

0 comments on commit 0d254c2

Please sign in to comment.