# test-config.R - Test MTaP configuration context("Configuration functions") # Create test helper function create_test_files <- function() { # Create temporary OTU table otu_temp <- tempfile(fileext = ".csv") otu_data <- data.frame( OTU_ID = c("OTU1", "OTU2", "OTU3"), Sample1 = c(10, 5, 3), Sample2 = c(15, 7, 2) ) write.csv(otu_data, otu_temp, row.names = FALSE) # Create temporary taxonomy table tax_temp <- tempfile(fileext = ".csv") tax_data <- data.frame( OTU_ID = c("OTU1", "OTU2", "OTU3"), Kingdom = c("Bacteria", "Bacteria", "Bacteria"), Phylum = c("Firmicutes", "Bacteroidetes", "Proteobacteria"), Class = c("Clostridia", "Bacteroidia", "Gammaproteobacteria") ) write.csv(tax_data, tax_temp, row.names = FALSE) return(list(otu_file = otu_temp, tax_file = tax_temp)) } test_that("mtap_config creates valid configuration with required parameters", { # Create test files test_files <- create_test_files() # Test 1: Basic configuration config <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file ) # Check class expect_s3_class(config, "mtap_config") expect_s3_class(config, "list") # Check structure expect_true(is.list(config)) expect_named(config) # Check required fields expect_true("otu_file" %in% names(config)) expect_true("tax_file" %in% names(config)) expect_true("output_prefix" %in% names(config)) expect_true("taxonomy_hierarchy" %in% names(config)) # Check default values expect_equal(config$output_prefix, "mtap_result") expect_null(config$taxonomy_hierarchy) # Clean up unlink(test_files$otu_file) unlink(test_files$tax_file) }) test_that("mtap_config handles custom output prefix and taxonomy hierarchy", { # Create test files test_files <- create_test_files() # Test 2: Custom output prefix config1 <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file, output_prefix = "my_analysis" ) expect_equal(config1$output_prefix, "my_analysis") # Test 3: Custom taxonomy hierarchy config2 <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file, taxonomy_hierarchy = c("Phylum", "Class", "Order") ) expect_equal(config2$taxonomy_hierarchy, c("Phylum", "Class", "Order")) # Test 4: Multiple parameters config3 <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file, output_prefix = "full_analysis", taxonomy_hierarchy = c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus") ) expect_equal(config3$output_prefix, "full_analysis") expect_equal( config3$taxonomy_hierarchy, c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus") ) # Clean up unlink(test_files$otu_file) unlink(test_files$tax_file) }) test_that("mtap_config handles additional parameters", { # Create test files test_files <- create_test_files() # Test 5: Additional parameters config <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file, threads = 4, verbose = TRUE, random_seed = 12345 ) # Check additional parameters expect_equal(config$threads, 4) expect_true(config$verbose) expect_equal(config$random_seed, 12345) # Clean up unlink(test_files$otu_file) unlink(test_files$tax_file) }) test_that("mtap_config validates required parameters", { # Test 6: Missing required parameters expect_error( mtap_config(), "Both otu_file and tax_file parameters are required" ) expect_error( mtap_config(otu_file = "otu.csv"), "Both otu_file and tax_file parameters are required" ) expect_error( mtap_config(tax_file = "tax.csv"), "Both otu_file and tax_file parameters are required" ) # Test 7: File path normalization test_files <- create_test_files() config <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file ) # Check if paths are normalized expect_true(is.character(config$otu_file)) expect_true(is.character(config$tax_file)) # Clean up unlink(test_files$otu_file) unlink(test_files$tax_file) }) test_that("print.mtap_config method works correctly", { # Create test files test_files <- create_test_files() config <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file, output_prefix = "test_print", taxonomy_hierarchy = c("Phylum", "Class") ) # Test 8: Print method expect_output( print(config), "MTaP Analysis Configuration" ) expect_output( print(config), "OTU table file:" ) expect_output( print(config), "Taxonomy table file:" ) expect_output( print(config), "Output prefix: test_print" ) expect_output( print(config), "Taxonomy hierarchy: Phylum -> Class" ) # Test 9: Print without taxonomy hierarchy config_no_hierarchy <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file ) expect_output( print(config_no_hierarchy), "Taxonomy hierarchy: Auto-detection" ) # Clean up unlink(test_files$otu_file) unlink(test_files$tax_file) }) test_that("mtap_validate_config validates configuration correctly", { # Create test files test_files <- create_test_files() # Test 10: Valid configuration config <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file ) # Core functionality test: function should return TRUE result <- suppressMessages(mtap_validate_config(config)) expect_true(result) # Test 11: Invalid configuration object expect_error( mtap_validate_config(list(otu_file = "test.csv")), "config must be a mtap_config object" ) # Test 12: Non-existent file config_bad <- mtap_config( otu_file = "/path/to/nonexistent/otu.csv", tax_file = test_files$tax_file ) expect_error( mtap_validate_config(config_bad), "OTU table file does not exist" ) # Test 13: Invalid output prefix config_invalid_prefix <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file, output_prefix = "" ) expect_error( mtap_validate_config(config_invalid_prefix), "output_prefix must be a non-empty string" ) # Test 14: Invalid taxonomy hierarchy config_invalid_hierarchy <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file, taxonomy_hierarchy = c("Phylum", "", "Class") ) expect_error( mtap_validate_config(config_invalid_hierarchy), "taxonomy_hierarchy cannot contain empty strings" ) # Clean up unlink(test_files$otu_file) unlink(test_files$tax_file) }) test_that("mtap_config with sample data works", { ## Test 15: Using package example data otu_file <- system.file("extdata", "example_otu_table.csv", package = "MTaP", mustWork = TRUE) tax_file <- system.file("extdata", "example_taxonomy_table.csv", package = "MTaP", mustWork = TRUE) # Check if example files exist skip_if_not(file.exists(otu_file), "Example OTU file not found") skip_if_not(file.exists(tax_file), "Example taxonomy file not found") config <- mtap_config(otu_file, tax_file) expect_s3_class(config, "mtap_config") expect_true(file.exists(config$otu_file)) expect_true(file.exists(config$tax_file)) }) # Additional: Test other functionality of configuration objects能 test_that("mtap_config objects have expected structure", { # Create test files test_files <- create_test_files() config <- mtap_config( otu_file = test_files$otu_file, tax_file = test_files$tax_file, output_prefix = "analysis1" ) # Test class structure expect_equal(class(config), c("mtap_config", "list")) # Test list structure expect_type(config, "list") expect_length(config, 4) # otu_file, tax_file, output_prefix, taxonomy_hierarchy # Clean up unlink(test_files$otu_file) unlink(test_files$tax_file) })