tests/testthat/test-strr_expand.R

#### TESTS FOR strr_expand #####################################################

### Setup ######################################################################

context("strr_expand tests")

daily_expand <-
  dplyr::tibble(
    property_ID = c(rep("ab-1", 3), rep("ab-2", 3)),
    start_date = as.Date(c(
      "2018-04-01", "2018-04-06", "2018-04-08",
      "2018-04-01", "2018-04-05", "2018-04-08")),
    end_date = as.Date(c(
      "2018-04-05", "2018-04-07", "2018-04-20",
      "2018-04-04", "2018-04-07", "2018-04-10")),
    status = c("A", "R", "A", "A", "B", "R"),
    booked_date = NA,
    price = c(100, 100, 50, 40, 100, 60),
    res_ID = NA,
    host_ID = c("A", "A", "A", "B", "B", "B"),
    listing_type = "Entire home/apt",
    housing = TRUE,
    country = "Canada",
    region = "Québec",
    city = c("Montreal", "Montreal", "Montreal", "Laval", "Laval", "Laval")
    )

daily_enormous <-
  dplyr::tibble(
    property_ID = c(rep("ab-1", 3), rep("ab-2", 3)),
    start_date = as.Date(c(
      "2001-04-01", "2001-04-06", "2001-04-08",
      "2001-04-01", "2001-04-05", "2001-04-08")),
    end_date = as.Date(c(
      "2218-04-05", "2218-04-07", "2218-04-20",
      "2218-04-04", "2218-04-07", "2218-04-10")),
    status = c("A", "R", "A", "A", "B", "R"),
    booked_date = NA,
    price = c(100, 100, 50, 40, 100, 60),
    res_ID = NA,
    host_ID = c("A", "A", "A", "B", "B", "B"),
    listing_type = "Entire home/apt",
    housing = TRUE,
    country = "Canada",
    region = "Québec",
    city = c("Montreal", "Montreal", "Montreal", "Laval", "Laval", "Laval")
    )

host_expand <-
  dplyr::tibble(
    host_ID = c("10000029", "10000029", "10000029", "1000008", "1000008",
                "1000008", "1000014", "1000014", "1000014", "1000014"),
    start_date = as.Date(c("2014-10-01", "2014-11-01", "2014-12-01",
                           "2014-10-01", "2014-11-01", "2014-12-01",
                           "2014-10-01", "2014-10-03", "2014-10-07",
                           "2014-10-16")),
    end_date = as.Date(c("2014-10-31", "2014-11-30", "2014-12-31", "2014-10-31",
                         "2014-11-30", "2014-12-31", "2014-10-01", "2014-10-05",
                         "2014-10-14", "2014-10-31")),
    listing_type = "Entire home/apt",
    housing = TRUE,
    count = 1
  )


### Tests ######################################################################

test_that("function succeeds with no errors", {
  # Daily
  expect_equal(nrow(strr_expand(daily_expand)), 30)
  # Host
  expect_equal(nrow(strr_expand(host_expand)), 212)
})

test_that("the quiet flag suppresses all messages", {
  expect_message(strr_expand(daily_expand, quiet = TRUE), regexp = NA)
})

# test_that("batches work", {
#   skip_if_not(isTRUE(as.logical(Sys.getenv("TESTCOV"))) | run_all_tests)
#   expect_equal({
#
#     daily_expand_batches <-
#       data.table::rbindlist(replicate(1800000, daily_expand, simplify = FALSE))
#
#     props <- rep(paste0("ab-00", letters[1:24]), 3)
#     props <- sort(props)
#     props <- rep(props, 150000)
#     daily_expand_batches$property_ID <- props
#
#     nrow(strr_expand(daily_expand_batches))
#     }, 54000000)
#
#   expect_equal({
#
#     host_expand_batches <-
#       data.table::rbindlist(replicate(1000800, host_expand, simplify = FALSE))
#
#     props <- rep(paste0("h-", letters[1:24]), 3)
#     props <- sort(props)
#     props <- rep(props, 139000)
#     host_expand_batches$host_ID <- props
#
#     nrow(strr_expand(host_expand_batches))
#     }, 212169600)
#
# })

test_that("enormous tables are flagged", {
  skip_if_not(isTRUE(as.logical(Sys.getenv("TESTCOV"))) | run_all_tests)
  expect_error(
    strr_expand(
      data.table::rbindlist(replicate(50000001, daily_enormous,
                                      simplify = FALSE))
      )
  )
})

test_that("The quiet flag suppresses all messages", {
  expect_message(strr_expand(daily_expand, quiet = TRUE), regexp = NA)
})
UPGo-McGill/strr documentation built on Feb. 24, 2024, 6:15 p.m.