tests/testthat/test-update.R

context("Scorecard update")

build_filename <- function() {
  ts <- format(lubridate::today("America/Chicago"), "%Y%m%d")
  fs <- paste0(ts,"_scorecard_workspace.RData")
  return(fs)
}


test_that("Update without directory fails",{
  wsd <- "testdata"
  expect_error( scorecard_update("",
                                 file.path(wsd,"scorecard.yml"),
                                 file.path(wsd,"transactions.csv")))
})

test_that("Update bogus scorecard fails",{
  wsd <- "testdata"
  expect_error( scorecard_update(wsd,
                                 file.path(wsd,"bogus.yml"),
                                 file.path(wsd,"transactions.csv")))
})

test_that("Update bogus transactions fails",{
  wsd <- "testdata"
  expect_error( scorecard_update(wsd,
                                 file.path(wsd,"scorecard.yml"),
                                 file.path(wsd,"bogus.csv")))
})



test_that("Basic update", {
  wsd <- "testdata"
  # warning on stale prices for XLE, XLK owing to test data
  scorecard_update(wsd,
                   file.path(wsd,"scorecard.yml"),
                   file.path(wsd,"transactions.csv"))

  fs <- build_filename()
  expect_true(file.exists(file.path(wsd,fs)))
})

test_that("Workspace contents",{
  # fs <- build_filename()
  # ts <- format(lubridate::today("America/Chicago"), "%Y%m%d")
  ts <- "20170125"
  fs <- paste0(ts,"_scorecard_workspace.RData")
  return(fs)

  wsd <- "testdata"

  expect_true(file.exists(file.path(wsd,fs)))
  ws <- new.env()
  load(file.path(wsd,fs),envir=ws)

  expect_true("init_date" %in% ls(ws))
  expect_true("image_date" %in% ls(ws))
  expect_true("scorecard_table" %in% ls(ws))

  rm(ws)
})

test_that("Produce without directory fails",{
  expect_error( scorecard_produce("","") )
  expect_error( scorecard_produce() )
})

test_that("Produce with bogus workspace fails",{
  wsd <- "testdata"
  expect_error( scorecard_produce(wsd) )
  expect_error( scorecard_produce(wsd, "bogus.Rdata"))
})

test_that("Produce plots",{
  wsd <- "testdata"

  scorecard_update(wsd,
                   file.path(wsd,"scorecard.yml"),
                   file.path(wsd,"transactions.csv"))

  fs <- build_filename()
  wsfp <- file.path(wsd,fs)
  ignore <- scorecard_produce(wsd,wsfp)

  ws <- new.env()
  load(file.path(wsd,fs),envir=ws)
  image_date <- get("image_date",envir=ws)

  expect_true(file.exists(file.path(wsd,paste0(image_date,"_sc_actual.png"))))
  expect_true(file.exists(file.path(wsd,paste0(image_date,"_sc_benchmark.png"))))
  expect_true(file.exists(file.path(wsd,paste0(image_date,"_sc_expected.png"))))
  expect_true(file.exists(file.path(wsd,paste0(image_date,"_sc_buyhold.png"))))
  expect_true(file.exists(file.path(wsd,paste0(image_date,"_sc_oos.png"))))

  rm(ws)
})
greatgray/scorecard documentation built on May 17, 2019, 8:34 a.m.