tests/testthat/test-nest_tree.R

test_that("nest_tree checks arguments (#163)", {
  df <- tibble::tibble(
    id = 1:3,
    x = c("a", "b", "c"),
    parent = c(NA, 1L, 1L)
  )

  expect_snapshot({
    (expect_error(nest_tree(1L), class = "tibblify-error-invalid_data_frame"))

    (expect_error(
      nest_tree(df, "not-there"),
      class = "vctrs_error_subscript_oob"
    ))
    (expect_error(
      nest_tree(df, 1:2),
      class = "tibblify-error-invalid_column_selection"
    ))

    (expect_error(
      nest_tree(df, id, parent_col = "not-there"),
      class = "vctrs_error_subscript_oob"
    ))
    (expect_error(
      nest_tree(df, id, parent_col = 1:2),
      class = "tibblify-error-invalid_column_selection"
    ))
    (expect_error(
      nest_tree(df, id, parent_col = id),
      class = "tibblify-error-args_same_value"
    ))

    (expect_error(
      nest_tree(df, id, parent, children_to = 1L),
      class = "vctrs_error_cast"
    ))
    (expect_error(
      nest_tree(df, id, parent, children_to = c("a", "b")),
      class = "vctrs_error_assert_size"
    ))
    (expect_error(
      nest_tree(df, id, parent, children_to = "id"),
      class = "tibblify-error-args_same_value"
    ))
    (expect_error(
      nest_tree(df, id, parent, children_to = "parent"),
      class = "tibblify-error-args_same_value"
    ))
  })
})

test_that("nest_tree checks that ids are valid (#163)", {
  # missing ids
  df <- tibble::tibble(
    id = c(1, NA),
    x = c("a", "b"),
    parent = c(NA, 1)
  )

  expect_snapshot({
    (expect_error(nest_tree(df, id, parent, children_to = "children")))
  })

  # duplicated ids
  df <- tibble::tibble(
    id = c(1, 1),
    x = c("a", "b"),
    parent = c(NA, 1)
  )

  expect_snapshot({
    (expect_error(nest_tree(df, id, parent, children_to = "children")))
  })
})

test_that("nest_tree checks column ids and parents have compatible types (#163)", {
  df <- tibble::tibble(
    id = 1:3,
    x = c("a", "b", "c"),
    parent = c("a", "b", "c")
  )

  expect_snapshot({
    (expect_error(nest_tree(df, id, parent, children_to = "children")))
  })
})

test_that("nest_tree errors if not all parent ids found (#163)", {
  df <- tibble::tibble(
    id = 1:3,
    x = c("a", "b", "c"),
    parent = c(NA, 4L, 5L)
  )

  expect_snapshot({
    (expect_error(nest_tree(df, id, parent, children_to = "children")))
    (expect_error(nest_tree(df[1:2, ], id, parent, children_to = "children")))
  })
})

test_that("nest_tree errors if parent references to itself (#163)", {
  df <- tibble::tibble(
    id = c(1, 2),
    x = c("a", "b"),
    parent = c(NA, 2)
  )

  expect_snapshot({
    (expect_error(nest_tree(df, id, parent, children_to = "children")))
  })
})

test_that("nest_tree errors if there are no root elements (#163)", {
  df <- tibble::tibble(
    id = c(1, 2),
    x = c("a", "b"),
    parent = c(2, 1)
  )

  expect_snapshot({
    (expect_error(nest_tree(df, id, parent, children_to = "children")))
  })
})

test_that("nest_tree errors if there are detached parts of the tree (#163)", {
  df <- tibble::tibble(
    id = c(1, 2, 3),
    x = c("a", "b", "c"),
    parent = c(NA, 3, 2)
  )

  expect_snapshot({
    (expect_error(
      nest_tree(df, id, parent, children_to = "children"),
      class = "tibblify-error-detached_tree_parts"
    ))
  })
})

test_that("nest_tree can nest (#163)", {
  # simple case
  df <- tibble::tibble(
    id = 1:3,
    x = c("a", "b", "c"),
    parent = c(NA, 1L, 1L)
  )

  expect_equal(
    nest_tree(df, id, parent, "children"),
    tibble::tibble(
      id = 1L,
      x = "a",
      children = list(
        tibble::tibble(
          id = 2:3,
          x = c("b", "c"),
          children = list(NULL)
        )
      )
    )
  )

  # some elements with children, others not
  df <- tibble::tibble(
    id = 1:3,
    x = c("a", "b", "c"),
    parent = c(NA, NA, 1L)
  )

  expect_equal(
    nest_tree(df, id, parent, "children"),
    tibble::tibble(
      id = 1:2,
      x = c("a", "b"),
      children = list(
        tibble::tibble(
          id = 3L,
          x = "c",
          children = list(NULL)
        ),
        NULL
      )
    )
  )

  # deep nesting
  df <- tibble::tibble(
    id = 1:6,
    x = letters[1:6],
    parent = c(NA, NA, 1L, 2L, 3L, 5L)
  )

  expect_equal(
    nest_tree(df, id, parent, "children"),
    tibble::tibble(
      id = 1:2,
      x = c("a", "b"),
      children = list(
        tibble::tibble(
          id = 3L,
          x = "c",
          children = list(
            tibble::tibble(
              id = 5L,
              x = "e",
              children = list(
                tibble::tibble(
                  id = 6L,
                  x = "f",
                  children = list(NULL)
                )
              )
            )
          )
        ),
        tibble::tibble(
          id = 4,
          x = "d",
          children = list(NULL)
        )
      )
    )
  )
})

test_that("nest_tree can handle 0-row data (#163)", {
  df <- tibble::tibble(
    id = integer(),
    x = character(),
    parent = integer()
  )

  expect_equal(
    nest_tree(df, id, parent, "children"),
    tibble::tibble(
      id = integer(),
      x = character(),
      children = list()
    )
  )
})

Try the tibblify package in your browser

Any scripts or data that you put into this service are public.

tibblify documentation built on May 9, 2026, 5:07 p.m.