tests/testthat/test-remove_group_subsets.R

test_that("remove_group_subsets works as expected", {

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

  out1 <- remove_group_subsets(x1, group = "g", by = c("x1", "x2"))
  out1 <- out1[order(out1$g, out1$x1),]
  expect_equal(out1$x1, c("a", "a", "a", "b", "b", "a", "b"))


  # multiple groups no duplicates
  x2 <- data.frame(
    g =  c("1", "1", "1",  "2", "2",  "3", "3"),
    x1 = c("a", "b", "c",  "d", "e",  "f", "g"),
    x2 = c("a", "b", "c",  "d", "e",  "f", "g"),
    stringsAsFactors = FALSE
  )

  out2 <- remove_group_subsets(x2, group = "g", by = c("x1", "x2"))
  out2 <- out2[order(out2$g, out2$x1),]
  expect_equal(out2$x2, c("a", "b", "c", "d", "e", "f", "g"))


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

  out3 <- remove_group_subsets(x3, group = "g", by = c("x1", "x2"))
  expect_identical(out3, x3)


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

  out4 <- remove_group_subsets(x4, group = "g", by = c("x1", "x2"))
  expect_is(out4, "data.frame")
})



test_that("remove_group_subsets fails gracefully", {

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

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

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