tests/testthat/test-io-long.R

test_that("make_long_from_workbook reconhece formato LONG", {

  skip_if_not_installed("openxlsx")

  d <- expand.grid(
    bloco = c("b1", "b2"),
    tratamento = c("Agua", "Produto"),
    tempo = 1:3,
    classe = c("F0", "F1")
  )

  d$frequencia <- 5

  arq <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(
    list(Dados = d),
    arq
  )

  res <- infectionIndex:::make_long_from_workbook(arq)

  expect_equal(attr(res, "format"), "LONG")
  expect_equal(attr(res, "source_sheet"), "Dados")

  expect_true(all(c(
    "block", "trat", "time", "class", "freq"
  ) %in% names(res)))

  expect_equal(nrow(res), nrow(d))
  expect_equal(nlevels(res$trat), 2L)
  expect_equal(sort(unique(res$time)), 1:3)
  expect_true(all(res$freq == 5))
})


test_that("LONG normaliza nomes dos tratamentos", {

  skip_if_not_installed("openxlsx")

  d <- data.frame(
    bloco = rep(c("b1", "b2"), each = 4),
    tratamento = rep(c("  AGUA  ", "Produto"), each = 4),
    tempo = rep(1:2, each = 2, times = 2),
    classe = rep(c("F0", "F1"), 4),
    frequencia = rep(5, 8)
  )

  arq <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(d, arq)

  res <- infectionIndex:::make_long_from_workbook(arq)

  expect_setequal(
    levels(res$trat),
    c("agua", "produto")
  )
})


test_that("sheet_prefer prioriza a aba solicitada", {

  skip_if_not_installed("openxlsx")

  make_long <- function(prefixo) {
    data.frame(
      block = rep(c("b1", "b2"), each = 4),
      trat = rep(c("A", "B"), each = 4),
      time = rep(1:2, each = 2, times = 2),
      class = rep(c("F0", "F1"), 4),
      freq = rep(5, 8)
    )
  }

  arq <- tempfile(fileext = ".xlsx")

  openxlsx::write.xlsx(
    list(
      primeira = make_long("x"),
      CRD_long = make_long("y")
    ),
    arq
  )

  res <- infectionIndex:::make_long_from_workbook(
    arq,
    sheet_prefer = "CRD_long"
  )

  expect_equal(
    attr(res, "source_sheet"),
    "CRD_long"
  )
})


test_that("make_long_from_workbook converte WIDE para LONG", {

  skip_if_not_installed("openxlsx")

  d <- data.frame(
    bloco = rep(c("b1", "b2"), each = 6),
    tratamento = rep(
      rep(c("A", "B"), each = 3),
      2
    ),
    F0 = 1:12,
    F1 = 2:13,
    F2 = 3:14
  )

  arq <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(d, arq)

  res <- infectionIndex:::make_long_from_workbook(arq)

  expect_equal(attr(res, "format"), "WIDE→LONG")

  expect_true(all(c(
    "block", "trat", "time", "class", "freq"
  ) %in% names(res)))

  # 12 linhas originais x 3 classes
  expect_equal(nrow(res), 36L)

  expect_setequal(
    as.character(unique(res$class)),
    c("f0", "f1", "f2")
  )

  # Cada block x trat possui tres avaliacoes.
  expect_equal(sort(unique(res$time)), 1:3)
})


test_that("workbook sem formato reconhecido gera erro informativo", {

  skip_if_not_installed("openxlsx")

  d <- data.frame(
    x = 1:5,
    y = letters[1:5]
  )

  arq <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(d, arq)

  expect_error(
    infectionIndex:::make_long_from_workbook(arq),
    "Could not construct LONG data"
  )
})


test_that("column-name normalization handles case, spaces and accents", {

  expect_equal(
    norm_txt(c(
      " Frequência ",
      "AVALIAÇÃO",
      "Bloco",
      "Tratamento"
    )),
    c(
      "frequencia",
      "avaliacao",
      "bloco",
      "tratamento"
    )
  )
})


test_that("pick_col uses exact normalized matching, not prefixes", {

  dat <- data.frame(
    temperatura = 25,
    nitrogenio = 10
  )

  expect_true(is.na(pick_col(dat, "t")))
  expect_true(is.na(pick_col(dat, "n")))
})


test_that("pick_col rejects ambiguous aliases", {

  dat <- data.frame(
    bloco = 1,
    block = 1
  )

  expect_error(
    pick_col(dat, c("block", "bloco")),
    "More than one column matches the same expected role"
  )
})



test_that("LONG data accept explicit experimental units without block", {

  dat <- data.frame(
    unit = rep(c("A1", "A2", "B1", "B2"), each = 4),
    trat = rep(c("A", "A", "B", "B"), each = 4),
    time = rep(c(1, 1, 2, 2), times = 4),
    class = rep(c("F0", "F1"), times = 8),
    freq = c(
      8,2, 6,4,
      9,1, 7,3,
      3,7, 2,8,
      4,6, 1,9
    )
  )

  f <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(dat, f, sheetName = "DIC")

  out <- make_long_from_workbook(f, sheet_prefer = "DIC")

  expect_true("unit" %in% names(out))
  expect_false("block" %in% names(out))
  expect_equal(levels(out$unit), c("A1", "A2", "B1", "B2"))
  expect_equal(attr(out, "format"), "LONG")
  expect_equal(attr(out, "source_sheet"), "DIC")
})


test_that("LONG block design remains backward compatible", {

  dat <- data.frame(
    block = rep(c(1, 1, 2, 2), each = 4),
    trat = rep(c("A", "B", "A", "B"), each = 4),
    time = rep(c(1, 1, 2, 2), times = 4),
    class = rep(c("F0", "F1"), times = 8),
    freq = rep(c(8, 2), times = 8)
  )

  f <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(dat, f, sheetName = "DBC")

  out <- make_long_from_workbook(f, sheet_prefer = "DBC")

  expect_true("block" %in% names(out))
  expect_false("unit" %in% names(out))
  expect_equal(levels(out$block), c("1", "2"))
  expect_equal(attr(out, "format"), "LONG")
  expect_equal(attr(out, "source_sheet"), "DBC")
})



test_that("LONG data preserve both block and explicit experimental unit", {

  dat <- data.frame(
    block = rep(c(1, 1, 2, 2), each = 4),
    unit = rep(c("A1", "B1", "A2", "B2"), each = 4),
    trat = rep(c("A", "B", "A", "B"), each = 4),
    time = rep(c(1, 1, 2, 2), times = 4),
    class = rep(c("F0", "F1"), times = 8),
    freq = rep(c(8, 2), times = 8)
  )

  f <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(dat, f, sheetName = "DBC_unit")

  out <- make_long_from_workbook(
    f,
    sheet_prefer = "DBC_unit"
  )

  expect_true("block" %in% names(out))
  expect_true("unit" %in% names(out))

  expect_equal(
    levels(out$block),
    c("1", "2")
  )

  expect_equal(
    levels(out$unit),
    c("A1", "A2", "B1", "B2")
  )

  expect_equal(attr(out, "format"), "LONG")
  expect_equal(attr(out, "source_sheet"), "DBC_unit")
})



test_that("WIDE data accept explicit experimental units without block", {

  dat <- data.frame(
    unit = c("A1","A1","A2","A2","B1","B1","B2","B2"),
    trat = c("A","A","A","A","B","B","B","B"),
    F0 = c(8,6,9,7,3,2,4,1),
    F1 = c(2,4,1,3,7,8,6,9)
  )

  f <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(dat, f, sheetName = "DIC_WIDE")

  out <- make_long_from_workbook(
    f,
    sheet_prefer = "DIC_WIDE"
  )

  expect_true("unit" %in% names(out))
  expect_false("block" %in% names(out))
  expect_false(".unit_id" %in% names(out))
  expect_equal(levels(out$unit), c("A1","A2","B1","B2"))

  expect_equal(
    out$time[out$unit == "A1"],
    c(1,1,2,2)
  )

  expect_equal(attr(out, "format"), "WIDE→LONG")
})


test_that("WIDE block-treatment design remains backward compatible", {

  dat <- data.frame(
    block = c(1,1,1,1,2,2,2,2),
    trat = c("A","A","B","B","A","A","B","B"),
    F0 = c(8,6,3,2,9,7,4,1),
    F1 = c(2,4,7,8,1,3,6,9)
  )

  f <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(dat, f, sheetName = "DBC_WIDE")

  out <- make_long_from_workbook(
    f,
    sheet_prefer = "DBC_WIDE"
  )

  expect_true("block" %in% names(out))
  expect_false("unit" %in% names(out))
  expect_false(".unit_id" %in% names(out))

  expect_equal(
    out$time[out$block == "1" & out$trat == "a"],
    c(1,1,2,2)
  )

  expect_equal(attr(out, "format"), "WIDE→LONG")
})


test_that("WIDE explicit unit takes priority over block-treatment grouping", {

  dat <- data.frame(
    block = rep(1, 8),
    unit = c("A1","A1","A2","A2","B1","B1","B2","B2"),
    trat = c("A","A","A","A","B","B","B","B"),
    F0 = c(8,6,9,7,3,2,4,1),
    F1 = c(2,4,1,3,7,8,6,9)
  )

  f <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(dat, f, sheetName = "BLOCK_UNIT")

  out <- make_long_from_workbook(
    f,
    sheet_prefer = "BLOCK_UNIT"
  )

  expect_true(all(c("block", "unit") %in% names(out)))
  expect_false(".unit_id" %in% names(out))

  expect_equal(
    out$time[out$unit == "A1"],
    c(1,1,2,2)
  )

  expect_equal(
    out$time[out$unit == "A2"],
    c(1,1,2,2)
  )

  expect_equal(attr(out, "format"), "WIDE→LONG")
})


test_that("LONG import supports multiple treatment factors", {
  skip_if_not_installed("openxlsx")

  d <- expand.grid(
    block = 1:3,
    cultivar = c("C1", "C2"),
    produto = c("P1", "P2"),
    time = 1:2,
    class = c("F0", "F1", "F2"),
    KEEP.OUT.ATTRS = FALSE
  )

  d$freq <- rep(
    c(8, 6, 5, 7, 5, 4, 6, 4, 3, 5, 3, 2),
    length.out = nrow(d)
  )

  f <- tempfile(fileext = ".xlsx")

  openxlsx::write.xlsx(
    d,
    f,
    sheetName = "factorial_long"
  )

  res <- make_long_from_workbook(
    f,
    factors = c("cultivar", "produto")
  )

  expect_equal(attr(res, "format"), "LONG")
  expect_equal(
    attr(res, "treatment_factors"),
    c("cultivar", "produto")
  )

  expect_equal(nrow(res), 72L)

  expect_true(
    all(c(
      "block", "cultivar", "produto",
      "time", "class", "freq"
    ) %in% names(res))
  )

  expect_false("trat" %in% names(res))

  expect_s3_class(res$cultivar, "factor")
  expect_s3_class(res$produto, "factor")

  expect_equal(levels(res$cultivar), c("c1", "c2"))
  expect_equal(levels(res$produto), c("p1", "p2"))

  expect_equal(
    sort(unique(res$time)),
    c(1, 2)
  )
})

test_that("WIDE import supports multiple treatment factors", {
  skip_if_not_installed("openxlsx")

  d <- expand.grid(
    block = 1:3,
    cultivar = c("C1", "C2"),
    produto = c("P1", "P2"),
    avaliacao = 1:2,
    KEEP.OUT.ATTRS = FALSE
  )

  d$F0 <- 10 - d$avaliacao
  d$F1 <- 2 + d$avaliacao
  d$F2 <- 1 + (d$cultivar == "C2")
  d$avaliacao <- NULL

  f <- tempfile(fileext = ".xlsx")
  openxlsx::write.xlsx(d, f, sheetName = "factorial_wide")

  res <- make_long_from_workbook(
    f,
    sheet_prefer = "factorial_wide",
    factors = c("cultivar", "produto")
  )

  expect_equal(attr(res, "format"), "WIDE→LONG")
  expect_equal(
    attr(res, "treatment_factors"),
    c("cultivar", "produto")
  )

  expect_equal(nrow(res), 72L)

  expect_true(all(c(
    "block", "cultivar", "produto",
    "time", "class", "freq"
  ) %in% names(res)))

  expect_false("trat" %in% names(res))
  expect_false("unit" %in% names(res))
  expect_false(".unit_id" %in% names(res))

  expect_equal(
    nrow(unique(res[c("block", "cultivar", "produto")])),
    12L
  )

  time_check <- unique(
    res[c("block", "cultivar", "produto", "time")]
  )

  expect_equal(nrow(time_check), 24L)

  n_times <- table(
    interaction(
      time_check$block,
      time_check$cultivar,
      time_check$produto,
      drop = TRUE
    )
  )

  expect_true(all(n_times == 2L))
  expect_equal(sort(unique(res$time)), c(1, 2))

  expect_equal(levels(res$cultivar), c("c1", "c2"))
  expect_equal(levels(res$produto), c("p1", "p2"))
})

test_that("Column-name normalization is platform independent", {
  expect_identical(
    norm_txt(c(" Frequ\u00eancia ", "AVALIA\u00c7\u00c3O",
               "Bloco", "Tratamento")),
    c("frequencia", "avaliacao", "bloco", "tratamento")
  )

  expect_identical(
    norm_txt(c("caf\u00e9", "cafe\u0301")),
    c("cafe", "cafe")
  )

  expect_identical(
    norm_txt(c(NA_character_, "", "  ", "TIME")),
    c(NA_character_, "", "", "time")
  )

  expect_identical(norm_txt(character(0)), character(0))

  d <- data.frame(x = 1)
  names(d) <- "Frequ\u00eancia"

  expect_identical(
    pick_col(d, "frequencia"),
    "Frequ\u00eancia"
  )
})

Try the infectionIndex package in your browser

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

infectionIndex documentation built on Dec. 4, 2026, 5:10 p.m.