library(testthat) library(ILORA) test_that("get_species_names returns a character vector", { species <- get_species_names() expect_type(species, "character") expect_gt(length(species), 0) expect_true("Rubus buergeri Miq." %in% species) }) test_that("get_variable_names returns a character vector", { variables <- get_variable_names() expect_type(variables, "character") expect_gt(length(variables), 0) expect_true("genus" %in% variables) }) test_that("get_table_names returns a character vector", { variables <- get_variable_names() expect_type(variables, "character") expect_gt(length(variables), 0) expect_true("introduction" %in% variables) }) test_that("get_data returns a data frame for valid inputs", { species_data <- get_data("Rubus buergeri Miq.", c("orders", "0700_Fuels2", "genus", "species")) expect_s3_class(species_data, "data.frame") expect_gt(nrow(species_data), 0) expect_true(all(c("orders", "0700_Fuels2", "genus", "species") %in% names(species_data))) }) test_that("get_data returns NULL for invalid species", { species_data <- get_data("Invalid species", c("orders", "0700_Fuels2", "genus", "species")) expect_null(species_data) }) #test_that("get_data handles empty species vector", { # output <- capture.output({ # species_data <- get_data(character(0), c("orders", "0700_Fuels2", "genus", "species")) #}, type = "message") #expect_true(any(grepl("Species vector cannot be empty.", output))) #expect_null(species_data) #}) test_that("get_species_details retrieves correct details for a single species", { species_name <- "Dahlia coccinea Cav." result <- get_species_details(species_name) expect_true(is.list(result)) expect_true("taxonomy" %in% names(result)) expect_true("invasion_status" %in% names(result)) expect_true("general_info" %in% names(result)) expect_true("native_range" %in% names(result)) expect_true("introduction" %in% names(result)) expect_true("uses" %in% names(result)) expect_true("market_info" %in% names(result)) expect_true("habitat" %in% names(result)) expect_true("naturalized_range" %in% names(result)) expect_true("occurrence_distribution" %in% names(result)) expect_true("geography" %in% names(result)) expect_true("climate" %in% names(result)) expect_true("data_availability" %in% names(result)) }) test_that("get_species_details retrieves correct details for multiple species", { species_names <- c("Dahlia coccinea Cav.", "Acacia auriculiformis Benth.") result <- get_species_details(species_names) expect_true(is.list(result)) expect_true(all(species_names %in% (result$taxonomy$acc_species_name))) expect_true(all(species_names %in% (result$invasion_status$acc_species_name))) expect_true(all(species_names %in% (result$general_info$acc_species_name_gen))) expect_true(all(species_names %in% (result$native_range$acc_species_name_nati))) expect_true(all(species_names %in% (result$introduction$acc_species_name_int))) expect_true(all(species_names %in% (result$uses$acc_species_name_ecou))) expect_true(all(species_names %in% (result$market_info$acc_species_name_mar))) expect_true(all(species_names %in% (result$habitat$acc_species_name_hab))) expect_true(all(species_names %in% (result$naturalized_range$acc_species_name_natur))) expect_true(all(species_names %in% (result$occurrence_distribution$occurrence$acc_species_name_occ))) expect_true(all(species_names %in% (result$occurrence_distribution$distribution$acc_species_name_dis))) expect_true(all(species_names %in% (result$geography$lulc$acc_species_name_lulc))) expect_true(all(species_names %in% (result$geography$anthrome$acc_species_name_ant))) expect_true(all(species_names %in% (result$geography$ecoregions$acc_species_name_ecor))) expect_true(all(species_names %in% (result$climate$acc_species_name_cli))) expect_true(all(species_names %in% (result$data_availability$acc_species_name_sum))) }) test_that("get_species_details handles invalid input types", { expect_error(get_species_details(123), "species_name should be character or list of characters") }) test_that("visualize_native_range creates a plot for given species", { skip_on_cran() species_name <- "Dahlia coccinea Cav." # Print species_name to debug print(paste("Testing with species_name:", species_name)) plot <- visualize_native_range(species_name) # Print class of the plot object to debug print(class(plot)) expect_s3_class(plot, "ggplot") }) # Tests for plot_species_on_map function test_that("plot_species_on_map creates a plot for given species", { species_names <- "Dahlia coccinea Cav." plot <- plot_species_on_map(species_names) expect_s3_class(plot, "ggplot") }) test_that("plot_species_on_map handles custom colors", { custom_colors <- list("Dahlia coccinea Cav." = "red") species_names <- "Dahlia coccinea Cav." plot <- plot_species_on_map(species_names, custom_colors = custom_colors) expect_s3_class(plot, "ggplot") }) test_that("plot_species_on_map returns error for invalid custom colors input", { custom_colors <- "red" species_names <- "Dahlia coccinea Cav." expect_error(plot_species_on_map(species_names, custom_colors = custom_colors), "custom_colors must be a list when specifying custom colors.") }) # Tests for bar_plot function test_that("bar_plot creates a plot", { plot <- bar_plot() expect_s3_class(plot, "ggplot") }) test_that("bar_plot has correct title and labels", { plot <- bar_plot() expect_match(plot$labels$title, "Number of Species for Each Invasion Status Category") expect_match(plot$labels$x, "Invasion Status Category") expect_match(plot$labels$y, "Number of Species") }) # Tests for species_count_plot function test_that("species_count_plot creates ggplot objects", { plots <- species_count_plot() expect_s3_class(plots$plot_growthhabit, "ggplot") expect_s3_class(plots$plot_duration, "ggplot") }) test_that("species_count_plot has correct titles and labels", { plots <- species_count_plot() # Check titles and labels for growth habit plot expect_match(plots$plot_growthhabit$labels$title, "Number of Species for Each Growth Habit Category") expect_match(plots$plot_growthhabit$labels$x, "Growth Habit Category") expect_match(plots$plot_growthhabit$labels$y, "Number of Species") # Check titles and labels for duration plot expect_match(plots$plot_duration$labels$title, "Number of Species for Each Duration Category") expect_match(plots$plot_duration$labels$x, "Duration Category") expect_match(plots$plot_duration$labels$y, "Number of Species") }) # Tests for introduction_pathways_plot function test_that("introduction_pathways_plot creates a Plotly pie chart", { plot <- introduction_pathways_plot() expect_s3_class(plot, "plotly") })