Skip to content

Commit

Permalink
Fixed issue with CRAN test by adding helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
MMenchero committed Feb 2, 2024
1 parent 5812e42 commit 4625ea0
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
^README\.Rmd$
^codecov\.yml$
^cran-comments\.md$
^CRAN-SUBMISSION$
3 changes: 3 additions & 0 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version: 1.0.0
Date: 2024-02-01 06:50:20 UTC
SHA: 5812e42484b4d2586fe7122f9724f2e0fbcec698
13 changes: 6 additions & 7 deletions R/get_token.R
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
2 changes: 1 addition & 1 deletion R/nixtla_validate_token.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 9 additions & 3 deletions R/validate_exogenous.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}

Expand Down
4 changes: 2 additions & 2 deletions man/dot-get_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions man/dot-validate_exogenous.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/nixtla_validate_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

skip_if_no_token <- function() {
tryCatch({
nixtlar::.get_token()
}, error = function(e) {
testthat::skip("TIMEGPT_TOKEN is not set")
})
}
1 change: 1 addition & 0 deletions tests/testthat/test-timegpt-anomaly_detection.R
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-timegpt_cross_validation.R
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-timegpt_forecast.R
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-timegpt_historic.R
Original file line number Diff line number Diff line change
@@ -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")
Expand Down

0 comments on commit 4625ea0

Please sign in to comment.