Skip to content

Commit

Permalink
Amend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zsteinmetz committed Aug 29, 2023
1 parent 68f5381 commit 423c8d1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 51 deletions.
30 changes: 17 additions & 13 deletions tests/testthat/test-manage_lib.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,55 @@ test_that("stop if OSF not reachable", {
skip_if_not(is.null(curl::nslookup("api.osf.io", error = F)),
message = "OSF is online")

expect_error(get_lib(type = "test", path = tmp))
get_lib(type = "test", path = tmp) |>
expect_error()
})

test_that("get_lib() downloads test library", {
skip_on_cran()
skip_if_offline(host = "api.osf.io")

expect_message(
expect_output(get_lib(type = "test", path = tmp))
)
get_lib(type = "test", path = tmp) |>
expect_output() |> expect_message()
get_lib(type = "model", path = tmp) |>
expect_output() |> expect_message()
})

test_that("check_lib() finds test library", {
skip_on_cran()
skip_if_offline(host = "api.osf.io")

expect_silent(check_lib(type = "test", path = tmp))
expect_warning(check_lib(type = c("raw", "derivative", "nobaseline"),
path = tmp))
check_lib(type = "test", path = tmp) |> expect_silent()
check_lib(type = c("raw", "derivative", "nobaseline"), path = tmp) |>
expect_warning()
})

test_that("get_lib() downloads complete library", {
skip_on_cran()
skip_if_offline(host = "api.osf.io")
skip_if_not(testthat:::on_ci(), "Not on CI")

expect_message(
expect_output(get_lib(type = c("derivative", "nobaseline"), path = tmp))
)
get_lib(type = c("derivative", "nobaseline"), path = tmp) |>
expect_output() |> expect_message()
})

test_that("check_lib() finds complete library", {
skip_on_cran()
skip_if_offline(host = "api.osf.io")
skip_if_not(testthat:::on_ci(), "Not on CI")

expect_silent(check_lib(type = c("derivative", "nobaseline"), path = tmp))
expect_warning(check_lib(type = "raw", path = tmp))
check_lib(type = c("derivative", "nobaseline"), path = tmp) |>
expect_silent()
check_lib(type = "raw", path = tmp) |>
expect_warning()
})

test_that("load_lib() works as expected", {
skip_on_cran()
skip_if_offline(host = "api.osf.io")

expect_silent(tl <- load_lib(type = "test", path = tmp))
tl <- load_lib(type = "test", path = tmp) |>
expect_silent()
expect_type(tl, "list")
expect_s3_class(tl, "OpenSpecy")
expect_identical(tl, test_lib, ignore_attr = T)
Expand Down
101 changes: 72 additions & 29 deletions tests/testthat/test-match_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,97 @@ dir.create(tmp, showWarnings = F)
# Create test data for cor_spec function
data("test_lib")

unknown <- read_any(read_extdata("ftir_ldpe_soil.asp")) |>
conform_spec(range= test_lib$wavenumber, res = spec_res(test_lib)) |>
unknown <- read_extdata("ftir_ldpe_soil.asp") |> read_any() |>
conform_spec(range = test_lib$wavenumber, res = spec_res(test_lib)) |>
process_spec(smooth_intens = T, make_rel = T)

# Create a subset of test_lib for filtering
test_lib_extract <- filter_spec(test_lib,
logic = test_lib$metadata$polymer_class == "polycarbonates")
test_lib_extract <- filter_spec(
test_lib, logic = test_lib$metadata$polymer_class == "polycarbonates"
)

# Match_spec function with AI
test_that("match_spec returns correct structure with AI", {
skip_on_cran()
get_lib("model")
expect_silent(check_lib("model"))
lib <- load_lib(type = "model")
skip_if_offline(host = "api.osf.io")

get_lib("model", path = tmp)
lib <- load_lib(type = "model", path = tmp)

set.seed(47)
rn <- runif(n = length(unique(lib$variables_in)))
fill <- as_OpenSpecy(as.numeric(unique(lib$variables_in)),
spectra = data.frame(runif(n = length(unique(lib$variables_in)))))
matches <- match_spec(x = unknown, library = lib, na.rm = T, fill = fill)
expect_true(nrow(matches) == 1)
expect_true(all(c("x", "y", "z", "value", "name") %in% names(matches)))
spectra = data.frame(rn))
matches <- match_spec(x = unknown, library = lib, na.rm = T, fill = fill) |>
expect_silent()
nrow(matches) |> expect_equal(1)
names(matches) |> expect_contains(c("x", "y", "z", "value", "name"))
round(matches$value, 2) |> expect_equal(0.52)
grepl("polyamide", matches$name) |> expect_true()
})

# Match_spec function
test_that("match_spec returns correct structure", {
matches <- match_spec(x = unknown, library = test_lib, na.rm = T, top_n = 5, add_library_metadata = "sample_name", add_object_metadata = "col_id")
expect_true(nrow(matches) == 5)
expect_true(all(c("object_id", "library_id", "match_val") %in% names(matches)))
matches <- match_spec(x = unknown, library = test_lib, na.rm = T, top_n = 5,
add_library_metadata = "sample_name",
add_object_metadata = "col_id") |>
expect_silent()

nrow(matches) |> expect_equal(5)
names(matches) |> expect_contains(c("object_id", "library_id", "match_val"))
round(matches$match_val, 2) |> expect_equal(c(0.67, 0.57, 0.55, 0.50, 0.43))
tolower(matches$polymer) |> expect_equal(
c("poly(ethylene)", "polystyrene", "poly(vinyl chloride)",
"poly(dimethylsiloxane) (pdms)", NA)
)
})

# Write the tests for cor_spec function
test_that("cor_spec returns a data.table with correct columns", {
matches <- cor_spec(unknown,library = test_lib)
matches <- cor_spec(unknown, library = test_lib) |>
expect_silent()

unknown2 <- unknown
unknown2$wavenumber[1:3] <- unknown2$wavenumber[1:3] +1
expect_warning(matches2 <- cor_spec(unknown2,library = test_lib))
expect_true(inherits(matches, "matrix"))
expect_identical(dim(matches), c(ncol(test_lib$spectra), ncol(unknown$spectra)))
top_matches <- max_cor_named(cor_matrix = matches, na.rm = T)
expect_true(length(top_matches) == 1)
expect_true(ncol(filter_spec(test_lib, logic = names(top_matches))$spectra) == 1)
unknown2$wavenumber[1:3] <- unknown2$wavenumber[1:3] + 1

matches2 <- cor_spec(unknown2, library = test_lib) |>
expect_warning()
inherits(matches, "matrix") |> expect_true()
expect_identical(dim(matches), c(ncol(test_lib$spectra),
ncol(unknown$spectra)))

top_matches <- max_cor_named(cor_matrix = matches, na.rm = T) |>
expect_silent()

expect_length(top_matches, 1)

ncol(filter_spec(test_lib, logic = names(top_matches))$spectra) |>
expect_equal(1)

test_lib$metadata$test <- NA
test_metadata <- get_metadata(test_lib, logic = names(top_matches), remove_empty = T)
expect_true(nrow(test_metadata) == 1)
expect_true(!"test" %in% names(test_metadata))
full_test <- ident_spec(matches, unknown, library = test_lib, top_n = 5, add_library_metadata = "sample_name")
expect_true(nrow(full_test) == 5)

test_metadata <- get_metadata(test_lib, logic = names(top_matches),
rm_empty = T) |>
expect_silent()

expect_equal(nrow(test_metadata), 1)
full_test <- ident_spec(matches, unknown, library = test_lib, top_n = 5,
add_library_metadata = "sample_name") |>
expect_silent()

nrow(full_test) |> expect_equal(5)
names(full_test) |> expect_contains(c("object_id", "library_id", "match_val"))
round(full_test$match_val, 2) |> expect_equal(c(0.67, 0.57, 0.55, 0.50, 0.43))
tolower(full_test$polymer) |> expect_equal(
c("poly(ethylene)", "polystyrene", "poly(vinyl chloride)",
"poly(dimethylsiloxane) (pdms)", NA)
)
})

# Write the tests for filter_spec function
test_that("filter_spec returns OpenSpecy object with filtered spectra", {
os_filtered <- filter_spec(test_lib, logic = rep(F,ncol(test_lib$spectra)))
os_filtered <- filter_spec(test_lib, logic = rep(F, ncol(test_lib$spectra))) |>
expect_silent()
expect_equal(ncol(os_filtered$spectra), 0)
expect_equal(nrow(os_filtered$metadata), 0)
})
Expand All @@ -63,7 +104,9 @@ test_that("filter_spec returns OpenSpecy object with filtered spectra", {
test_that("filter_spec returns OpenSpecy object with filtered spectra", {
logic <- rep(F,ncol(test_lib$spectra))
logic[1] <- TRUE
os_filtered <- filter_spec(test_lib, logic = logic)

os_filtered <- filter_spec(test_lib, logic = logic) |>
expect_silent()
expect_equal(ncol(os_filtered$spectra), 1)
expect_equal(nrow(os_filtered$metadata), 1)
})
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-process_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ test_that("process_spec() returns expected values", {
tiny_map <- read_extdata("CA_tiny_map.zip") |> read_any()

conf <- process_spec(tiny_map) |> expect_silent()
expect_equal(conf, conform_spec(tiny_map, range = NULL, res = 5) |>
smooth_intens(polynomial = 3,
window = 11,
derivative = 1,
abs = T,
make_rel = T))
expect_equal(conf, conform_spec(tiny_map, range = NULL, res = 5) |>
smooth_intens(polynomial = 3,
window = 11,
derivative = 1,
abs = T,
make_rel = T))


proc <- process_spec(raman_hdpe,
smooth_intens = TRUE,
Expand All @@ -19,6 +19,6 @@ test_that("process_spec() returns expected values", {
)
) |> expect_silent()

expect_equal(proc, conform_spec(raman_hdpe) |> smooth_intens(derivative = 1,
make_rel = T))
expect_equal(proc, conform_spec(raman_hdpe) |>
smooth_intens(derivative = 1, make_rel = T))
})

0 comments on commit 423c8d1

Please sign in to comment.