tests/testthat/test_getSliceData.R

context("Test getSliceData")

# Test generic function
test_that("function returns correct data.", {

  start.date <- as.Date("2018-10-29")
  end.date   <- as.Date("2018-10-30")
  columns    <- c("open", "high", "low", "close", "volume", "dividend",
                  "adj.open", "adj.high", "adj.low", "adj.close",
                  "tover", "roc.pc2tc", "roc.pc2to", "roc.to2tc")

  # Test both Sharadar and QuoteMedia
  for (dbcon.str in c(settings$sharadar.sqli,
                      settings$sharadar_plus.sqli,
                      settings$quotemedia.sqli)) {

    symbols <- getActiveSymbols(dbcon.str, start.date, end.date)
    data <- getSliceData(dbcon.str, columns, start.date, end.date,
                         symbols, FALSE, NULL, NULL, NULL, NULL)

    # Check type and dim
    expect_is(data, "list")
    expect_equal(nrow(data$close), 2)
    expect_lte(ncol(data$close), length(symbols))

    # Check all list names
    expect_equal(names(data), columns)

    # Check all list contents are xts
    for (i in 1:length(data)) expect_is(data[[i]], "xts")

    # Check values of un-adjusted price
    expect_equal(data$open[start.date, "AAPL"]  %>% as.numeric(), 219.19)
    expect_equal(data$high[start.date, "AAPL"]  %>% as.numeric(), 219.69)
    expect_equal(data$low[start.date, "AAPL"]   %>% as.numeric(), 206.09)
    expect_equal(data$close[start.date, "AAPL"] %>% as.numeric(), 212.24)

    expect_equal(data$open[end.date, "AAPL"]    %>% as.numeric(), 211.15)
    expect_equal(data$high[end.date, "AAPL"]    %>% as.numeric(), 215.18)
    expect_equal(data$low[end.date, "AAPL"]     %>% as.numeric(), 209.27)
    expect_equal(data$close[end.date, "AAPL"]   %>% as.numeric(), 213.30)

    expect_equal(data$dividend[start.date, "CBAN"] %>% as.numeric(), 0.05)
  }
})

test_that("function check inputs correctly.", {

  # Test both Sharadar and QuoteMedia
  for (dbcon.str in c(settings$sharadar.sqli,
                      settings$sharadar_plus.sqli,
                      settings$quotemedia.sqli)) {

    expect_error(getSliceData(dbcon.str, "close", start.date = "hoge"),
                 "character string is not in a standard unambiguous format")

    expect_error(getSliceData(dbcon.str, "close", end.date = "fuga"),
                 "character string is not in a standard unambiguous format")

    expect_error(getSliceData(dbcon.str, columns = "hoge"),
                 "hoge is not supported column.")

    expect_error(getSliceData(dbcon.str, columns = "close",
                              start.date = "1950-01-01",
                              end.date = "1950-12-31"),
                 "*No data found for close. (1950-01-01 - 1950-12-31)*")
  }
})
tmk-c/myrlib documentation built on May 29, 2019, 1:44 p.m.