Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zsteinmetz committed Aug 20, 2023
1 parent 6d4ca99 commit b88444e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
7 changes: 5 additions & 2 deletions tests/testthat/test-adj_range.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
library(data.table)

test_that("restrict_range() provides correct range", {
test_noise <- as_OpenSpecy(x = seq(400,4000, by = 10),
spectra = data.table(intensity = rnorm(361)))
Expand Down Expand Up @@ -41,8 +43,9 @@ test_that("flatten_range() function test", {
expect_false(all.equal(flat_map$spectra, tiny_map$spectra) |> isTRUE())
expect_equal(flat_map$spectra[1:20], tiny_map$spectra[1:20])

expect_equal(flat_map$spectra[40:60, 1:5] |> unique() |> round(4),
data.table(-0.8694, -0.7769, -0.5828, -0.292, 0.1916))
expect_equal(flat_map$spectra[40:60, 1:5] |> unique() |> round(4)
|> as.numeric(),
c(-0.8694, -1.246, -0.8304, -1.1909, -0.7857))
})

test_that("flatten_range() error handling", {
Expand Down
24 changes: 12 additions & 12 deletions tests/testthat/test-def_features.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
map <- read_extdata("CA_tiny_map.zip") |> read_any()

test_that("features are identified when given logical", {
map$metadata$particles <- map$metadata$x == 0
map$metadata$particles <- map$metadata$y == 0
id_map <- def_features(map, map$metadata$particles)
expect_true(length(unique(id_map$metadata$feature_id)) == 2)
expect_true(max(id_map$metadata$area, na.rm = T) == 13)
expect_true(max(id_map$metadata$feret_max, na.rm = T) == 13)
expect_length(unique(id_map$metadata$feature_id), 2)
expect_equal(max(id_map$metadata$area, na.rm = T), 13)
expect_equal(max(id_map$metadata$feret_max, na.rm = T), 13)
})

test_that("particles are identified when given character", {
map$metadata$particles <- ifelse(map$metadata$x == 1, "particle", "not_particle")
map$metadata$particles <- ifelse(map$metadata$y == 1, "particle", "not_particle")
id_map <- def_features(map, map$metadata$particles)
expect_true(length(unique(id_map$metadata$feature_id)) == 3)
expect_true(max(id_map$metadata$area, na.rm = T) == 182)
expect_true(round(max(id_map$metadata$feret_max, na.rm = T)) == 19)
expect_length(unique(id_map$metadata$feature_id), 3)
expect_equal(max(id_map$metadata$area, na.rm = T), 182)
expect_equal(round(max(id_map$metadata$feret_max, na.rm = T)), 19)
})

test_that("an error is thrown for invalid feature input", {
Expand Down Expand Up @@ -46,21 +46,21 @@ test_that("the original spectrum remains unmodified and metadata is amended", {
})

test_that("collapse particles returns expected values", {
particles <- ifelse(map$metadata$x == 1, "particleA", "particleB")
particles <- ifelse(map$metadata$y == 1, "particleA", "particleB")
id_map <- def_features(map, particles)
test_collapsed <- collapse_spec(id_map)

expect_equal(test_collapsed$metadata |> nrow(), 3)
expect_equal(test_collapsed$metadata$feret_max |> round(2), c(13, 13, 18.69))
expect_equal(test_collapsed$metadata$centroid_y |> unique(), 6)
expect_equal(test_collapsed$metadata$centroid_x |> unique(), 6)

particles <- map$metadata$x == 1
particles <- map$metadata$y == 1
id_map <- def_features(map, particles)
test_collapsed <- collapse_spec(id_map)

expect_equal(test_collapsed$metadata |> nrow(), 2)
expect_equal(test_collapsed$metadata$feret_max |> round(2), c(NA, 13))
expect_equal(test_collapsed$metadata$centroid_y |> unique(), 6)
expect_equal(test_collapsed$metadata$centroid_x |> unique(), 6)

expect_contains(names(test_collapsed$metadata),
c("feature_id", "area", "feret_max", "centroid_y",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-read_envi.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("ENVI files are read", {
expect_message(tiny_map <- read_zip(read_extdata("CA_tiny_map.zip")))
tiny_map <- read_zip(read_extdata("CA_tiny_map.zip"))
expect_s3_class(tiny_map, "OpenSpecy")

expect_equal(ncol(tiny_map$spectra), 208)
Expand Down

0 comments on commit b88444e

Please sign in to comment.