diff --git a/R/mongo.R b/R/mongo.R index 2aff9fe..b3b7659 100644 --- a/R/mongo.R +++ b/R/mongo.R @@ -57,6 +57,8 @@ 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( @@ -64,14 +66,14 @@ get_nonexistent_pubs <- function(pub_con, verbose = TRUE) { ) ) |> 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) } @@ -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 diff --git a/tests/testthat/test-mongo.R b/tests/testthat/test-mongo.R index 811ad34..2a2f600 100644 --- a/tests/testthat/test-mongo.R +++ b/tests/testthat/test-mongo.R @@ -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") @@ -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, @@ -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) + }) +}) \ No newline at end of file