tests/testthat/test-daily.R

context("Daily periods")

load_data <- function() {
  h <- new.env()
  load("testdata/history.RData",envir=h)
  return(get("history",envir=h))
}

test_that("daily adjusted works", {
  h <- load_data()
  XLE <- get("XLE",envir=h)
  rv <- daily_ad(XLE)

  fv <- as.numeric(xts::last(rv))
  expect_gt(fv,74.5)
  expect_lt(fv,74.6)
})

test_that("daily adjusted errors",{
  expect_error(daily_ad(NA))
})

test_that("daily returns defaults",{
  h <- load_data()
  rv <- daily_returns(c("XLE","XLK"),env=h,n_roc=1)

  expect_equal(ncol(rv),2)
  expect_equal(nrow(rv),519)
  rv <- xts::last(rv)
  fv <- as.numeric(rv[1,2])
  expect_gt(fv,0.000)
  expect_lt(fv,0.039)
})

test_that("daily returns paramter",{
  h <- load_data()
  rv <- daily_returns(c("XLE","XLK"),env=h,n_roc=3)

  expect_equal(ncol(rv),2)
  expect_equal(nrow(rv),519)
  rv <- xts::last(rv)
  fv <- as.numeric(rv[1,2])
  expect_gt(fv,0.0)
  expect_lt(fv,0.064)
})


test_that("daily returns environment",{
  expect_error(daily_returns(c("XLE","XLK"),env="none"))
})

test_that("daily prices works",{
  h <- load_data()
  rv <- daily_prices(c("XLE","XLK"),env=h)
  expect_equal(ncol(rv),2)
  expect_equal(nrow(rv),519)
  rv <- xts::last(rv)
  fv <- as.numeric(rv[1,2])
  expect_gt(fv,50.1)
  expect_lt(fv,50.3)
})
greatgray/scorecard documentation built on May 17, 2019, 8:34 a.m.