Permalink
Browse files

Made round tests more specific with expect_identical, reorganized tes…

…ts previously grouped in test-utilities
  • Loading branch information...
1 parent 044a7a3 commit 91bd4ee9695791989fcb717995ffa2e6fd0c496d @garrettgman garrettgman committed Oct 4, 2012
View
@@ -0,0 +1,32 @@
+context("am/pm")
+
+test_that("am and pm correctly identify time of day",{
+ x <- as.POSIXct("2009-08-03 13:01:59", tz = "UTC")
+ y <- as.POSIXct("2008-08-03 10:01:59", tz = "UTC")
+
+ expect_that(am(x), is_false())
+ expect_that(am(y), is_true())
+
+ expect_that(pm(x), is_true())
+ expect_that(pm(y), is_false())
+})
+
+test_that("am and pm handle vectors",{
+ x <- as.POSIXct(c("2009-08-03 13:01:59", "2008-08-03 10:01:59"), tz = "UTC")
+ y <- as.POSIXct(c("2009-08-03 10:01:59", "2008-08-03 13:01:59"), tz = "UTC")
+
+ expect_that(pm(x)[1], is_true())
+ expect_that(pm(x)[2], is_false())
+ expect_that(am(y)[1], is_true())
+ expect_that(am(y)[2], is_false())
+})
+
+test_that("am and pm handle various classes of date-time object",{
+ x <- as.POSIXct(c("2008-08-03 13:01:59", "2009-08-03 10:01:59"), tz = "UTC")
+
+ expect_that(pm(x)[1], is_true())
+ expect_that(pm(x)[2], is_false())
+ expect_that(pm(as.Date(x))[1], is_false())
+ expect_that(pm(as.POSIXlt(x))[1], is_true())
+ expect_that(pm(as.POSIXlt(x))[2], is_false())
+})
@@ -0,0 +1,14 @@
+context("Decimal_date")
+
+test_that("decimal_date works as expected",{
+ x <- as.POSIXct("2008-08-03 10:01:59", tz = "America/New_York")
+
+ expect_that(decimal_date(x), equals(2008.58846))
+})
+
+test_that("decimal_date works handles vectors",{
+ x <- as.POSIXct(c("2008-08-03 13:01:59", "2009-08-03 10:01:59"), tz = "UTC")
+
+ expect_that(round(decimal_date(x), 3), equals(c(2008.589, 2009.587)))
+
+})
@@ -18,3 +18,12 @@ test_that("is.instant/is.timepoint handle vectors",{
expect_that(is.instant(as.POSIXct(c("2008-08-03 13:01:59",
"2008-08-03 13:01:59"), tz = "UTC")), is_true())
})
+
+test_that("now() handles time zone input correctly",{
+ expect_identical(floor_date(now("UTC"), "minute"), floor_date(as.POSIXct(format(
+ as.POSIXct(Sys.time()), tz = "UTC"), tz = "UTC"), "minute"))
+})
+
+test_that("today() works correctly",{
+ expect_identical(today(), Sys.Date())
+})
View
@@ -4,178 +4,186 @@ test_that("floor_date works for each time element",{
x <- as.POSIXct("2009-08-03 12:01:59.23", tz =
"UTC")
- expect_that(floor_date(x, "second"), equals(as.POSIXct(
- "2009-08-03 12:01:59", tz = "UTC")))
- expect_that(floor_date(x, "minute"), equals(as.POSIXct(
- "2009-08-03 12:01:00", tz = "UTC")))
- expect_that(floor_date(x, "hour"), equals(as.POSIXct(
- "2009-08-03 12:00:00", tz = "UTC")))
- expect_that(floor_date(x, "day"), equals(as.POSIXct(
- "2009-08-03 00:00:00", tz = "UTC")))
- expect_that(floor_date(x, "week"), equals(as.POSIXct(
- "2009-08-02 00:00:00", tz = "UTC")))
- expect_that(floor_date(x, "month"), equals(as.POSIXct(
- "2009-08-01 00:00:00", tz = "UTC")))
- expect_that(floor_date(x, "year"), equals(as.POSIXct(
- "2009-01-01 00:00:00", tz = "UTC")))
+ expect_identical(floor_date(x, "second"), as.POSIXct(
+ "2009-08-03 12:01:59", tz = "UTC"))
+ expect_identical(floor_date(x, "minute"), as.POSIXct(
+ "2009-08-03 12:01:00", tz = "UTC"))
+ expect_identical(floor_date(x, "hour"), as.POSIXct(
+ "2009-08-03 12:00:00", tz = "UTC"))
+ expect_identical(floor_date(x, "day"), as.POSIXct(
+ "2009-08-03 00:00:00", tz = "UTC"))
+ expect_identical(floor_date(x, "week"), as.POSIXct(
+ "2009-08-02 00:00:00", tz = "UTC"))
+ expect_identical(floor_date(x, "month"), as.POSIXct(
+ "2009-08-01 00:00:00", tz = "UTC"))
+ expect_identical(floor_date(x, "year"), as.POSIXct(
+ "2009-01-01 00:00:00", tz = "UTC"))
})
test_that("ceiling_date works for each time element",{
x <- as.POSIXct("2009-08-03 12:01:59.23", tz = "UTC")
- expect_that(ceiling_date(x, "second"), equals(as.POSIXct(
- "2009-08-03 12:02:00", tz = "UTC")))
- expect_that(ceiling_date(x, "minute"), equals(as.POSIXct(
- "2009-08-03 12:02:00", tz = "UTC")))
- expect_that(ceiling_date(x, "hour"), equals(as.POSIXct(
- "2009-08-03 13:00:00", tz = "UTC")))
- expect_that(ceiling_date(x, "day"), equals(as.POSIXct(
- "2009-08-04 00:00:00", tz = "UTC")))
- expect_that(ceiling_date(x, "week"), equals(as.POSIXct(
- "2009-08-09 00:00:00", tz = "UTC")))
- expect_that(ceiling_date(x, "month"), equals(as.POSIXct(
- "2009-09-01 00:00:00", tz = "UTC")))
- expect_that(ceiling_date(x, "year"), equals(as.POSIXct(
- "2010-01-01 00:00:00", tz = "UTC")))
+ expect_identical(ceiling_date(x, "second"), as.POSIXct(
+ "2009-08-03 12:02:00", tz = "UTC"))
+ expect_identical(ceiling_date(x, "minute"), as.POSIXct(
+ "2009-08-03 12:02:00", tz = "UTC"))
+ expect_identical(ceiling_date(x, "hour"), as.POSIXct(
+ "2009-08-03 13:00:00", tz = "UTC"))
+ expect_identical(ceiling_date(x, "day"), as.POSIXct(
+ "2009-08-04 00:00:00", tz = "UTC"))
+ expect_identical(ceiling_date(x, "week"), as.POSIXct(
+ "2009-08-09 00:00:00", tz = "UTC"))
+ expect_identical(ceiling_date(x, "month"), as.POSIXct(
+ "2009-09-01 00:00:00", tz = "UTC"))
+ expect_identical(ceiling_date(x, "year"), as.POSIXct(
+ "2010-01-01 00:00:00", tz = "UTC"))
})
test_that("round_date works for each time element",{
x <- as.POSIXct("2009-08-03 12:01:59.23", tz =
"UTC")
- expect_that(round_date(x, "second"), equals(as.POSIXct(
- "2009-08-03 12:01:59", tz = "UTC")))
- expect_that(round_date(x, "minute"), equals(as.POSIXct(
- "2009-08-03 12:02:00", tz = "UTC")))
- expect_that(round_date(x, "hour"), equals(as.POSIXct(
- "2009-08-03 12:00:00", tz = "UTC")))
- expect_that(round_date(x, "day"), equals(as.POSIXct(
- "2009-08-04 00:00:00", tz = "UTC")))
- expect_that(round_date(x, "week"), equals(as.POSIXct(
- "2009-08-02 00:00:00", tz = "UTC")))
- expect_that(round_date(x, "month"), equals(as.POSIXct(
- "2009-08-01 00:00:00", tz = "UTC")))
- expect_that(round_date(x, "year"), equals(as.POSIXct(
- "2010-01-01 00:00:00", tz = "UTC")))
+ expect_identical(round_date(x, "second"), as.POSIXct(
+ "2009-08-03 12:01:59", tz = "UTC"))
+ expect_identical(round_date(x, "minute"), as.POSIXct(
+ "2009-08-03 12:02:00", tz = "UTC"))
+ expect_identical(round_date(x, "hour"), as.POSIXct(
+ "2009-08-03 12:00:00", tz = "UTC"))
+ expect_identical(round_date(x, "day"), as.POSIXct(
+ "2009-08-04 00:00:00", tz = "UTC"))
+ expect_identical(round_date(x, "week"), as.POSIXct(
+ "2009-08-02 00:00:00", tz = "UTC"))
+ expect_identical(round_date(x, "month"), as.POSIXct(
+ "2009-08-01 00:00:00", tz = "UTC"))
+ expect_identical(round_date(x, "year"), as.POSIXct(
+ "2010-01-01 00:00:00", tz = "UTC"))
})
test_that("floor_date handles vectors",{
x <- as.POSIXct(c("2009-08-03 12:01:59.23",
"2010-08-03 12:01:59.23"), tz = "UTC")
- expect_that(floor_date(x, "second"), equals(as.POSIXct(c(
+ expect_identical(floor_date(x, "second"), as.POSIXct(c(
"2009-08-03 12:01:59", "2010-08-03 12:01:59"), tz =
- "UTC")))
- expect_that(floor_date(x, "minute"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(floor_date(x, "minute"), as.POSIXct(c(
"2009-08-03 12:01:00", "2010-08-03 12:01:00"), tz =
- "UTC")))
- expect_that(floor_date(x, "hour"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(floor_date(x, "hour"), as.POSIXct(c(
"2009-08-03 12:00:00", "2010-08-03 12:00:00"), tz =
- "UTC")))
- expect_that(floor_date(x, "day"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(floor_date(x, "day"), as.POSIXct(c(
"2009-08-03 00:00:00", "2010-08-03 00:00:00"), tz =
- "UTC")))
- expect_that(floor_date(x, "week"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(floor_date(x, "week"), as.POSIXct(c(
"2009-08-02 00:00:00", "2010-08-01 00:00:00"), tz =
- "UTC")))
- expect_that(floor_date(x, "month"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(floor_date(x, "month"), as.POSIXct(c(
"2009-08-01 00:00:00", "2010-08-01 00:00:00"), tz =
- "UTC")))
- expect_that(floor_date(x, "year"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(floor_date(x, "year"), as.POSIXct(c(
"2009-01-01 00:00:00", "2010-01-01 00:00:00"), tz =
- "UTC")))
+ "UTC"))
})
test_that("ceiling_date handles vectors",{
x <- as.POSIXct(c("2009-08-03 12:01:59.23",
"2010-08-03 12:01:59.23"), tz = "UTC")
- expect_that(ceiling_date(x, "second"), equals(as.POSIXct(c(
+ expect_identical(ceiling_date(x, "second"), as.POSIXct(c(
"2009-08-03 12:02:00", "2010-08-03 12:02:00"), tz =
- "UTC")))
- expect_that(ceiling_date(x, "minute"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(ceiling_date(x, "minute"), as.POSIXct(c(
"2009-08-03 12:02:00", "2010-08-03 12:02:00"), tz =
- "UTC")))
- expect_that(ceiling_date(x, "hour"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(ceiling_date(x, "hour"), as.POSIXct(c(
"2009-08-03 13:00:00", "2010-08-03 13:00:00"), tz =
- "UTC")))
- expect_that(ceiling_date(x, "day"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(ceiling_date(x, "day"), as.POSIXct(c(
"2009-08-04 00:00:00", "2010-08-04 00:00:00"), tz =
- "UTC")))
- expect_that(ceiling_date(x, "week"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(ceiling_date(x, "week"), as.POSIXct(c(
"2009-08-09 00:00:00", "2010-08-08 00:00:00"), tz =
- "UTC")))
- expect_that(ceiling_date(x, "month"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(ceiling_date(x, "month"), as.POSIXct(c(
"2009-09-01 00:00:00", "2010-09-01 00:00:00"), tz =
- "UTC")))
- expect_that(ceiling_date(x, "year"), equals(as.POSIXct(c(
+ "UTC"))
+ expect_identical(ceiling_date(x, "year"), as.POSIXct(c(
"2010-01-01 00:00:00", "2011-01-01 00:00:00"), tz =
- "UTC")))
+ "UTC"))
})
test_that("round_date handles vectors",{
x <- as.POSIXct(c("2009-08-03 12:01:59.23",
"2010-08-03 12:01:59.23"), tz = "UTC")
- expect_that(round_date(x, "second"),
- equals(as.POSIXct(c("2009-08-03 12:01:59",
- "2010-08-03 12:01:59"), tz = "UTC")))
- expect_that(round_date(x, "minute"),
- equals(as.POSIXct(c("2009-08-03 12:02:00",
- "2010-08-03 12:02:00"), tz = "UTC")))
- expect_that(round_date(x, "hour"),
- equals(as.POSIXct(c("2009-08-03 12:00:00",
- "2010-08-03 12:00:00"), tz = "UTC")))
- expect_that(round_date(x, "day"),
- equals(as.POSIXct(c("2009-08-04 00:00:00",
- "2010-08-04 00:00:00"), tz = "UTC")))
- expect_that(round_date(x, "week"),
- equals(as.POSIXct(c("2009-08-02 00:00:00",
- "2010-08-01 00:00:00"), tz = "UTC")))
- expect_that(round_date(x, "month"),
- equals(as.POSIXct(c("2009-08-01 00:00:00",
- "2010-08-01 00:00:00"), tz = "UTC")))
- expect_that(round_date(x, "year"),
- equals(as.POSIXct(c("2010-01-01 00:00:00",
- "2011-01-01 00:00:00"), tz = "UTC")))
+ expect_identical(round_date(x, "second"),
+ as.POSIXct(c("2009-08-03 12:01:59",
+ "2010-08-03 12:01:59"), tz = "UTC"))
+ expect_identical(round_date(x, "minute"),
+ as.POSIXct(c("2009-08-03 12:02:00",
+ "2010-08-03 12:02:00"), tz = "UTC"))
+ expect_identical(round_date(x, "hour"),
+ as.POSIXct(c("2009-08-03 12:00:00",
+ "2010-08-03 12:00:00"), tz = "UTC"))
+ expect_identical(round_date(x, "day"),
+ as.POSIXct(c("2009-08-04 00:00:00",
+ "2010-08-04 00:00:00"), tz = "UTC"))
+ expect_identical(round_date(x, "week"),
+ as.POSIXct(c("2009-08-02 00:00:00",
+ "2010-08-01 00:00:00"), tz = "UTC"))
+ expect_identical(round_date(x, "month"),
+ as.POSIXct(c("2009-08-01 00:00:00",
+ "2010-08-01 00:00:00"), tz = "UTC"))
+ expect_identical(round_date(x, "year"),
+ as.POSIXct(c("2010-01-01 00:00:00",
+ "2011-01-01 00:00:00"), tz = "UTC"))
})
test_that("floor_date works for a variety of formats",{
x <- as.POSIXct("2009-08-03 12:01:59", tz = "UTC")
- expect_that(floor_date(x, "minute"), equals(
- as.POSIXct("2009-08-03 12:01:00", tz = "UTC")))
- expect_that(floor_date(as.Date(x), "month"), equals(
- as.Date("2009-08-01"), tz = "UTC"))
- expect_that(floor_date(as.POSIXlt(x), "minute"), equals(
- as.POSIXlt(as.POSIXct("2009-08-03 12:01:00", tz = "UTC"))))
+ expect_identical(floor_date(x, "minute"),
+ as.POSIXct("2009-08-03 12:01:00", tz = "UTC"))
+ expect_identical(floor_date(as.Date(x), "month"),
+ as.Date("2009-08-01"))
+ expect_identical(floor_date(as.POSIXlt(x), "minute"),
+ as.POSIXlt(as.POSIXct("2009-08-03 12:01:00", tz = "UTC")))
})
test_that("ceiling_date works for a variety of formats",{
x <- as.POSIXct("2009-08-03 12:01:59", tz = "UTC")
- expect_that(ceiling_date(x, "minute"), equals(
- as.POSIXct("2009-08-03 12:02:00", tz = "UTC")))
- expect_that(ceiling_date(as.Date(x), "month"), equals(
- as.Date("2009-09-01")))
- expect_that(ceiling_date(as.POSIXlt(x), "minute"), equals(
+ expect_identical(ceiling_date(x, "minute"),
+ as.POSIXct("2009-08-03 12:02:00", tz = "UTC"))
+ expect_identical(ceiling_date(as.Date(x), "month"),
+ as.Date("2009-09-01"))
+ expect_identical(ceiling_date(as.POSIXlt(x), "minute"),
as.POSIXlt(as.POSIXct("2009-08-03 12:02:00", tz =
- "UTC"))))
+ "UTC")))
})
test_that("round_date works for a variety of formats",{
x <- as.POSIXct("2009-08-03 12:01:59", tz = "UTC")
- expect_that(round_date(x, "minute"), equals(
- as.POSIXct("2009-08-03 12:02:00", tz = "UTC")))
- expect_that(round_date(as.Date(x), "month"), equals(
- as.Date("2009-08-01")))
- expect_that(round_date(as.POSIXlt(x), "minute"), equals(
+ expect_identical(round_date(x, "minute"),
+ as.POSIXct("2009-08-03 12:02:00", tz = "UTC"))
+ expect_identical(round_date(as.Date(x), "month"),
+ as.Date("2009-08-01"))
+ expect_identical(round_date(as.POSIXlt(x), "minute"),
as.POSIXlt(as.POSIXct("2009-08-03 12:02:00", tz =
- "UTC"))))
+ "UTC")))
})
test_that("ceiling_date does not round up dates that are already on a boundary",{
expect_equal(ceiling_date(as.Date("2012-09-27"), 'day'), as.Date("2012-09-27"))
+})
+
+test_that("floor_date does not round down dates that are already on a boundary",{
+ expect_equal(floor_date(as.Date("2012-09-27"), 'day'), as.Date("2012-09-27"))
+})
+
+test_that("round_date does not round dates that are already on a boundary",{
+ expect_equal(round_date(as.Date("2012-09-27"), 'day'), as.Date("2012-09-27"))
})
Oops, something went wrong.

0 comments on commit 91bd4ee

Please sign in to comment.