tests/testthat/test-general.R

context("General functions supporting multiple formats")

test_that("set_column_attr", {
  expect_equal(set_column_attr(.data=data.frame(A=1, B=2, C=3),
                               A="test1",
                               .attr="label",
                               .allow_overwrite=TRUE,
                               .allow_multiple_values=TRUE,
                               .clear=FALSE),
               data.frame(A=structure(1, label="test1"), B=2, C=3),
               info="Setting a single attribute works.")
  expect_equal(set_column_attr(.data=data.frame(A=1, B=2, C=3),
                               A="test1",
                               B="test2",
                               .attr="label",
                               .allow_overwrite=TRUE,
                               .allow_multiple_values=TRUE,
                               .clear=FALSE),
               data.frame(A=structure(1, label="test1"),
                          B=structure(2, label="test2"),
                          C=3),
               info="Setting two attributes works.")
  expect_equal(set_column_attr(.data=data.frame(A=1, B=2, C=3),
                               A="test1",
                               B="test2",
                               .attr="foo",
                               .allow_overwrite=TRUE,
                               .allow_multiple_values=TRUE,
                               .clear=FALSE),
               data.frame(A=structure(1, foo="test1"),
                          B=structure(2, foo="test2"),
                          C=3),
               info=".attr is respected.")
  expect_equal(set_column_attr(.data=data.frame(A=structure(1, foo="test3"),
                                                B=structure(2, foo="test4"),
                                                C=3),
                               A="test1",
                               B="test2",
                               .attr="foo",
                               .allow_overwrite=TRUE,
                               .allow_multiple_values=TRUE,
                               .clear=FALSE),
               data.frame(A=structure(1, foo="test1"),
                          B=structure(2, foo="test2"),
                          C=3),
               info=".allow_overwrite is respected (overwriting).")
  expect_error(set_column_attr(.data=data.frame(A=structure(1, foo="test3"),
                                                B=structure(2, foo="test4"),
                                                C=3),
                               A="test1",
                               B="test2",
                               .attr="foo",
                               .allow_overwrite=FALSE,
                               .allow_multiple_values=TRUE,
                               .clear=FALSE),
               regex="The following columns will have attributes overwritten and .allow_overwrite is FALSE: A, B",
               fixed=TRUE,
               info=".allow_overwrite is respected (not overwriting).")
  expect_equal(set_column_attr(.data=data.frame(A=1,
                                                B=structure(2, foo="test1"),
                                                C=3),
                               A="test1",
                               .attr="foo",
                               .allow_overwrite=TRUE,
                               .allow_multiple_values=TRUE,
                               .clear=FALSE),
               data.frame(A=structure(1, foo="test1"),
                          B=structure(2, foo="test1"),
                          C=3),
               info=".allow_multiple_values is respected (multiple values).")
  expect_error(set_column_attr(.data=data.frame(A=structure(1, foo="test1"),
                                                B=structure(2, foo="test1"),
                                                C=3),
                               C="test2",
                               .attr="foo",
                               .allow_overwrite=TRUE,
                               .allow_multiple_values=FALSE,
                               .clear=FALSE),
               regex="Identical arguments will be set for (A|B) and (A|B)",
               info=".allow_multiple_values is respected (no multiple values, input, input).")
  expect_error(set_column_attr(.data=data.frame(A=1,
                                                B=structure(2, foo="test1"),
                                                C=3),
                               A="test1",
                               .attr="foo",
                               .allow_overwrite=TRUE,
                               .allow_multiple_values=FALSE,
                               .clear=FALSE),
               regex="Identical arguments will be set for (A|B) and (A|B)",
               info=".allow_multiple_values is respected (no multiple values, input, output).")
  expect_error(set_column_attr(.data=data.frame(A=1,
                                                B=2,
                                                C=3),
                               A="test1",
                               B="test1",
                               .attr="foo",
                               .allow_overwrite=TRUE,
                               .allow_multiple_values=FALSE,
                               .clear=FALSE),
               regex="Identical arguments will be set for (A|B) and (A|B)",
               info=".allow_multiple_values is respected (no multiple values, output, output).")
  expect_equal(set_column_attr(.data=data.frame(A=1,
                                                B=structure(2, foo="test1"),
                                                C=3),
                               A="test1",
                               .attr="foo",
                               .allow_overwrite=TRUE,
                               .allow_multiple_values=FALSE,
                               .clear=TRUE),
               data.frame(A=structure(1, foo="test1"),
                          B=2,
                          C=3),
               info=".allow_multiple_values is respected (no multiple values, input is cleared, output).")
})

test_that("get_column_attr", {
  expect_equal(get_column_attr(.data=data.frame(A=1, B=2, C=3),
                               .attr="label"),
               structure(list(), .Names=character(0)),
               info="Getting attributes from a data.frame without the attribute returns an empty, named list.")
  expect_equal(get_column_attr(.data=data.frame(A=1, B=structure(2, foo="test1"), C=3),
                               .attr="label"),
               structure(list(), .Names=character(0)),
               info="Getting attributes from a data.frame without the attribute but with a different attribute returns an empty, named list.")
  expect_equal(get_column_attr(.data=data.frame(A=1,
                                                B=structure(2, foo="test1"),
                                                C=3),
                               .attr="foo"),
               list(B="test1"),
               info="Getting attributes from a data.frame with the attribute returns a named list.")
})
billdenney/pknca.portation documentation built on May 3, 2019, 2:55 p.m.