Skip to content

Commit

Permalink
Merge pull request #602 from MichaelChirico/patch-1
Browse files Browse the repository at this point in the history
Optionally skip tests requiring Suggests depenencies
  • Loading branch information
SebKrantz committed Jun 20, 2024
2 parents a53242b + 60c39a8 commit 2914868
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
4 changes: 2 additions & 2 deletions tests/testthat/test-fmatch.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ test_that("fmatch works well", {
# Proper Systematic Testing
###########################

library(kit) # count()
fmatch_base <- function(x, table, nomatch = NA_integer_, count = FALSE) {
if (count) skip_if_not_installed("kit")
if(is.list(x)) {
x <- do.call(paste0, x)
table <- do.call(paste0, table)
}
res <- match(x, table, nomatch)
if(count) {
attr(res, "N.nomatch") <- count(res, nomatch)
attr(res, "N.nomatch") <- kit::count(res, nomatch)
attr(res, "N.groups") <- length(table)
attr(res, "N.distinct") <- if(is.na(nomatch))
fndistinct.default(res) else fndistinct.default(res) - anyv(res, nomatch)
Expand Down
17 changes: 7 additions & 10 deletions tests/testthat/test-indexing.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ test_that("unindexing and reindexing work well", {

})

if(requireNamespace("magrittr", quietly = TRUE)) {
library(magrittr)

require(magrittr) # attach here for next two tests
test_that("subsetting works well", {
skip_if_not_installed("magrittr")

expect_equal(fsubset(wldi, iso3c %in% c("KEN", "USA", "CHN")),
findex_by(fsubset(wlddev, iso3c %in% c("KEN", "USA", "CHN")), country, year))
Expand All @@ -43,12 +42,12 @@ test_that("subsetting works well", {

})

if(requireNamespace("data.table", quietly = TRUE)) {
library(data.table)
wlddt <- qDT(wlddev)
wldidt <- wlddt %>% findex_by(iso3c, year)

test_that("indexed data.table works well", {
skip_if_not_installed("magrittr")
skip_if_not_installed("data.table")
library(data.table)
wlddt <- qDT(wlddev)
wldidt <- wlddt %>% findex_by(iso3c, year)

expect_equal(unindex(wldidt[1:1000]), wlddt[1:1000])
expect_equal(unindex(wldidt[year > 2000]), wlddt[year > 2000])
Expand All @@ -75,8 +74,6 @@ test_that("indexed data.table works well", {
unclass(wlddt[, .c(PCGDP_growth_5Y, LIFEEX_growth_5Y) := lapply(slt(.SD, PCGDP, LIFEEX), G, 5, 1, iso3c, year, power = 1/5)]))

})
}
}

test_that("data selection by type works well", {
for (FUN in list(num_vars, cat_vars, char_vars, logi_vars, fact_vars, date_vars))
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ test_that("adding and removing stubs works", {
})

test_that("zoo dispatch works well", {
skip_if_not_installed("zoo")

tsm <- zoo::as.zoo(EuStockMarkets)
set.seed(101)
Expand Down
46 changes: 22 additions & 24 deletions tests/testthat/test-miscellaneous-issues.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ test_that("Using a factor with unused levels does not pose a problem to statisti
}
})

if(requireNamespace("magrittr", quietly = TRUE)) {
library(magrittr)
test_that("Testing grouped_df methods", {
skip_if_not_installed("magrittr")
library(magrittr)
for(sortg in c(TRUE, FALSE)) {
for(retgrp in c(TRUE, FALSE)) {
gdf <- wlddev %>% fsubset(year > 1990, region, income, PCGDP:ODA) %>% fgroup_by(region, income, return.groups = retgrp, sort = sortg)
Expand Down Expand Up @@ -219,7 +219,6 @@ test_that("Testing grouped_df methods", {
}
}
})
}

# Also better not run on CRAN...
test_that("0-length vectors give expected output", {
Expand Down Expand Up @@ -433,27 +432,26 @@ test_that("issue with integer followed by NA #432", {
if(f %!in% c("fmode", "ffirst")) expect_equal(match.fun(f)(c(10, NA), g = rep(1L, 2), na.rm = FALSE, use.g.names = FALSE), NA_real_)
if(f != "flast") expect_equal(match.fun(f)(c(NA, 10), g = rep(1L, 2), na.rm = FALSE, use.g.names = FALSE), NA_real_)
}
if(Sys.getenv("OMP") == "TRUE") {
for (f in c("fsum", "fmean", "fmode", "fnth", "fmedian")) {
expect_equal(match.fun(f)(c(10L, rep(NA_integer_, 1e5)), nthreads = 2L), 10L)
expect_equal(match.fun(f)(c(rep(NA_integer_, 1e5), 10L), nthreads = 2L), 10L)
expect_equal(match.fun(f)(c(10, rep(NA_real_, 1e5)), nthreads = 2L), 10)
expect_equal(match.fun(f)(c(rep(NA_real_, 1e5), 10), nthreads = 2L), 10)
expect_equal(match.fun(f)(c(10L, rep(NA_integer_, 1e5)), g = rep(1L, 1e5+1), nthreads = 2L, use.g.names = FALSE), 10L)
expect_equal(match.fun(f)(c(rep(NA_integer_, 1e5), 10L), g = rep(1L, 1e5+1), nthreads = 2L, use.g.names = FALSE), 10L)
expect_equal(match.fun(f)(c(10, rep(NA_real_, 1e5)), g = rep(1L, 1e5+1), nthreads = 2L, use.g.names = FALSE), 10)
expect_equal(match.fun(f)(c(rep(NA_real_, 1e5), 10), g = rep(1L, 1e5+1), nthreads = 2L, use.g.names = FALSE), 10)
# na.rm = FALSE
expect_equal(match.fun(f)(c(10L, rep(NA_integer_, 1e5)), na.rm = FALSE, nthreads = 2L), NA_integer_)
expect_equal(match.fun(f)(c(rep(NA_integer_, 1e5), 10L), na.rm = FALSE, nthreads = 2L), NA_integer_)
expect_equal(match.fun(f)(c(10, rep(NA_real_, 1e5)), na.rm = FALSE, nthreads = 2L), NA_real_)
expect_equal(match.fun(f)(c(rep(NA_real_, 1e5), 10), na.rm = FALSE, nthreads = 2L), NA_real_)
# Some functions are optimized and don't check here
# expect_equal(match.fun(f)(c(10L, rep(NA_integer_, 1e5)), g = rep(1L, 1e5+1), na.rm = FALSE, nthreads = 2L, use.g.names = FALSE), NA_integer_)
# expect_equal(match.fun(f)(c(rep(NA_integer_, 1e5), 10L), g = rep(1L, 1e5+1), na.rm = FALSE, nthreads = 2L, use.g.names = FALSE), NA_integer_)
expect_equal(match.fun(f)(c(10, rep(NA_real_, 1e5)), g = rep(1L, 1e5+1), na.rm = FALSE, nthreads = 2L, use.g.names = FALSE), NA_real_)
expect_equal(match.fun(f)(c(rep(NA_real_, 1e5), 10), g = rep(1L, 1e5+1), na.rm = FALSE, nthreads = 2L, use.g.names = FALSE), NA_real_)
}
skip_if_not(Sys.getenv("OMP") == "TRUE")
for (f in c("fsum", "fmean", "fmode", "fnth", "fmedian")) {
expect_equal(match.fun(f)(c(10L, rep(NA_integer_, 1e5)), nthreads = 2L), 10L)
expect_equal(match.fun(f)(c(rep(NA_integer_, 1e5), 10L), nthreads = 2L), 10L)
expect_equal(match.fun(f)(c(10, rep(NA_real_, 1e5)), nthreads = 2L), 10)
expect_equal(match.fun(f)(c(rep(NA_real_, 1e5), 10), nthreads = 2L), 10)
expect_equal(match.fun(f)(c(10L, rep(NA_integer_, 1e5)), g = rep(1L, 1e5+1), nthreads = 2L, use.g.names = FALSE), 10L)
expect_equal(match.fun(f)(c(rep(NA_integer_, 1e5), 10L), g = rep(1L, 1e5+1), nthreads = 2L, use.g.names = FALSE), 10L)
expect_equal(match.fun(f)(c(10, rep(NA_real_, 1e5)), g = rep(1L, 1e5+1), nthreads = 2L, use.g.names = FALSE), 10)
expect_equal(match.fun(f)(c(rep(NA_real_, 1e5), 10), g = rep(1L, 1e5+1), nthreads = 2L, use.g.names = FALSE), 10)
# na.rm = FALSE
expect_equal(match.fun(f)(c(10L, rep(NA_integer_, 1e5)), na.rm = FALSE, nthreads = 2L), NA_integer_)
expect_equal(match.fun(f)(c(rep(NA_integer_, 1e5), 10L), na.rm = FALSE, nthreads = 2L), NA_integer_)
expect_equal(match.fun(f)(c(10, rep(NA_real_, 1e5)), na.rm = FALSE, nthreads = 2L), NA_real_)
expect_equal(match.fun(f)(c(rep(NA_real_, 1e5), 10), na.rm = FALSE, nthreads = 2L), NA_real_)
# Some functions are optimized and don't check here
# expect_equal(match.fun(f)(c(10L, rep(NA_integer_, 1e5)), g = rep(1L, 1e5+1), na.rm = FALSE, nthreads = 2L, use.g.names = FALSE), NA_integer_)
# expect_equal(match.fun(f)(c(rep(NA_integer_, 1e5), 10L), g = rep(1L, 1e5+1), na.rm = FALSE, nthreads = 2L, use.g.names = FALSE), NA_integer_)
expect_equal(match.fun(f)(c(10, rep(NA_real_, 1e5)), g = rep(1L, 1e5+1), na.rm = FALSE, nthreads = 2L, use.g.names = FALSE), NA_real_)
expect_equal(match.fun(f)(c(rep(NA_real_, 1e5), 10), g = rep(1L, 1e5+1), na.rm = FALSE, nthreads = 2L, use.g.names = FALSE), NA_real_)
}
})

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-pivot.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
context("pivot")

skip_if_not_installed("data.table")
library(data.table)
mtcDT <- qDT(mtcars)
mtcnaDT <- qDT(na_insert(mtcars))
Expand Down

0 comments on commit 2914868

Please sign in to comment.