Skip to content

Commit

Permalink
[r] Update tests (#1928)
Browse files Browse the repository at this point in the history
* [r] Replace use of SeuratObject in ecosystem-agnostic function
Current `write_soma.data.frame()` pipeline used
`SeuratObject::RandomName()`; this shouldn't happen as
`write_soma.data.frame()` is supposed to be ecosystem agnostic. This PR
re-implements `SeuratObject::RandomName()` as `random_name()` and uses
that instead

* Update tests for SOMAOpen to be cleaner and more flexible

* Add error messages

* Skip iterated sparse matrix reader on CI
For some reason, iterated sparse matrix reader is segfaulting on CI, but
not locally. Add `skip_on_ci()` to skip these tests on CI, but not
locally
  • Loading branch information
mojaveazure authored Nov 20, 2023
1 parent 1ebc379 commit c298f9b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
9 changes: 9 additions & 0 deletions apis/r/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ null <- function(...) {
return(NULL)
}

random_name <- function(length = 5L, chars = letters, ...) {
stopifnot(
"'length' must be a single integer" = is_integerish(length, n = 1L),
"'chars' must be character" = is.character(chars)
)
chars <- unique(unlist(strsplit(chars, split = '')))
return(paste(sample(chars, size = length, ...), collapse = ''))
}

#' Pad Names of a Character Vector
#'
#' Fill in missing names of a vector using missing values of said vector
Expand Down
3 changes: 1 addition & 2 deletions apis/r/R/write_soma.R
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ write_soma.TsparseMatrix <- function(
prefix = 'tiledbsoma',
...
) {
check_package('SeuratObject', version = .MINIMUM_SEURAT_VERSION())
stopifnot(
"'x' must be a data frame" = is.data.frame(x) || inherits(x, 'DataFrame'),
"'alt' must be a single character value" = is_scalar_character(alt),
Expand All @@ -369,7 +368,7 @@ write_soma.TsparseMatrix <- function(
'2' = alt,
'3' = paste(prefix, default, sep = '_'),
'4' = paste(prefix, alt, sep = '_'),
SeuratObject::RandomName(length = i, ...)
random_name(length = i, ...)
)
i <- i + 1L
}
Expand Down
1 change: 1 addition & 0 deletions apis/r/tests/testthat/test-SOMAArrayReader-Iterated.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ test_that("Iterated Interface from SOMA Classes", {

test_that("Iterated Interface from SOMA Sparse Matrix", {
skip_if(!extended_tests() || covr_tests())
skip_on_ci()
skip_if_not_installed("pbmc3k.tiledb") # a Suggests: pre-package 3k PBMC data

tdir <- tempfile()
Expand Down
24 changes: 15 additions & 9 deletions apis/r/tests/testthat/test-SOMAOpen.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@ test_that("SOMAOpen", {
skip_if_not_installed("pbmc3k.tiledb") # a Suggests: pre-package 3k PBMC data

tdir <- tempfile()
tgzfile <- system.file("raw-data", "soco-pbmc3k.tar.gz", package="pbmc3k.tiledb")
tgzfile <- system.file("raw-data", "soco-pbmc3k.tar.gz", package = "pbmc3k.tiledb")
untar(tarfile = tgzfile, exdir = tdir)
uri <- file.path(tdir, "soco")

expect_error(SOMAOpen(tdir)) # we cannot open directories are neither TileDB Array nor Group

expect_true(inherits(SOMAOpen(uri), "SOMACollection"))
expect_s3_class(SOMAOpen(uri), "SOMACollection")

expect_true(inherits(SOMAOpen(file.path(uri, "pbmc3k_processed")), "SOMAExperiment"))
expect_s3_class(SOMAOpen(file.path(uri, "pbmc3k_processed")), "SOMAExperiment")

expect_true(inherits(SOMAOpen(file.path(uri, "pbmc3k_processed", "ms")), "SOMACollection"))
expect_true(inherits(SOMAOpen(file.path(uri, "pbmc3k_processed", "obs")), "SOMADataFrame"))
expect_s3_class(SOMAOpen(file.path(uri, "pbmc3k_processed", "ms")), "SOMACollection")
expect_s3_class(SOMAOpen(file.path(uri, "pbmc3k_processed", "obs")), "SOMADataFrame")

expect_true(inherits(SOMAOpen(file.path(uri, "pbmc3k_processed", "ms", "raw")), "SOMAMeasurement"))
expect_true(inherits(SOMAOpen(file.path(uri, "pbmc3k_processed", "ms", "RNA")), "SOMAMeasurement"))
expect_s3_class(SOMAOpen(file.path(uri, "pbmc3k_processed", "ms", "raw")), "SOMAMeasurement")
expect_s3_class(SOMAOpen(file.path(uri, "pbmc3k_processed", "ms", "RNA")), "SOMAMeasurement")

expect_true(inherits(SOMAOpen(file.path(uri, "pbmc3k_processed", "ms", "RNA", "obsm", "X_draw_graph_fr")), "SOMADenseNDArray"))
expect_s3_class(
SOMAOpen(file.path(uri, "pbmc3k_processed", "ms", "RNA", "obsm", "X_draw_graph_fr")),
c("SOMADenseNDArray", "SOMASparseNDArray")
)

expect_true(inherits(SOMAOpen(file.path(uri, "pbmc3k_processed", "ms", "raw", "X", "data")), "SOMASparseNDArray"))
expect_s3_class(
SOMAOpen(file.path(uri, "pbmc3k_processed", "ms", "raw", "X", "data")),
c("SOMASparseNDArray", "SOMADenseNDArray")
)

})

0 comments on commit c298f9b

Please sign in to comment.