tests/testthat/test-mod_data.R

skip_without_zip <- function() {
  testthat::skip_if(!nzchar(Sys.which("zip")), "zip is not available.")
}

expect_nacho <- function(object) {
  testthat::expect_true(S7::S7_inherits(object, NACHO:::nacho))
}

sheet_upload <- function(
  sheet,
  name = "samplesheet.csv",
  envir = parent.frame()
) {
  path <- file.path(withr::local_tempdir(.local_envir = envir), "0")
  utils::write.csv(sheet, path, row.names = FALSE)
  data.frame(
    name = name,
    size = file.size(path),
    type = "text/csv",
    datapath = path
  )
}

upload_table <- function(data_directory, sample_sheet = NULL, rcc_type = "") {
  rcc_files <- list.files(
    data_directory,
    pattern = "\\.(rcc|rcc\\.gz|zip)$",
    ignore.case = TRUE,
    full.names = TRUE
  )
  upload_directory <- withr::local_tempdir(.local_envir = parent.frame())
  upload_paths <- file.path(upload_directory, seq_along(rcc_files))
  file.copy(rcc_files, upload_paths)
  uploaded <- data.frame(
    name = basename(rcc_files),
    size = file.size(rcc_files),
    type = rcc_type,
    datapath = upload_paths
  )
  if (!is.null(sample_sheet)) {
    uploaded <- rbind(
      uploaded,
      sheet_upload(sample_sheet, envir = parent.frame())
    )
  }
  uploaded
}

read_files <- function(files) {
  withCallingHandlers(
    NACHO:::load_upload(NACHO:::list_upload(files)),
    nacho_warning_metric_unavailable = function(cnd) {
      invokeRestart("muffleWarning")
    }
  )
}

read_upload <- function(...) {
  read_files(upload_table(...))
}

test_that("loads uploaded RCC files without a sample sheet", {
  expect_nacho(read_upload("salmon_data"))
})

test_that("treats single-sample files that mention Endogenous8s as single-sample", {
  source_files <- list.files(
    geo_fixture("GSE178516")[["dir"]],
    pattern = "\\.RCC\\.gz$",
    full.names = TRUE
  )
  directory <- withr::local_tempdir()
  for (source_file in source_files) {
    lines <- readLines(source_file)
    endogenous <- grep("^Endogenous,", lines)[1]
    lines[endogenous] <- sub(
      "^Endogenous,[^,]*,",
      "Endogenous,Endogenous8s_like,",
      lines[endogenous]
    )
    writeLines(
      lines,
      file.path(directory, sub("\\.gz$", "", basename(source_file)))
    )
  }
  expect_warning(
    nacho <- read_upload(directory),
    class = "nacho_warning_n_comp_reduced"
  )
  expect_identical(nacho@rcc_type, "n1")
})

test_that("loads uploaded PlexSet RCC files", {
  expect_warning(
    nacho <- read_upload("plexset_data"),
    class = "nacho_warning_no_housekeeping"
  )
  expect_nacho(nacho)
  expect_false(nacho@settings[["housekeeping_norm"]])
  expect_type(nacho_samples(nacho)[["plexset_id"]], "character")
})

test_that("merges an uploaded sample sheet", {
  sample_sheet <- expand.grid(
    IDFILE = basename(list.files("salmon_data", pattern = "\\.RCC$")),
    plexset_id = paste0("S", seq_len(8)),
    stringsAsFactors = FALSE
  )
  sample_sheet[["group"]] <- "case"
  nacho <- read_upload("salmon_data", sample_sheet)
  expect_true("group" %in% names(nacho_samples(nacho)))
})

test_that("discards a sample sheet without IDFILE", {
  sample_sheet <- data.frame(file = "salmon_01_01.RCC", group = "case")
  expect_warning(
    nacho <- read_upload("salmon_data", sample_sheet),
    class = "nacho_warning_sample_sheet_discarded"
  )
  expect_nacho(nacho)
  expect_false("group" %in% names(nacho_samples(nacho)))
})

test_that("merges a sample sheet by IDFILE for single-sample RCC files", {
  single_directory <- geo_fixture("GSE178516")[["dir"]]
  rcc_files <- list.files(single_directory, pattern = "\\.RCC\\.gz$")

  expect_warning(
    merged <- read_upload(
      single_directory,
      data.frame(IDFILE = rcc_files, group = "case")
    ),
    class = "nacho_warning_n_comp_reduced"
  )
  expect_true("group" %in% names(nacho_samples(merged)))

  expect_warning(
    expect_warning(
      discarded <- read_upload(
        single_directory,
        data.frame(file = rcc_files, group = "case")
      ),
      class = "nacho_warning_n_comp_reduced"
    ),
    class = "nacho_warning_sample_sheet_discarded"
  )
  expect_nacho(discarded)
  expect_false("group" %in% names(nacho_samples(discarded)))
})

test_that("discards a PlexSet sample sheet without plexset_id", {
  sample_sheet <- data.frame(
    IDFILE = basename(list.files("salmon_data", pattern = "\\.RCC$")),
    group = "case"
  )
  expect_warning(
    nacho <- read_upload("salmon_data", sample_sheet),
    class = "nacho_warning_sample_sheet_discarded"
  )
  expect_nacho(nacho)
  expect_false("group" %in% names(nacho_samples(nacho)))
})

test_that("loads a zip archive whatever MIME type the browser sends", {
  skip_if(!nzchar(Sys.which("zip")), "zip is not available.")
  archive_directory <- withr::local_tempdir()
  withr::with_dir(
    test_path("salmon_data"),
    utils::zip(
      file.path(archive_directory, "salmon.zip"),
      list.files(pattern = "\\.RCC$"),
      flags = "-q"
    )
  )
  for (mime_type in c("application/zip", "application/x-zip-compressed")) {
    expect_nacho(read_upload(archive_directory, rcc_type = mime_type))
  }
})

test_that("matches file extensions regardless of case", {
  lower_directory <- withr::local_tempdir()
  rcc_files <- list.files("salmon_data", pattern = "\\.RCC$")
  file.copy(
    file.path("salmon_data", rcc_files),
    file.path(lower_directory, sub("\\.RCC$", ".rcc", rcc_files))
  )
  sample_sheet <- expand.grid(
    IDFILE = sub("\\.RCC$", ".rcc", rcc_files),
    plexset_id = paste0("S", seq_len(8)),
    stringsAsFactors = FALSE
  )
  sample_sheet[["group"]] <- "case"
  nacho <- read_upload(lower_directory, sample_sheet)
  expect_true("group" %in% names(nacho_samples(nacho)))
})

test_that("keeps gzipped RCC files next to a sample sheet", {
  gz_directory <- withr::local_tempdir()
  for (rcc in list.files("salmon_data", pattern = "\\.RCC$")) {
    connection <- gzfile(file.path(gz_directory, paste0(rcc, ".gz")), "w")
    writeLines(readLines(file.path("salmon_data", rcc)), connection)
    close(connection)
  }
  sample_sheet <- expand.grid(
    IDFILE = list.files(gz_directory),
    plexset_id = paste0("S", seq_len(8)),
    stringsAsFactors = FALSE
  )
  sample_sheet[["group"]] <- "case"
  nacho <- read_upload(gz_directory, sample_sheet)
  expect_true("group" %in% names(nacho_samples(nacho)))
})

test_that("an upload without RCC files is refused", {
  directory <- withr::local_tempdir()
  path <- file.path(directory, "notes.csv")
  writeLines("IDFILE\nx.RCC", path)
  files <- data.frame(
    name = "notes.csv",
    size = file.size(path),
    type = "text/csv",
    datapath = path
  )
  expect_error(
    read_files(files),
    class = "nacho_error_bad_upload"
  )
})

test_that("a discarded sample sheet is announced", {
  sample_sheet <- data.frame(file = "salmon_01_01.RCC", group = "case")
  expect_warning(
    nacho <- read_files(upload_table("salmon_data", sample_sheet)),
    "IDFILE",
    class = "nacho_warning_sample_sheet_discarded"
  )
  expect_false("group" %in% names(nacho_samples(nacho)))
})

test_that("a rejected upload keeps the previous data", {
  messages <- character()
  local_no_mirai()
  local_mocked_bindings(
    notify_user = function(message, type) {
      messages <<- c(messages, type)
    }
  )
  directory <- withr::local_tempdir()
  path <- file.path(directory, "notes.csv")
  writeLines("IDFILE\nx.RCC", path)
  bad <- data.frame(
    name = "notes.csv",
    size = file.size(path),
    type = "text/csv",
    datapath = path
  )
  shiny::testServer(
    NACHO:::mod_data_server,
    args = list(initial = GSE74821),
    {
      session$setInputs(files = bad, import = 1)
      expect_identical(session$returned(), GSE74821)
    }
  )
  expect_identical(messages, "error")
})

test_that("the module loads an upload", {
  local_no_mirai()
  shiny::testServer(NACHO:::mod_data_server, {
    expect_null(session$returned())
    session$setInputs(files = upload_table("salmon_data"), import = 1)
    wait_for_task(task, session)
    expect_true(S7::S7_inherits(session$returned(), NACHO:::nacho))
  })
})

test_that("a corrupt zip shows an error and keeps the previous data", {
  types <- character()
  local_no_mirai()
  local_mocked_bindings(
    notify_user = function(message, type) types <<- c(types, type)
  )
  directory <- withr::local_tempdir()
  path <- file.path(directory, "0")
  writeLines("this is not a zip", path)
  bad <- data.frame(
    name = "broken.zip",
    size = file.size(path),
    type = "application/zip",
    datapath = path
  )
  shiny::testServer(
    NACHO:::mod_data_server,
    args = list(initial = GSE74821),
    {
      session$setInputs(files = bad, import = 1)
      expect_identical(session$returned(), GSE74821)
    }
  )
  expect_identical(types, "error")
})

test_that("a zip made from a folder is read", {
  skip_without_zip()
  source_files <- list.files(
    test_path("salmon_data"),
    pattern = "\\.rcc$",
    ignore.case = TRUE,
    full.names = TRUE
  )
  skip_if(length(source_files) == 0)
  staging <- withr::local_tempdir()
  dir.create(file.path(staging, "run1"))
  file.copy(source_files, file.path(staging, "run1"))
  archive <- file.path(withr::local_tempdir(), "0")
  withr::with_dir(
    staging,
    utils::zip(archive, "run1", flags = "-rq")
  )
  archive <- paste0(archive, ".zip")
  zipped <- data.frame(
    name = "run1.zip",
    size = file.size(archive),
    type = "application/zip",
    datapath = archive
  )
  expect_nacho(
    read_files(zipped)
  )
})

test_that("extra sample sheets are announced", {
  sample_sheet <- data.frame(
    IDFILE = "salmon_01_01.RCC",
    plexset_id = "S1",
    group = "case"
  )
  uploads <- upload_table("salmon_data", sample_sheet)
  second <- uploads[uploads$name == "samplesheet.csv", ]
  second$name <- "second.csv"
  second$datapath <- file.path(dirname(second$datapath), "second_sheet")
  file.copy(
    uploads$datapath[uploads$name == "samplesheet.csv"],
    second$datapath
  )
  expect_warning(
    withCallingHandlers(
      read_files(rbind(uploads, second)),
      nacho_warning_metric_unavailable = function(cnd) {
        invokeRestart("muffleWarning")
      },
      nacho_warning_n_comp_reduced = function(cnd) {
        invokeRestart("muffleWarning")
      }
    ),
    "second.csv",
    class = "nacho_warning_sample_sheet_discarded"
  )
})

zip_upload <- function(files, folder = NULL, name = "run.zip") {
  skip_without_zip()
  staging <- withr::local_tempdir(.local_envir = parent.frame())
  target <- if (is.null(folder)) staging else file.path(staging, folder)
  dir.create(target, showWarnings = FALSE)
  file.copy(files, target)
  archive <- file.path(withr::local_tempdir(.local_envir = parent.frame()), "0")
  withr::with_dir(
    staging,
    utils::zip(archive, list.files(staging), flags = "-rq")
  )
  data.frame(
    name = name,
    size = file.size(paste0(archive, ".zip")),
    type = "application/zip",
    datapath = paste0(archive, ".zip")
  )
}

test_that("an empty zip is refused as an upload without RCC files", {
  skip_without_zip()
  staging <- withr::local_tempdir()
  dir.create(file.path(staging, "empty"))
  archive <- file.path(withr::local_tempdir(), "0")
  withr::with_dir(staging, utils::zip(archive, "empty", flags = "-rq"))
  empty <- data.frame(
    name = "empty.zip",
    size = 1,
    type = "application/zip",
    datapath = paste0(archive, ".zip")
  )
  expect_error(
    read_files(empty),
    "holds no RCC file",
    class = "nacho_error_bad_upload"
  )
})

test_that("a corrupt zip names the problem", {
  messages <- character()
  local_no_mirai()
  local_mocked_bindings(
    notify_user = function(message, type) messages <<- c(messages, message)
  )
  path <- file.path(withr::local_tempdir(), "0")
  writeLines("this is not a zip", path)
  bad <- data.frame(
    name = "broken.zip",
    size = file.size(path),
    type = "application/zip",
    datapath = path
  )
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(files = bad, import = 1)
  })
  expect_match(messages, "not a valid zip archive")
})

test_that("a sample sheet with bare file names matches files in a zip", {
  rcc <- list.files(
    test_path("salmon_data"),
    pattern = "\\.rcc$",
    ignore.case = TRUE,
    full.names = TRUE
  )
  sheet <- data.frame(
    IDFILE = rep(basename(rcc), each = 8),
    plexset_id = paste0("S", 1:8),
    group = "case"
  )
  sheet_path <- file.path(withr::local_tempdir(), "sheet")
  utils::write.csv(sheet, sheet_path, row.names = FALSE)
  sheet_row <- data.frame(
    name = "sheet.csv",
    size = file.size(sheet_path),
    type = "text/csv",
    datapath = sheet_path
  )
  for (folder in list(NULL, "run1")) {
    fresh <- tempfile()
    file.copy(sheet_path, fresh)
    sheet_row$datapath <- fresh
    nacho <- read_files(rbind(zip_upload(rcc, folder), sheet_row))
    expect_true("group" %in% names(nacho_samples(nacho)))
  }
})

test_that("a sample sheet that matches no file is announced", {
  rcc <- list.files(
    test_path("salmon_data"),
    pattern = "\\.rcc$",
    ignore.case = TRUE,
    full.names = TRUE
  )
  sheet <- data.frame(IDFILE = "other.RCC", plexset_id = "S1", group = "case")
  sheet_path <- file.path(withr::local_tempdir(), "sheet")
  utils::write.csv(sheet, sheet_path, row.names = FALSE)
  sheet_row <- data.frame(
    name = "sheet.csv",
    size = file.size(sheet_path),
    type = "text/csv",
    datapath = sheet_path
  )
  expect_warning(
    read_files(rbind(zip_upload(rcc), sheet_row)),
    "matches no",
    class = "nacho_warning_sample_sheet_discarded"
  )
})

test_that("only sample information reaches the samples table", {
  nacho <- read_upload("salmon_data")
  expect_false(any(
    c("datapath", "type", "name") %in% names(nacho_samples(nacho))
  ))
  sample_sheet <- data.frame(
    IDFILE = rep(list.files("salmon_data", pattern = "\\.RCC$"), each = 8),
    plexset_id = paste0("S", 1:8),
    group = "case"
  )
  with_sheet <- read_upload("salmon_data", sample_sheet)
  expect_true("group" %in% names(nacho_samples(with_sheet)))
  expect_false(any(
    c("datapath", "type", "name") %in% names(nacho_samples(with_sheet))
  ))
})

test_that("the example button loads GSE74821", {
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(example = 1)
    expect_identical(session$returned(), GSE74821)
  })
})

test_that("messages reach the user as toasts", {
  shown <- NULL
  local_mocked_bindings(
    show_toast = function(toast, ...) shown <<- toast,
    .package = "bslib"
  )
  NACHO:::notify_user("Hello", "warning")
  expect_s3_class(shown, "bslib_toast")
})

test_that("an error toast stays open until it is closed", {
  shown <- list()
  local_mocked_bindings(
    show_toast = function(toast, ...) shown[[length(shown) + 1]] <<- toast,
    .package = "bslib"
  )
  NACHO:::notify_user("Broken", "error")
  NACHO:::notify_user("Fine", "message")
  expect_false(unclass(shown[[1]])$autohide)
  expect_true(unclass(shown[[2]])$autohide)
})

test_that("a discarded sample sheet and other load warnings become warning toasts", {
  types <- character()
  local_no_mirai()
  local_mocked_bindings(
    notify_user = function(message, type) types <<- c(types, type)
  )
  sheet <- data.frame(file = "salmon_01_01.RCC", group = "case")
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(files = upload_table("salmon_data", sheet), import = 1)
    wait_for_task(task, session)
    expect_true(S7::S7_inherits(session$returned(), NACHO:::nacho))
  })
  expect_true("warning" %in% types)
  expect_false("error" %in% types)
})

test_that("any load warning of the package is shown as a warning toast", {
  messages <- character()
  local_no_mirai()
  local_mocked_bindings(
    notify_user = function(message, type) messages <<- c(messages, message),
    list_upload = function(files) list(),
    load_upload = function(listing) {
      NACHO:::nacho_warn("Too few genes.", class = "n_comp_reduced")
      GSE74821
    }
  )
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(
      files = data.frame(name = "a", size = 1, type = "x", datapath = "a"),
      import = 1
    )
    wait_for_task(task, session)
    expect_identical(session$returned(), GSE74821)
  })
  expect_identical(messages, "Too few genes.")
})

test_that("importing without a file asks the user to choose files", {
  messages <- character()
  local_mocked_bindings(
    notify_user = function(message, type) messages <<- c(messages, message)
  )
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(import = 1)
    expect_null(session$returned())
  })
  expect_match(messages, "Choose the RCC files")
})

test_that("zip entries that leave the upload folder are refused", {
  for (entry in c(
    "../evil.RCC",
    "a/../../evil.RCC",
    "/tmp/evil.RCC",
    "C:/evil.RCC",
    "a\\..\\..\\evil.RCC"
  )) {
    expect_error(
      NACHO:::check_zip_entries(c("ok.RCC", entry), "upload.zip"),
      class = "nacho_error_bad_upload",
      info = entry
    )
  }
  expect_no_error(
    NACHO:::check_zip_entries(c("run/a.RCC", "b..c.RCC", "run/"), "upload.zip")
  )
})

test_that("an upload name cannot leave the upload folder", {
  directory <- withr::local_tempdir()
  datapath <- file.path(directory, "0")
  writeLines("x", datapath)
  upload <- NACHO:::expand_upload("../../escaped.RCC", datapath, "")
  expect_identical(
    normalizePath(dirname(upload$datapath)),
    normalizePath(directory)
  )
  expect_identical(upload$name, "escaped.RCC")
  expect_true(file.exists(file.path(directory, "escaped.RCC")))
})

test_that("an import returns the object and writes its last stage", {
  progress <- withr::local_tempfile()
  imported <- NACHO:::import_uploads(
    NACHO:::list_upload(upload_table("salmon_data")),
    progress
  )
  expect_nacho(imported$object)
  expect_null(imported$error)
  expect_identical(
    readLines(progress),
    "Computing quality-control metrics and normalising"
  )
})

test_that("an import returns its warnings as text", {
  local_mocked_bindings(
    load_upload = function(listing) {
      NACHO:::nacho_warn("Metric missing.", class = "metric_unavailable")
      NACHO:::nacho_warn("Too few genes.", class = "n_comp_reduced")
      warning("Not from NACHO.")
      GSE74821
    }
  )
  expect_warning(
    imported <- NACHO:::import_uploads(list()),
    "Not from NACHO."
  )
  expect_identical(imported$object, GSE74821)
  expect_identical(imported$warnings, "Too few genes.")
  expect_null(imported$error)
})

test_that("an import returns its error as text", {
  local_mocked_bindings(
    load_upload = function(listing) {
      NACHO:::nacho_warn("Too few genes.", class = "n_comp_reduced")
      NACHO:::nacho_abort("No RCC file.", class = "bad_upload")
    }
  )
  imported <- NACHO:::import_uploads(list())
  expect_null(imported$object)
  expect_identical(imported$warnings, "Too few genes.")
  expect_identical(imported$error, "No RCC file.")
  local_mocked_bindings(load_upload = function(listing) stop("disk full"))
  expect_identical(
    NACHO:::import_uploads(list())$error,
    "The upload could not be read: disk full"
  )
})

test_that("the upload panel says when the import blocks the app", {
  ui <- function() htmltools::renderTags(NACHO:::mod_data_ui("data"))$html
  local_mocked_bindings(has_package = function(package) FALSE)
  expect_match(ui(), "install mirai to import in the background", fixed = TRUE)
  local_mocked_bindings(has_package = function(package) TRUE)
  expect_no_match(ui(), "install mirai", fixed = TRUE)
})

test_that("a background import shows its stage and then loads the object", {
  skip_on_cran()
  skip_if_not_installed("mirai")
  local_mocked_bindings(
    has_package = function(package) TRUE,
    run_in_daemon = function(fun, args, compute) {
      progress_file <- args[[2]]
      mirai::mirai(
        {
          writeLines("Reading 2 RCC files", progress_file)
          Sys.sleep(1)
          list(object = "loaded", warnings = character(), error = NULL)
        },
        progress_file = progress_file,
        .compute = compute
      )
    }
  )
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(files = upload_table("salmon_data"), import = 1)
    wait_until(
      session,
      function() {
        session$elapse(250)
        nzchar(output$progress)
      },
      tries = 100
    )
    expect_identical(output$progress, "Reading 2 RCC files")
    wait_for_task(task, session, tries = 100)
    expect_identical(session$returned(), "loaded")
    expect_identical(output$progress, "")
    session$close()
    expect_false(file.exists(progress_file))
  })
})

test_that("the background import uses the installed NACHO", {
  skip_on_cran()
  skip_if_not_installed("mirai")
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(files = upload_table("salmon_data"), import = 1)
    wait_for_task(task, session, tries = 300)
    expect_nacho(session$returned())
  })
})

test_that("the example data loaded during an import is kept", {
  local_no_mirai()
  local_mocked_bindings(
    run_in_daemon = function(fun, args, compute) {
      promises::promise(function(resolve, reject) {
        later::later(
          function() {
            resolve(list(object = "late", warnings = character(), error = NULL))
          },
          0.5
        )
      })
    }
  )
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(files = upload_table("salmon_data"), import = 1)
    session$setInputs(example = 1)
    wait_for_task(task, session)
    expect_identical(session$returned(), GSE74821)
  })
})

test_that("choosing files starts the import daemon", {
  skip_if_not_installed("mirai")
  started <- 0
  local_mocked_bindings(has_package = function(package) TRUE)
  local_mocked_bindings(
    daemons = function(n, ...) {
      if (n > 0) {
        started <<- started + 1
      }
      invisible(NULL)
    },
    mirai = function(...) invisible(NULL),
    .package = "mirai"
  )
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(files = upload_table("salmon_data"))
    expect_identical(started, 1)
  })
})

salmon_files <- function() {
  list.files(
    testthat::test_path("salmon_data"),
    pattern = "\\.RCC$",
    ignore.case = TRUE
  )
}


test_that("list_upload() lists the RCC files with their PlexSet flag", {
  listing <- NACHO:::list_upload(upload_table("salmon_data"))
  expect_identical(listing$rcc$name, salmon_files())
  expect_identical(listing$rcc$from, rep("", length(salmon_files())))
  expect_true(all(listing$rcc$plexset))
  expect_true(all(file.exists(listing$rcc$datapath)))
  expect_null(listing$sheet)
  expect_identical(listing$ignored_sheets, character())
})

test_that("list_upload() names the zip each file came from", {
  rcc <- list.files(test_path("salmon_data"), full.names = TRUE)
  listing <- NACHO:::list_upload(zip_upload(rcc, "run1"))
  expect_identical(basename(listing$rcc$name), basename(rcc))
  expect_identical(listing$rcc$from, rep("run.zip", length(rcc)))
})

test_that("list_upload() reads the first sample sheet and names the others", {
  sheet <- data.frame(IDFILE = "salmon_01_01.RCC", plexset_id = "S1")
  files <- rbind(
    upload_table("salmon_data"),
    sheet_upload(sheet),
    sheet_upload(sheet, "second.csv")
  )
  listing <- NACHO:::list_upload(files)
  expect_identical(listing$sheet$IDFILE, "salmon_01_01.RCC")
  expect_identical(listing$ignored_sheets, "second.csv")
})

test_that("list_upload() refuses an upload without RCC files", {
  expect_error(
    NACHO:::list_upload(sheet_upload(data.frame(IDFILE = "x.RCC"))),
    "holds no RCC file",
    class = "nacho_error_bad_upload"
  )
})

test_that("the preview counts PlexSet wells found in the sample sheet", {
  files <- salmon_files()
  sheet <- data.frame(
    IDFILE = c(rep(files[1], 8), rep(files[2], 3), "other.RCC"),
    plexset_id = c(paste0("S", 1:8), paste0("S", 1:3), "S1")
  )
  preview <- NACHO:::upload_preview(
    NACHO:::list_upload(rbind(upload_table("salmon_data"), sheet_upload(sheet)))
  )
  expect_identical(
    preview$table[["In sample sheet"]],
    c("8 of 8", "3 of 8", rep("0 of 8", length(files) - 2))
  )
  expect_identical(preview$table[["PlexSet"]], rep("yes", length(files)))
  expect_identical(
    preview$summary,
    sprintf(
      "%d RCC files, %d PlexSet, %d not in the sample sheet, 1 sample sheet row with no file.",
      length(files),
      length(files),
      length(files) - 2
    )
  )
})

test_that("the preview matches single-sample files by file name", {
  directory <- withr::local_tempdir()
  sources <- list.files(
    geo_fixture("GSE178516")[["dir"]],
    pattern = "\\.RCC\\.gz$",
    full.names = TRUE
  )
  file.copy(sources, directory)
  sheet <- data.frame(IDFILE = basename(sources[1]))
  preview <- NACHO:::upload_preview(
    NACHO:::list_upload(rbind(upload_table(directory), sheet_upload(sheet)))
  )
  expect_identical(
    preview$table[["In sample sheet"]],
    c("yes", rep("no", length(sources) - 1))
  )
  expect_identical(preview$table[["PlexSet"]], rep("no", length(sources)))
  expect_identical(
    preview$summary,
    sprintf(
      "%d RCC files, 0 PlexSet, %d not in the sample sheet.",
      length(sources),
      length(sources) - 1
    )
  )
})

test_that("the preview says when there is no usable sample sheet", {
  preview <- NACHO:::upload_preview(
    NACHO:::list_upload(upload_table("salmon_data"))
  )
  expect_false("In sample sheet" %in% names(preview$table))
  expect_match(preview$summary, "No sample sheet.", fixed = TRUE)
  without_id <- NACHO:::upload_preview(
    NACHO:::list_upload(rbind(
      upload_table("salmon_data"),
      sheet_upload(data.frame(file = "salmon_01_01.RCC")),
      sheet_upload(data.frame(file = "x"), "second.csv")
    ))
  )
  expect_false("In sample sheet" %in% names(without_id$table))
  expect_match(
    without_id$summary,
    "The sample sheet was discarded, because it has no \"IDFILE\" and \"plexset_id\" columns.",
    fixed = TRUE
  )
  expect_match(
    without_id$summary,
    "Only the first sample sheet is used, so 'second.csv' was ignored.",
    fixed = TRUE
  )
})

test_that("the preview says when the sample sheet matches no file", {
  preview <- NACHO:::upload_preview(
    NACHO:::list_upload(rbind(
      upload_table("salmon_data"),
      sheet_upload(data.frame(IDFILE = "other.RCC", plexset_id = "S1"))
    ))
  )
  expect_false("In sample sheet" %in% names(preview$table))
  expect_match(
    preview$summary,
    "The sample sheet was discarded, because it matches no uploaded RCC file.",
    fixed = TRUE
  )
})

test_that("a sample sheet with numbers in IDFILE is read as file names", {
  directory <- withr::local_tempdir()
  file.copy(
    list.files(
      geo_fixture("GSE178516")[["dir"]],
      pattern = "\\.RCC\\.gz$",
      full.names = TRUE
    )[1],
    directory
  )
  listing <- NACHO:::list_upload(rbind(
    upload_table(directory),
    sheet_upload(data.frame(IDFILE = 1:2))
  ))
  expect_type(listing$sheet$IDFILE, "character")
  preview <- NACHO:::upload_preview(listing)
  expect_match(
    preview$summary,
    "The sample sheet was discarded, because it matches no uploaded RCC file.",
    fixed = TRUE
  )
})

test_that("sample sheet columns named like the listing columns are kept", {
  sheet <- data.frame(
    IDFILE = rep(salmon_files(), each = 8),
    plexset_id = paste0("S", 1:8),
    plexset = "panel A",
    from = "site 1"
  )
  nacho <- read_upload("salmon_data", sheet)
  expect_identical(unique(nacho_samples(nacho)[["plexset"]]), "panel A")
  expect_identical(unique(nacho_samples(nacho)[["from"]]), "site 1")
})

test_that("choosing files shows the preview", {
  local_no_mirai()
  shiny::testServer(NACHO:::mod_data_server, {
    expect_identical(output$preview_summary, "")
    session$setInputs(files = upload_table("salmon_data"))
    expect_match(output$preview_summary, "RCC files, ", fixed = TRUE)
    expect_match(output$preview_table, salmon_files()[1], fixed = TRUE)
  })
})

test_that("an upload that cannot be listed shows its error in the preview", {
  local_no_mirai()
  messages <- character()
  local_mocked_bindings(
    notify_user = function(message, type) messages <<- c(messages, message)
  )
  shiny::testServer(NACHO:::mod_data_server, {
    session$setInputs(files = sheet_upload(data.frame(IDFILE = "x.RCC")))
    expect_match(output$preview_summary, "holds no RCC file", fixed = TRUE)
    expect_identical(messages, character())
  })
})

Try the NACHO package in your browser

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

NACHO documentation built on Nov. 2, 2026, 5:09 p.m.