From 4625ea0bddcaa629047085fd593a398834148f93 Mon Sep 17 00:00:00 2001 From: MMenchero Date: Thu, 1 Feb 2024 20:55:26 -0600 Subject: [PATCH] Fixed issue with CRAN test by adding helper. --- .Rbuildignore | 1 + CRAN-SUBMISSION | 3 +++ R/get_token.R | 13 ++++++------- R/nixtla_validate_token.R | 2 +- R/validate_exogenous.R | 12 +++++++++--- man/dot-get_token.Rd | 4 ++-- man/dot-validate_exogenous.Rd | 7 +++++++ man/nixtla_validate_token.Rd | 2 +- tests/testthat/helper.R | 8 ++++++++ tests/testthat/test-timegpt-anomaly_detection.R | 1 + tests/testthat/test-timegpt_cross_validation.R | 1 + tests/testthat/test-timegpt_forecast.R | 1 + tests/testthat/test-timegpt_historic.R | 1 + 13 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 CRAN-SUBMISSION create mode 100644 tests/testthat/helper.R diff --git a/.Rbuildignore b/.Rbuildignore index 29e6b01..445f929 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,3 +9,4 @@ ^README\.Rmd$ ^codecov\.yml$ ^cran-comments\.md$ +^CRAN-SUBMISSION$ diff --git a/CRAN-SUBMISSION b/CRAN-SUBMISSION new file mode 100644 index 0000000..bf4b46e --- /dev/null +++ b/CRAN-SUBMISSION @@ -0,0 +1,3 @@ +Version: 1.0.0 +Date: 2024-02-01 06:50:20 UTC +SHA: 5812e42484b4d2586fe7122f9724f2e0fbcec698 diff --git a/R/get_token.R b/R/get_token.R index d68b800..88518a8 100644 --- a/R/get_token.R +++ b/R/get_token.R @@ -1,26 +1,25 @@ #' Get TIMEGPT_TOKEN from options or from .Renviron #' This is a private function of nixtlar #' -#' @return If available, the TIMEGTP_TOKEN +#' @return If available, the TIMEGTP_TOKEN. Otherwise it returns an error and a message asking the user to set the token. #' @export #' @keywords internal #' @examples #' \dontrun{ -#' .get_token() # Assumes token was previously set successfully +#' .get_token() #' } #' .get_token <- function(){ # Get token from options - token <- getOption("TIMEGPT_TOKEN") - + token <- Sys.getenv("TIMEGPT_TOKEN", unset = NA) # If not available, get it from .Renviron - if(is.null(token)){ - token <- Sys.getenv("TIMEGPT_TOKEN") + if(is.na(token)) { + token <- getOption("TIMEGPT_TOKEN", default = NA) } # Return token or, if not available, stop - if(nzchar(token)){ + if(!is.na(token)){ return(token) }else{ stop("Please set TIMEGPT_TOKEN. Use nixtla_set_token() or set it as an environment variable in .Renviron") diff --git a/R/nixtla_validate_token.R b/R/nixtla_validate_token.R index d4bc6d1..ef0a184 100644 --- a/R/nixtla_validate_token.R +++ b/R/nixtla_validate_token.R @@ -1,6 +1,6 @@ #' Validate token #' -#' @return A message indicating whether the token is valid. +#' @return A status code and a message indicating whether the token is valid. #' @export #' #' @examples diff --git a/R/validate_exogenous.R b/R/validate_exogenous.R index 2afc8a1..46c1095 100644 --- a/R/validate_exogenous.R +++ b/R/validate_exogenous.R @@ -9,18 +9,24 @@ #' @export #' @keywords internal #' +#' @examples +#' \dontrun{ +#' Download df and X_df from https://docs.nixtla.io/docs/exogenous_variables and then run +#' .validate_exogenous(df, h=24, X_df) +#' } +#' .validate_exogenous <- function(df, h, X_df){ status <- list(validation = TRUE, - message = NULL - ) + message = NULL + ) # Check if df and X_df contain the same exogenous variables vals_df <- setdiff(names(df), c("unique_id", "ds", "y")) vals_X_df <- setdiff(names(X_df), c("unique_id", "ds")) if(!setequal(vals_df, vals_X_df)){ - status$valdiation <- FALSE + status$validation <- FALSE status$message <- "df and X_df must contain the same exogenous variables." } diff --git a/man/dot-get_token.Rd b/man/dot-get_token.Rd index 18f8ad2..e41f3cf 100644 --- a/man/dot-get_token.Rd +++ b/man/dot-get_token.Rd @@ -8,7 +8,7 @@ This is a private function of nixtlar} .get_token() } \value{ -If available, the TIMEGTP_TOKEN +If available, the TIMEGTP_TOKEN. Otherwise it returns an error and a message asking the user to set the token. } \description{ Get TIMEGPT_TOKEN from options or from .Renviron @@ -16,7 +16,7 @@ This is a private function of nixtlar } \examples{ \dontrun{ - .get_token() # Assumes token was previously set successfully + .get_token() } } diff --git a/man/dot-validate_exogenous.Rd b/man/dot-validate_exogenous.Rd index dd61fd4..8691abc 100644 --- a/man/dot-validate_exogenous.Rd +++ b/man/dot-validate_exogenous.Rd @@ -20,5 +20,12 @@ A list with the result of the validation (TRUE/FALSE) and an error message (if a \description{ Validate exogenous variables (if applicable) This is a private function of nixtlar +} +\examples{ +\dontrun{ +Download df and X_df from https://docs.nixtla.io/docs/exogenous_variables and then run +.validate_exogenous(df, h=24, X_df) +} + } \keyword{internal} diff --git a/man/nixtla_validate_token.Rd b/man/nixtla_validate_token.Rd index a39b13b..c2c5ae5 100644 --- a/man/nixtla_validate_token.Rd +++ b/man/nixtla_validate_token.Rd @@ -7,7 +7,7 @@ nixtla_validate_token() } \value{ -A message indicating whether the token is valid. +A status code and a message indicating whether the token is valid. } \description{ Validate token diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R new file mode 100644 index 0000000..171d83d --- /dev/null +++ b/tests/testthat/helper.R @@ -0,0 +1,8 @@ + +skip_if_no_token <- function() { + tryCatch({ + nixtlar::.get_token() + }, error = function(e) { + testthat::skip("TIMEGPT_TOKEN is not set") + }) +} diff --git a/tests/testthat/test-timegpt-anomaly_detection.R b/tests/testthat/test-timegpt-anomaly_detection.R index 2e37402..6b01454 100644 --- a/tests/testthat/test-timegpt-anomaly_detection.R +++ b/tests/testthat/test-timegpt-anomaly_detection.R @@ -1,5 +1,6 @@ with_mock_dir("../mocks", { test_that("timegpt_anomaly_detection", { + skip_if_no_token() test_data <- nixtlar::electricity response <- timegpt_anomaly_detection(test_data, id_col = "unique_id") expect_s3_class(response, "data.frame") diff --git a/tests/testthat/test-timegpt_cross_validation.R b/tests/testthat/test-timegpt_cross_validation.R index 251f901..f9e322b 100644 --- a/tests/testthat/test-timegpt_cross_validation.R +++ b/tests/testthat/test-timegpt_cross_validation.R @@ -1,5 +1,6 @@ with_mock_dir("../mocks", { test_that("timegpt_cross_validation", { + skip_if_no_token() test_data <- nixtlar::electricity response <- timegpt_cross_validation(test_data, h = 8, id_col = "unique_id", n_windows = 5) expect_s3_class(response, "data.frame") diff --git a/tests/testthat/test-timegpt_forecast.R b/tests/testthat/test-timegpt_forecast.R index ef34889..83898a4 100644 --- a/tests/testthat/test-timegpt_forecast.R +++ b/tests/testthat/test-timegpt_forecast.R @@ -1,5 +1,6 @@ with_mock_dir("../mocks", { test_that("timegpt_forecast", { + skip_if_no_token() test_data <- nixtlar::electricity response <- timegpt_forecast(test_data, h = 8, id_col = "unique_id", level = c(80,95)) expect_s3_class(response, "data.frame") diff --git a/tests/testthat/test-timegpt_historic.R b/tests/testthat/test-timegpt_historic.R index 4c46428..241ffeb 100644 --- a/tests/testthat/test-timegpt_historic.R +++ b/tests/testthat/test-timegpt_historic.R @@ -1,5 +1,6 @@ with_mock_dir("../mocks", { test_that("timegpt historic", { + skip_if_no_token() test_data <- nixtlar::electricity response <- timegpt_historic(test_data, id_col = "unique_id", level = c(80,95)) expect_s3_class(response, "data.frame")