tests/testthat/test-api-interface.R

context("test-api-interface")

test_that("Schedules are ok", {
  tmpdir <- withr::local_tempdir()
  withr::local_options("HockeyModel.prediction.path" = tmpdir)

  sched <- getNHLSchedule()
  expect_true(is.data.frame(sched))
  expect_equal(ncol(sched), 6)
  expect_equal(colnames(sched), c("Date", "HomeTeam", "AwayTeam", "GameID", "GameType", "GameStatus"))
  expect_true(all(sched$GameType %in% c("R", "P")))
})

test_that("Scores are OK", {
  tmpdir <- withr::local_tempdir()
  withr::local_options("HockeyModel.prediction.path" = tmpdir)
  withr::local_file(file.path(tmpdir, "xG.csv"))
  write.table(data.frame("GameId" = 2020020001, "home_xg" = 4.3, "away_xg" = 3.1),
    file = file.path(tmpdir, "xG.csv"),
    row.names = FALSE, col.names = TRUE, sep = ","
  )

  score <- getNHLScores(2020020001, progress = F)
  expect_true(is.data.frame(score))
  expect_equal(ncol(score), 12)
  expect_equal(nrow(score), 1)
  goodscore <- structure(list(
    Date = structure(18640, class = "Date"), HomeTeam = "Philadelphia Flyers",
    AwayTeam = "Pittsburgh Penguins", GameID = 2020020001, HomeGoals = 6L,
    AwayGoals = 3L, OTStatus = "", GameType = "R", GameStatus = "Final",
    Result = 1, HomexG = 4.3, AwayxG = 3.1
  ), row.names = c(NA, -1L), class = "data.frame")
  expect_identical(score, goodscore)

  today <- games_today(date = as.Date("2019-11-01"))
  expect_true(is.null(today)) # Why null? because games_today only returns 'scheduled' games from a date. NULL return is equivalent to finishing the code anyway (i.e. not an error)
})

test_that("Series is ok", {
  # tough to test as it's a moving target
  tmpdir <- withr::local_tempdir()
  withr::local_options("HockeyModel.prediction.path" = tmpdir)

  series <- getAPISeries()
  if (inPlayoffs()) {
    # now there should be a series
    expect_gt(nrow(series), 0)
    expect_true(is.data.frame(series))
  }
  series <- getAPISeries("20182019")
  expect_true(is.data.frame(series))
  expect_equal(nrow(series), 15)
  expect_equal(ncol(series), 10)
  expect_true(all(series$Status == "Complete"))
})

test_that("Season Dates & Binaries work", {
  tmpdir <- withr::local_tempdir()
  withr::local_options("HockeyModel.prediction.path" = tmpdir)

  expect_visible(inRegularSeason())
  expect_visible(inPlayoffs())
  expect_visible(inOffSeason())
  expect_false(any(inRegularSeason(), inPlayoffs()) == inOffSeason())
  expect_equal(inRegularSeason("2018-12-02", boolean = FALSE), "20182019")
  expect_false(inPlayoffs("2018-12-02", boolean = FALSE))
  expect_true(inOffSeason("2018-08-01"))
})

test_that("SeasonID gets seasons ok", {
  tmpdir <- withr::local_tempdir()
  withr::local_options("HockeyModel.prediction.path" = tmpdir)

  expect_match(getCurrentSeason8(), regexp = "\\d{8}")
  expect_equal(getSeason("2018-12-02"), "20182019")
  expect_null(getSeason("2018-09-01"))
})

test_that("Get Team Info is OK", {
  tmpdir <- withr::local_tempdir()
  withr::local_options("HockeyModel.prediction.path" = tmpdir)

  expect_equal(getTeamDivisions("bob"), character(0))
})

test_that("Other Utility Functions are OK", {
  tmpdir <- withr::local_tempdir()
  withr::local_options("HockeyModel.prediction.path" = tmpdir)

  expect_equal(clean_names(c("Chicago Blackhawks", "Toronto Maple Leafs")), c("Chicago Blackhawks", "Toronto Maple Leafs"))
  expect_equal(getTeamConferences("Chicago Blackhawks"), "Western")
  expect_equal(getTeamConferences("Toronto Maple Leafs"), "Eastern")
  expect_equal(getTeamDivisions("Toronto Maple Leafs"), "Atlantic")
  expect_equal(getShortTeam("Toronto Maple Leafs"), "TOR")
  expect_equal(getSeasonEndDate(season = "20182019"), as.Date("2019-06-12"))
  expect_equal(getNumGames("20202021"), 56)
})
pbulsink/HockeyModel documentation built on Dec. 16, 2024, 8:03 a.m.