Skip to content

Commit

Permalink
Fix #167 (#168)
Browse files Browse the repository at this point in the history
* Fix #167

Right?

* Add tests

* Add a real test
  • Loading branch information
chainsawriot authored Nov 10, 2023
1 parent de5e7d2 commit b9ff6ef
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
22 changes: 19 additions & 3 deletions R/resolve.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
.query_rver <- function(snapshot_date) {
.query_rver <- function(snapshot_date, semver = FALSE) {
if (snapshot_date < attr(cached_rver, "newest_date")) {
allvers <- cached_rver
} else {
allvers <- .memo_rver()
}
allvers$date <- parsedate::parse_date(allvers$date)
utils::tail(allvers[allvers$date < snapshot_date,], 1)$version
if (!semver) {
return(utils::tail(allvers[allvers$date < snapshot_date,], 1)$version)
}
return(utils::tail(allvers[allvers$date < snapshot_date,], 1)$semver)
}

.query_biocver <- function(snapshot_date) {
Expand Down Expand Up @@ -41,10 +44,23 @@
})
}

.generate_pubdate <- function(search_res) {
## there are three different date columns: "date", "Date/Publication", and "crandb_file_date"
## 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(search_res$`Date/Publication`)
}
if (!is.null(search_res$date) && all(!is.na(search_res$date))) {
return(search_res$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 <- parsedate::parse_date(search_res$crandb_file_date)
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
Binary file modified R/sysdata.rda
Binary file not shown.
56 changes: 56 additions & 0 deletions tests/testthat/test_resolve.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,62 @@ 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")
## real test
data <- structure(list(date = c("2012-10-30T20:05:45+00:00", "2014-11-24T11:48:55+00:00",
"2001-05-15T09:00:31+00:00", "2016-10-20T20:59:35+00:00", "2016-10-28T23:11:52+00:00",
"2023-08-22T08:10:02+00:00"), crandb_file_date = c("2023-08-24 10:00:31",
"2023-08-24 10:00:31", "2023-08-24 10:00:31", "2023-08-24 10:00:32",
"2023-08-24 10:00:32", "2023-08-24 10:00:32"), `Date/Publication` = c("2012-10-30 21:05:45",
"2014-11-24 12:48:55", NA, "2016-10-20 21:59:35", "2016-10-29 00:11:52",
"2023-08-22 09:10:02 UTC")), class = c("tbl", "data.frame"), row.names = c("acepack.1.3-3.2",
"acepack.1.3-3.3", "acepack.1.3", "acepack.1.4.0", "acepack.1.4.1",
"acepack.1.4.2"))
expect_equal(.generate_pubdate(data), c("2012-10-30T20:05:45+00:00", "2014-11-24T11:48:55+00:00", "2001-05-15T09:00:31+00:00", "2016-10-20T20:59:35+00:00", "2016-10-28T23:11:52+00:00", "2023-08-22T08:10:02+00:00"))
})


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

test_that("normal", {
Expand Down

0 comments on commit b9ff6ef

Please sign in to comment.