tests/testthat/test-is_group_subset.R

test_that("is_group_subset works as expected", {

  # target has single group
  target <- data.frame(g =  c("2", "2", "2"),
                       x1 = c("a", "c", "c"),
                       x2 = c("a", "c", "c"),
                       stringsAsFactors = FALSE)

  x1 <- data.frame(g =  c("1", "1"),
                   x1 = c("c", "c"),
                   x2 = c("c", "c"),
                   stringsAsFactors = FALSE)

  x2 <- data.frame(g =  c("1", "1"),
                   x1 = c("b", "c"),
                   x2 = c("b", "c"),
                   stringsAsFactors = FALSE)

  expect_true(is_group_subset(x1, target, group = "g", by = c("x1", "x2")))
  expect_false(is_group_subset(x2, target, group = "g", by = c("x1", "x2")))


  # target has duplicates
  target <- data.frame(
    g =  c("1", "1", "1",  "2", "2",  "3", "3",  "4", "4",  "5", "5",  "6", "6", "6"),
    x1 = c("a", "a", "a",  "b", "b",  "a", "a",  "b", "b",  "a", "b",  "a", "b", "c"),
    x2 = c("a", "a", "a",  "b", "b",  "a", "a",  "b", "b",  "a", "b",  "a", "b", "c"),
    stringsAsFactors = FALSE
  )

  x3 <- data.frame(
    g =  c("7", "7"),
    x1 = c("a", "e"),
    x2 = c("a", "e"),
    stringsAsFactors = FALSE
  )

  x4 <- data.frame(
    g =  c("7", "7"),
    x1 = c("a", "a"),
    x2 = c("a", "a"),
    stringsAsFactors = FALSE
  )

  expect_false(is_group_subset(x3, target, by = c("x1", "x2"), group = "g"))
  expect_true(is_group_subset(x4, target, by = c("x1", "x2"), group = "g"))

  # target has no duplicates
  target <- data.frame(
    g =  c("1", "1", "1", "2", "2", "3", "3", "4", "4", "5", "5", "6", "6"),
    x1 = letters[1:13],
    x2 = letters[1:13],
    stringsAsFactors = FALSE
  )

  x5 <- data.frame(
    g =  c("7", "7"),
    x1 = c("b", "c"),
    x2 = c("b", "c"),
    stringsAsFactors = FALSE
  )

  x6 <- data.frame(
    g =  c("7", "7"),
    x1 = c("a", "a"),
    x2 = c("a", "a"),
    stringsAsFactors = FALSE
  )

  expect_true(is_group_subset(x5, target, by = c("x1", "x2"), group = "g"))
  expect_false(is_group_subset(x6, target, by = c("x1", "x2"), group = "g"))

  # large target
  target <- data.frame(
    g = replicate(1e4, paste0(sample(letters[1:5]), collapse = "")),
    x1 = sample(letters, 1e4, replace = TRUE),
    x2 = sample(letters, 1e4, replace = TRUE),
    stringsAsFactors = FALSE
  )

  x7 <- data.frame(
    g =  c("7", "7"),
    x1 = c("a", "a"),
    x2 = c("a", "a"),
    stringsAsFactors = FALSE
  )

  expect_is(is_group_subset(x7, target, by = c("x1", "x2"), group = "g"),
            "logical")
})



test_that("is_group_subset fails gracefully", {

  x <- data.frame(g =  c("1", "1"),
                  x1 = c("c", "c"),
                  x2 = c("c", "c"),
                  stringsAsFactors = FALSE)

  y <- data.frame(g =  c("2", "2", "2"),
                  x1 = c("a", "c", "c"),
                  x2 = c("a", "c", "c"),
                  stringsAsFactors = FALSE)

  expect_error(is_group_subset(x, y, group = "g", by = c("x1", "blah")))
  expect_error(is_group_subset(x, y, group = "blah", by = c("x1", "x2")))
})
epicentre-msf/llutils documentation built on Nov. 9, 2020, 8:24 p.m.