Skip to content

Commit

Permalink
Added check for POSIXt objects to unbox. (jeroen#392)
Browse files Browse the repository at this point in the history
* Added check for POSIXt objects to `unbox`.

Added tests for `unbox` and Data objects.

* changed is to inherits

* Changed is to inherits
  • Loading branch information
ralmond committed Jun 19, 2022
1 parent 6773daa commit 8085435
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/unbox.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ unbox <- function(x){
stop("Tried to unbox dataframe with ", nrow(x), " rows.")
}
}
if (length(x) == 1L && inherits(x,"POSIXt")) {
return (as.scalar(x))
}
if(is.null(x) || !is.atomic(x) || length(dim(x)) > 1){
stop("Only atomic vectors of length 1 or data frames with 1 row can be unboxed.")
}
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-fromJSON-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ test_that("fromJSON date objects", {
expect_that(fromJSON(toJSON(mydf, POSIXt="mongo", na="null"))$x, is_a("POSIXct"))
expect_that(fromJSON(toJSON(mydf, POSIXt="mongo"))$x, equals(x))

xct <- as.POSIXct(x)
xlt <- as.POSIXlt(x)

expect_equal(xct, xlt)
expect_true(unbox(xct[1]) == unbox(xlt[1]))
xct3un <- unbox(xct[3])
expect_true(is.na(xct3un) && inherits(xct3un,"scalar") &&
inherits(xct3un,"POSIXt"))
xlt3un <- unbox(xlt[3])
expect_true(is.na(xlt3un) && inherits(xlt3un,"scalar") &&
inherits(xlt3un,"POSIXt"))

expect_equal(toJSON(xct,POSIXt="mongo"),toJSON(xlt,POSIXt="mongo"))
expect_equal(toJSON(unbox(xct[1]),POSIXt="mongo"),
toJSON(unbox(xlt[1]),POSIXt="mongo"))

});

0 comments on commit 8085435

Please sign in to comment.