tests/testthat/test-functions.R

library(mapmate)
suppressMessages({
  library(dplyr)
  library(purrr)
})
context("functions.R")

test_that("project_to_hemisphere returns valid output", {
  p <- project_to_hemisphere(0, 0, 0, 0)
  expect_is(p, "tbl_df")
  expect_is(p, "tbl")
  expect_is(p, "data.frame")
  expect_equal(ncol(p), 3)
  expect_equal(nrow(p), 1)
  expect_is(p$inview, "logical")
  expect_is(project_to_hemisphere(-180:180, seq(-90, 90, length.out=361), 40, -60), "tbl_df")
  expect_equal(nrow(project_to_hemisphere(-180:180, seq(-90, 90, length.out=361), -60, 40)), 361)
  expect_error(project_to_hemisphere(1:360, 1:359, 0, 0), "lon and lat must be equal length")
  expect_error(project_to_hemisphere(1:91, 1:91, 0, 0), "latitudes must be >= -90 and <= 90")
  expect_error(project_to_hemisphere(-181, 0, 0, 0), "longitudes must be >= -180 and <= 180")
})

data(annualtemps)
x <- map(1:4, ~mutate(filter(annualtemps, Year-2009==.x), idx=.x))
n <- 6

test_that("pad_frames returns valid output", {
  expect_error(pad_frames(5, n.period=n, rotation="add"), "'x' must be a list.")
  expect_error(pad_frames(data.frame(a=5), n.period=n, rotation="add"), "'x' must be a list.")
  expect_error(pad_frames(x, n.period=n, rotation="add"), "'id' column is missing.")
  expect_error(pad_frames(x, id="idx1", n.period=n, rotation="add"), "'id' must refer to a column name.")
  x.add <- pad_frames(x, id="idx", n.period=n, rotation="add")
  x.pad <- pad_frames(x, id="idx", n.period=n, rotation="pad")
  expect_is(x.add, "list")
  expect_is(x.add[[1]], "tbl_df")
  expect_is(x.pad, "list")
  expect_is(x.pad[[1]], "tbl_df")
  expect_equal(length(x.add), length(x) + n - 1)
  expect_equal(length(x.pad), n)
  expect_identical(x[[length(x)]] %>% dplyr::select(-idx), x.add[[length(x) + n - 1]]  %>% dplyr::select(-idx))
  expect_error(x.add[[length(x) + n]], "subscript out of bounds")
  expect_identical(x[[length(x)]]  %>% dplyr::select(-idx), x.pad[[n]]  %>% dplyr::select(-idx))
  expect_error(x.add[[length(x) + n + 1]], "subscript out of bounds")
})

test_that("get_lonlat_seq returns valid output", {
  expect_error(get_lonlat_seq(c(1, 2), c(1, 2)), "lon must be length one or length n.period")
  expect_error(get_lonlat_seq(1, c(1, 2)), "lat must be length one or length n.period")
  expect_error(get_lonlat_seq(180.1, -90.1), "lon invalid")
  expect_error(get_lonlat_seq(0, -90.1), "lat invalid")

  ll <- get_lonlat_seq(0, 0, n.period=360, n.frames=40)
  expect_is(ll, "list")
  expect_equal(length(ll), 2)
  expect_equal(length(ll[[1]]), length(ll[[2]]))
  expect_equal(length(ll[[1]]), 40)

  ll <- get_lonlat_seq(0, 0, n.period=60)
  expect_is(ll, "list")
  expect_equal(length(ll), 2)
  expect_equal(length(ll[[1]]), length(ll[[2]]))
  expect_equal(length(ll[[1]]), 60)

  ll <- get_lonlat_seq(1:60, 2:61, n.period=60)
  expect_is(ll, "list")
  expect_equal(length(ll), 2)
  expect_equal(length(ll[[1]]), length(ll[[2]]))
  expect_equal(length(ll[[1]]), 60)
  expect_identical(ll[[1]], 1:60)
  expect_identical(ll[[2]], 2:61)
})

test_that("do_projection returns valid output", {
  x0 <- mutate(annualtemps, frameID = Year - min(Year) + 1)
  x1 <- do_projection(x0, id="frameID")
  x2 <- do_projection(x0, id="frameID", keep=TRUE)

  expect_error(do_projection(x0), "'id' column is missing.")
  expect_error(do_projection(x0, id="a"), "'id' must refer to a column name.")

  expect_is(x1, "data.frame")
  expect_is(x2, "data.frame")
  expect_equal(nrow(x0), nrow(x2))
  expect_equal(ncol(x0), ncol(x1))
  expect_equal(ncol(x0), ncol(x2) - 1)
  expect_is(x2$inview, "logical")
  expect_identical(x1, dplyr::filter(x2, inview==TRUE) %>% dplyr::select(-inview))
  expect_identical(x0, dplyr::select(x2, -inview))
})
leonawicz/mapmate documentation built on May 21, 2019, 5:09 a.m.