Skip to content

Commit

Permalink
Add end-to-end test with mocked executable
Browse files Browse the repository at this point in the history
  • Loading branch information
esiegel committed Dec 6, 2023
1 parent 927e0cd commit 950a9df
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
40 changes: 38 additions & 2 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#' Create random barcode
random_barcode <- function(size = 10) {
paste0(sample(c("A", "C", "T", "G"), size, replace=TRUE), collapse="")
}

#' Create a sparse count_mat
#'
#' @importFrom Matrix rsparsematrix
create_count_mat <- function(rows, cols) {
create_count_mat <- function(rows, cols, valid_barcodes = FALSE) {
mat <- Matrix::rsparsematrix(rows, cols, 0.5, rand.x = function(n) as.integer(100*runif(n)))

rownames <- as.character()
Expand All @@ -11,15 +16,46 @@ create_count_mat <- function(rows, cols) {

colnames <- as.character()
if (cols > 0) {
colnames <- paste0("col", 1:cols)
if (valid_barcodes) {
colnames <- lapply(rep(10, cols), random_barcode)
} else {
colnames <- paste0("col", 1:cols)
}
}

dimnames(mat) <- list(rownames, colnames)
mat
}

#' Create a dimensional reduction (projection) object
create_dim_reduction <- function(count_mat, key) {
barcode_count <- dim(count_mat)[2]

proj <- create_dense_mat(barcode_count, 2)

rownames(proj) <- colnames(count_mat)
colnames(proj) <- c(paste0(key, 1), paste0(key, 2))

Seurat::CreateDimReducObject(
embeddings = proj,
key = paste0(key, "_"),
assay = "rna",
global = TRUE
)
}

#' Create a dense matrix
create_dense_mat <- function(rows, cols) {
count <- rows * cols
matrix(runif(count), nrow=rows)
}

get_executable_path <- function() {
wd <- getwd()
os <- get_system_os()
if (os == "windows") {
return(file.path(wd, "mock_louper.bat"))
} else {
return(file.path(wd, "mock_louper"))
}
}
5 changes: 5 additions & 0 deletions tests/testthat/mock_louper
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

echo "-----------RUNNING MOCK LOUPER EXECUTABLE--------------"
echo Arguments passed to the script: "$@"
echo "-------------------------------------------------------"
5 changes: 5 additions & 0 deletions tests/testthat/mock_louper.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off

echo "-----------RUNNING MOCK LOUPER.BAT EXECUTABLE--------------"
echo Arguments passed to the script: %*
echo "-----------------------------------------------------------"
25 changes: 25 additions & 0 deletions tests/testthat/test-lib.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test_that("can run create_loupe_from_seurat", {
barcode_count <- 3

count_mat <- create_count_mat(100, barcode_count, valid_barcodes = TRUE)
proj <- create_dim_reduction(count_mat, "proj1")
cluster <- factor(seq(barcode_count))

obj <- Seurat::CreateSeuratObject(count_mat, assay="rna")
obj[["proj1"]] <- proj
obj[["cluster1"]] <- cluster

x <- create_loupe_from_seurat(obj, executable_path = get_executable_path())
expect(x, "create_loupe_from_seurat returns TRUE")
})

test_that("can run create_loupe", {
barcode_count <- 5
count_mat <- create_count_mat(100, barcode_count, valid_barcodes = TRUE)
proj <- create_dense_mat(barcode_count, 2)
clusters <- list("f1" = factor(c("a", "c", "b", "a", "b"), levels=c("a", "b", "c"), ordered=TRUE))
projections <- list("p1" = proj)

x <- create_loupe(count_mat, clusters = clusters, projections = projections, executable_path = get_executable_path())
expect(x, "create_loupe returns TRUE")
})

0 comments on commit 950a9df

Please sign in to comment.