Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tests/testthat/test-data_helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## Tests for internal data helper functions

# ── .normalize_results_root ──────────────────────────────────────────────────

test_that(".normalize_results_root returns NULL for invalid inputs", {
expect_null(.normalize_results_root(NULL))
expect_null(.normalize_results_root(NA))
expect_null(.normalize_results_root(""))
expect_null(.normalize_results_root(character(0)))
# Multiple values
expect_null(.normalize_results_root(c("/a", "/b")))
})

test_that(".normalize_results_root normalizes a valid path", {
tmp <- tempdir()
result <- .normalize_results_root(tmp)
expect_type(result, "character")
expect_equal(nchar(result) > 0, TRUE)
# Should be an absolute path
expect_true(startsWith(result, "/") || grepl("^[A-Z]:", result))
})

# ── .read_parquet_safe ───────────────────────────────────────────────────────

test_that(".read_parquet_safe returns empty tibble for missing file", {
result <- suppressMessages(
.read_parquet_safe("/nonexistent/path.parquet", verbose = FALSE)
)
expect_s3_class(result, "tbl_df")
expect_equal(nrow(result), 0)
})

test_that(".read_parquet_safe returns empty tibble for NULL path", {
result <- suppressMessages(
.read_parquet_safe(NULL, verbose = FALSE)
)
expect_s3_class(result, "tbl_df")
expect_equal(nrow(result), 0)
})

test_that(".read_parquet_safe reads a valid parquet file", {
extdata <- system.file("extdata", package = "amRshiny")
perf_files <- list.files(
extdata,
pattern = "_ML_perf\\.parquet$",
recursive = TRUE, full.names = TRUE
)
skip_if(length(perf_files) == 0, "No demo parquet files found")

result <- .read_parquet_safe(perf_files[1], verbose = FALSE)
expect_s3_class(result, "tbl_df")
expect_gt(nrow(result), 0)
})
135 changes: 135 additions & 0 deletions tests/testthat/test-data_loading.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
## Tests for data loading functions: loadMLResults, loadTopFeat,
## listAmRmlSpeciesFolders, get_metadata_path, loadDrugClassMap

# ── listAmRmlSpeciesFolders ──────────────────────────────────────────────────

test_that("listAmRmlSpeciesFolders returns empty for NULL/invalid root", {
expect_equal(listAmRmlSpeciesFolders(NULL), character(0))
expect_equal(listAmRmlSpeciesFolders(""), character(0))
expect_equal(
listAmRmlSpeciesFolders("/nonexistent/path"),
character(0)
)
})

test_that("listAmRmlSpeciesFolders discovers species dirs in extdata", {
extdata <- system.file("extdata", package = "amRshiny")
skip_if(!nzchar(extdata), "No extdata directory found")

result <- listAmRmlSpeciesFolders(extdata, verbose = FALSE)
expect_type(result, "character")
expect_true(length(result) > 0)
expect_true(all(nzchar(names(result))))
})

test_that("listAmRmlSpeciesFolders ignores dirs without perf parquets", {
tmp <- tempfile("test_species_")
dir.create(tmp)
on.exit(unlink(tmp, recursive = TRUE))

empty_dir <- file.path(tmp, "Empty_species")
dir.create(empty_dir)

result <- listAmRmlSpeciesFolders(tmp, verbose = FALSE)
expect_equal(length(result), 0)
})

# ── loadMLResults ────────────────────────────────────────────────────────────

test_that("loadMLResults returns empty tibble when results_root given but no species_dirs", {
tmp <- tempdir()
result <- suppressMessages(
loadMLResults(
results_root = tmp,
species_dirs = NULL,
verbose = FALSE
)
)
expect_s3_class(result, "tbl_df")
expect_equal(nrow(result), 0)
})

test_that("loadMLResults loads demo data in fallback mode", {
result <- suppressMessages(loadMLResults(verbose = FALSE))
expect_s3_class(result, "tbl_df")
expect_gt(nrow(result), 0)
expect_true("species" %in% names(result))
})

test_that("loadMLResults loads from specific species_dirs", {
extdata <- system.file("extdata", package = "amRshiny")
skip_if(!nzchar(extdata), "No extdata directory found")

folders <- listAmRmlSpeciesFolders(extdata, verbose = FALSE)
skip_if(
length(folders) == 0,
"No species dirs with baseline perf in extdata"
)

result <- suppressMessages(
loadMLResults(
results_root = extdata,
species_dirs = folders[1],
verbose = FALSE
)
)
expect_s3_class(result, "tbl_df")
expect_gt(nrow(result), 0)
expect_true("species_label" %in% names(result))
})

# ── loadTopFeat ──────────────────────────────────────────────────────────────

test_that("loadTopFeat returns empty tibble when results_root given but no species_dirs", {
tmp <- tempdir()
result <- suppressMessages(
loadTopFeat(
results_root = tmp,
species_dirs = NULL,
verbose = FALSE
)
)
expect_s3_class(result, "tbl_df")
expect_equal(nrow(result), 0)
})

test_that("loadTopFeat loads demo data in fallback mode", {
result <- suppressMessages(loadTopFeat(verbose = FALSE))
expect_s3_class(result, "tbl_df")
expect_gt(nrow(result), 0)
expect_true("Variable" %in% names(result))
expect_true("Importance" %in% names(result))
})

# ── get_metadata_path ────────────────────────────────────────────────────────

test_that("get_metadata_path returns NULL for nonexistent species code", {
result <- get_metadata_path("Zzz")
expect_null(result)
})

test_that("get_metadata_path finds demo metadata parquet", {
extdata <- system.file("extdata", package = "amRshiny")
skip_if(!nzchar(extdata), "No extdata directory found")

result <- get_metadata_path("Sfl")
if (!is.null(result)) {
expect_true(file.exists(result))
expect_true(grepl("Sfl_metadata\\.parquet$", result))
}
})

# ── loadDrugClassMap ─────────────────────────────────────────────────────────

test_that("loadDrugClassMap returns a tibble with expected columns", {
result <- suppressMessages(loadDrugClassMap())
expect_s3_class(result, "tbl_df")
expect_true("drug.antibiotic_name" %in% names(result))
expect_true("drug_class" %in% names(result))
expect_gt(nrow(result), 0)
})

test_that("loadDrugClassMap returns distinct rows", {
result <- suppressMessages(loadDrugClassMap())
expect_equal(nrow(result), nrow(dplyr::distinct(result)))
})
78 changes: 78 additions & 0 deletions tests/testthat/test-getHoldoutsDrugChoices.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
## Tests for getHoldoutsDrugChoices

test_that("getHoldoutsDrugChoices returns empty for NULL input", {
expect_equal(getHoldoutsDrugChoices(NULL), character(0))
})

test_that("getHoldoutsDrugChoices returns empty for empty data frame", {
expect_equal(getHoldoutsDrugChoices(data.frame()), character(0))
})

test_that("getHoldoutsDrugChoices returns empty for zero-row tibble", {
df <- tibble::tibble(
strat_label = character(0),
drug_or_class = character(0),
species = character(0)
)
expect_equal(getHoldoutsDrugChoices(df), character(0))
})

test_that("getHoldoutsDrugChoices filters to stratified rows only", {
df <- tibble::tibble(
strat_label = c(NA_character_, "", "country", "year"),
drug_or_class = c(
"amoxicillin", "ampicillin",
"ciprofloxacin", "tetracycline"
),
species = c("Sau", "Sau", "Sau", "Sau")
)
result <- getHoldoutsDrugChoices(df)
expect_true("ciprofloxacin" %in% result)
expect_true("tetracycline" %in% result)
expect_false("amoxicillin" %in% result)
expect_false("ampicillin" %in% result)
})

test_that("getHoldoutsDrugChoices returns sorted unique values", {
df <- tibble::tibble(
strat_label = c("country", "country", "year"),
drug_or_class = c("tetracycline", "ampicillin", "tetracycline"),
species = c("Sau", "Sau", "Sau")
)
result <- getHoldoutsDrugChoices(df)
expect_equal(result, c("ampicillin", "tetracycline"))
})

test_that("getHoldoutsDrugChoices filters by bug when provided", {
df <- tibble::tibble(
strat_label = c("country", "country", "country"),
drug_or_class = c(
"ampicillin", "ciprofloxacin", "tetracycline"
),
species = c("Sau", "Kpn", "Sau")
)
result <- getHoldoutsDrugChoices(df, bug = "Sau")
expect_true("ampicillin" %in% result)
expect_true("tetracycline" %in% result)
expect_false("ciprofloxacin" %in% result)
})

test_that("getHoldoutsDrugChoices normalizes species for comparison", {
df <- tibble::tibble(
strat_label = c("country", "country"),
drug_or_class = c("ampicillin", "ciprofloxacin"),
species = c("Esp.", "Esp")
)
result <- getHoldoutsDrugChoices(df, bug = "Esp.")
expect_length(result, 2)
})

test_that("getHoldoutsDrugChoices excludes NA and blank drug_or_class", {
df <- tibble::tibble(
strat_label = c("country", "country", "country"),
drug_or_class = c("ampicillin", NA_character_, " "),
species = c("Sau", "Sau", "Sau")
)
result <- getHoldoutsDrugChoices(df)
expect_equal(result, "ampicillin")
})
76 changes: 76 additions & 0 deletions tests/testthat/test-makeFeatureImportTable.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
## Tests for makeFeatureImportTable

test_that("makeFeatureImportTable returns datatable for NULL input", {
result <- makeFeatureImportTable(NULL)
expect_s3_class(result, "datatables")
})

test_that("makeFeatureImportTable returns datatable for empty data", {
df <- tibble::tibble()
result <- makeFeatureImportTable(df)
expect_s3_class(result, "datatables")
})

test_that("makeFeatureImportTable formats numeric columns to scientific notation", {
df <- tibble::tibble(
species = c("Sau", "Kpn"),
drug_or_class = c("ampicillin", "tetracycline"),
Variable = c("gene1", "gene2"),
Importance = c(0.00123, 0.456)
)

result <- makeFeatureImportTable(df)
expect_s3_class(result, "datatables")
})

test_that("makeFeatureImportTable reorders columns by priority", {
df <- tibble::tibble(
Variable = c("gene1"),
Importance = c(0.5),
species = c("Sau"),
drug_or_class = c("ampicillin"),
feature_type = c("genes"),
feature_subtype = c("binary")
)

result <- makeFeatureImportTable(df)
expect_s3_class(result, "datatables")
})

test_that("makeFeatureImportTable adds hyperlinks for accession column", {
df <- tibble::tibble(
species = c("Sau"),
drug_or_class = c("ampicillin"),
accession = c("WP_001234567.1"),
Importance = c(0.5)
)

result <- makeFeatureImportTable(df)
expect_s3_class(result, "datatables")
expect_true(any(grepl("href", unlist(result$x$data))))
})

test_that("makeFeatureImportTable adds hyperlinks for COG_name column", {
df <- tibble::tibble(
species = c("Sau"),
drug_or_class = c("ampicillin"),
COG_name = c("COG0001"),
Importance = c(0.5)
)

result <- makeFeatureImportTable(df)
expect_s3_class(result, "datatables")
expect_true(any(grepl("href", unlist(result$x$data))))
})

test_that("makeFeatureImportTable replaces non-ARG in ARG_name column", {
df <- tibble::tibble(
species = c("Sau", "Sau"),
drug_or_class = c("ampicillin", "tetracycline"),
ARG_name = c("non-ARG", "blaOXA-1"),
Importance = c(0.5, 0.8)
)

result <- makeFeatureImportTable(df)
expect_s3_class(result, "datatables")
})
41 changes: 41 additions & 0 deletions tests/testthat/test-makeQuickStats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Tests for makeQuickStats

test_that("makeQuickStats returns a tagList with expected structure", {
df <- tibble::tibble(
genome_drug.genome_id = c("G1", "G1", "G2", "G3"),
genome_drug.antibiotic = c(
"ampicillin", "tetracycline",
"ampicillin", "ciprofloxacin"
),
genome_drug.resistant_phenotype = c(
"Resistant", "Susceptible",
"Resistant", "Susceptible"
),
drug_class = c(
"penicillins", "tetracyclines",
"penicillins", "fluoroquinolones"
),
genome.isolation_country = c("USA", "UK", "USA", "Germany"),
species = c(
"staphylococcus aureus", "staphylococcus aureus",
"staphylococcus aureus", "staphylococcus aureus"
)
)

result <- makeQuickStats(df)
expect_s3_class(result, "shiny.tag.list")
})

test_that("makeQuickStats handles single-row data", {
df <- tibble::tibble(
genome_drug.genome_id = "G1",
genome_drug.antibiotic = "ampicillin",
genome_drug.resistant_phenotype = "Resistant",
drug_class = "penicillins",
genome.isolation_country = "USA",
species = "staphylococcus aureus"
)

result <- makeQuickStats(df)
expect_s3_class(result, "shiny.tag.list")
})
Loading
Loading