From a21b9bd246ca8e663a56563bc2e65a48cf1476e1 Mon Sep 17 00:00:00 2001 From: pvictor Date: Fri, 15 Feb 2019 23:06:11 +0100 Subject: [PATCH] start testing --- DESCRIPTION | 2 ++ R/parse-data.R | 4 +-- tests/testthat.R | 4 +++ tests/testthat/test-parse_df.R | 55 ++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-parse_df.R diff --git a/DESCRIPTION b/DESCRIPTION index 26bcb2f..a61fb3c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,3 +19,5 @@ Imports: RoxygenNote: 6.1.1 URL: https://github.com/dreamRs/apexcharter BugReports: https://github.com/dreamRs/apexcharter/issues +Suggests: + testthat diff --git a/R/parse-data.R b/R/parse-data.R index c8106be..dcf1616 100644 --- a/R/parse-data.R +++ b/R/parse-data.R @@ -18,8 +18,8 @@ parse_df <- function(data, add_names = FALSE) { FUN = function(x) { if (inherits(x, "Date") & identical(add_names, FALSE)) { as.numeric(x) * 86400000 - } else if (inherits(x, "POSIXt") & identical(add_names, FALSE)) { - as.numeric(x) + } else if (inherits(x, "POSIXt")) { + as.numeric(x) * 1000 } else if (inherits(x, "factor")) { as.character(x) } else { diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..a84f82d --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(apexcharter) + +test_check("apexcharter") diff --git a/tests/testthat/test-parse_df.R b/tests/testthat/test-parse_df.R new file mode 100644 index 0000000..7796286 --- /dev/null +++ b/tests/testthat/test-parse_df.R @@ -0,0 +1,55 @@ +context("test-parse_df") + +test_that("parse_df works", { + + x <- head(iris) + res <- parse_df(x) + + expect_is(res, "list") + expect_length(res, nrow(x)) + expect_length(res[[1]], ncol(x)) + expect_named(res[[1]], NULL) +}) + +test_that("parse_df works with names", { + + x <- head(iris) + res <- parse_df(x, add_names = TRUE) + + expect_is(res, "list") + expect_length(res, nrow(x)) + expect_length(res[[1]], ncol(x)) + expect_named(res[[1]], names(x)) +}) + + +test_that("parse_df works with custom names", { + + x <- head(iris) + custom <- LETTERS[1:5] + res <- parse_df(x, add_names = custom) + + expect_is(res, "list") + expect_length(res, nrow(x)) + expect_length(res[[1]], ncol(x)) + expect_named(res[[1]], custom) +}) + + +test_that("parse_df works with Date/POSIXt", { + + x <- data.frame( + date = Sys.Date() + 1:5, + datetime = Sys.time() + 1:5 + ) + res <- parse_df(x, add_names = TRUE) + + expect_is(res[[1]]$date, "Date") + expect_is(res[[1]]$datetime, "numeric") + + res <- parse_df(x, add_names = FALSE) + expect_is(res[[1]][[1]], "numeric") + expect_is(res[[1]][[2]], "numeric") +}) + +