Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chainsawriot committed Nov 8, 2023
1 parent 93e7cf4 commit 30c436a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/resolve.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@
## But they have different problems: "date" and "Date/Publication" are not always available and complete, and they are
## the most accurate; "crandb_file_date" is always available, but not accurate
if (!is.null(search_res$`Date/Publication`) && all(!is.na(search_res$`Date/Publication`))) {
return(parsedate::parse_date(search_res$`Date/Publication`))
return(search_res$`Date/Publication`)
}
if (!is.null(search_res$date) && all(!is.na(search_res$date))) {
return(parsedate::parse_date(search_res$date))
return(search_res$date)
}
return(parsedate::parse_date(search_res$crandb_file_date))
return(search_res$crandb_file_date)
}

.query_snapshot_dependencies_cran <- function(handle = "rtoot", snapshot_date = "2022-12-10", bioc_version = NULL) {
snapshot_date <- parsedate::parse_date(snapshot_date)
search_res <- .memo_search(handle)
search_res$pubdate <- .generate_pubdate(search_res)
search_res$pubdate <- parsedate::parse_date(.generate_pubdate(search_res))
snapshot_versions <- search_res[search_res$pubdate <= snapshot_date,]
if (nrow(snapshot_versions) == 0) {
stop("No snapshot version exists for ", handle, ".", call. = FALSE)
Expand Down
44 changes: 44 additions & 0 deletions tests/testthat/test_resolve.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,50 @@ test_that(".check_local_in_pkgrefs", {
expect_error(suppressWarnings(.check_local_in_pkgrefs(c("local::../testdata/issue39.RDS", "cran::rtoot"))))
})

test_that(".query_rver (and semver argument)", {
expect_equal(.query_rver(parsedate::parse_date("2000-02-29 10:00:00")), "1.0")
expect_equal(.query_rver(parsedate::parse_date("2000-02-29 10:00:00"), semver = TRUE), "1.0.0")
})

test_that(".generate_pubdate", {
a <- c("a", "b")
b <- c("c", "d")
c <- c("e", "f")
expect_equal(.generate_pubdate(data.frame(date = a)), a)
expect_equal(.generate_pubdate(data.frame(date = a, crandb_file_date = b)), a)
expect_equal(.generate_pubdate(data.frame(crandb_file_date = b)), b)
x <- data.frame(`Date/Publication` = a, crandb_file_date = b)
colnames(x)[1] <- "Date/Publication"
expect_equal(.generate_pubdate(x), a)
## "Date/Publication has a higher priority"
x <- data.frame(`Date/Publication` = a, date = b)
colnames(x)[1] <- "Date/Publication"
expect_equal(.generate_pubdate(x), a)
## NA attack
x <- data.frame(`Date/Publication` = a, date = b)
colnames(x)[1] <- "Date/Publication"
x[1, 1] <- NA
expect_equal(.generate_pubdate(x), b)
x <- data.frame(`Date/Publication` = a, date = b, crandb_file_date = c)
colnames(x)[1] <- "Date/Publication"
x[1, 1] <- NA
x[2, 2] <- NA
expect_equal(.generate_pubdate(x), c)
## single
expect_equal(.generate_pubdate(data.frame(date = "a")), "a")
expect_equal(.generate_pubdate(data.frame(date = "a", crandb_file_date = "b")), "a")
expect_equal(.generate_pubdate(data.frame(crandb_file_date = "a")), "a")
x <- data.frame(`Date/Publication` = NA, date = NA, crandb_file_date = "c")
colnames(x)[1] <- "Date/Publication"
expect_equal(.generate_pubdate(x), "c")
x <- data.frame(`Date/Publication` = "a", date = NA, crandb_file_date = "c")
colnames(x)[1] <- "Date/Publication"
expect_equal(.generate_pubdate(x), "a")
x <- data.frame(`Date/Publication` = NA, date = "b", crandb_file_date = "c")
colnames(x)[1] <- "Date/Publication"
expect_equal(.generate_pubdate(x), "b")
})

## The following are real tests. Even with memoisation, please keep at minimum

test_that("normal", {
Expand Down

0 comments on commit 30c436a

Please sign in to comment.