tests/testthat/test-read_adl_functions.R

library(testthat)
library(dplyr)
library(readr)

skip_if_no_auth <- function() {
  if (identical(Sys.getenv("client_id"), "") || identical(Sys.getenv("client_secret"), "") || identical(Sys.getenv("tenant"), "")) {
    skip("No authentication available")
  }
}

context("ADL Read Functions - Token")

test_that("set_adl_token throws error if tenant is not set",{

  # set token if authentication is possible
  if(identical(Sys.getenv("client_id"), "")){NULL
  }else{
  # set token
  set_adl_token(tenant = Sys.getenv("tenant"), client_id = Sys.getenv("client_id"), client_secret = Sys.getenv("client_secret"))
  }

  # if not authentication is available then skip tests
  skip_if_no_auth()
  expect_error(read_adl_csv(adl_file_path =  Sys.getenv("adl_file_path_incorrect")))
})

context("ADL Read Functions - Pull csv")
test_that("df is returned is correct",{
  # set token if authentication is possible
  if(identical(Sys.getenv("client_id"), "")){NULL
  }else{
  # set token
  set_adl_token(tenant = Sys.getenv("tenant"), client_id = Sys.getenv("client_id"), client_secret = Sys.getenv("client_secret"))
  data(band_members)
  pulled_df <- read_adl_delim(adl_file_path =  Sys.getenv("adl_file_path_correct"), delim = ",", col_types = cols())
  }

  # if no authentication is available then skip tests
  skip_if_no_auth()
  expect_identical(nrow(pulled_df), nrow(band_members))
  expect_identical(ncol(pulled_df), ncol(band_members))
})

context("ADL Read Functions - Pull feather")
test_that("df is returned is correct",{
  # set token if authentication is possible
  if(identical(Sys.getenv("client_id"), "")){NULL
  }else{
  # set token
  set_adl_token(tenant = Sys.getenv("tenant"), client_id = Sys.getenv("client_id"), client_secret = Sys.getenv("client_secret"))
  # make test df
  data(band_members)
  pulled_df <- read_adl_feather(adl_file_path =  Sys.getenv("adl_file_path_correct_feather"))
  }

  # if no authentication is available then skip tests
  skip_if_no_auth()
  expect_identical(nrow(pulled_df), nrow(band_members))
  expect_identical(ncol(pulled_df), ncol(band_members))
})

context("ADL Read Functions - Pull pipe delim")
test_that("df is returned is correct",{
  # set token if authentication is possible
  if(identical(Sys.getenv("client_id"), "")){NULL
  }else{
  # set token
  set_adl_token(tenant = Sys.getenv("tenant"), client_id = Sys.getenv("client_id"), client_secret = Sys.getenv("client_secret"))
  # make test df
  pulled_df <- read_adl_delim(adl_file_path =  Sys.getenv("adl_file_path_correct_delim"), delim = "|", col_types = cols())
  }

  # if no authentication is available then skip tests
  skip_if_no_auth()
  expect_identical(nrow(pulled_df), nrow(band_members))
  expect_identical(ncol(pulled_df), ncol(band_members))
})
alexhallam/flyrod documentation built on Nov. 20, 2019, 7:33 a.m.