From 950a9df4394e0f0d9eed2680efbee6ba219622f8 Mon Sep 17 00:00:00 2001 From: Eric Siegel Date: Tue, 5 Dec 2023 09:50:06 -0800 Subject: [PATCH] Add end-to-end test with mocked executable --- tests/testthat/helper.R | 40 ++++++++++++++++++++++++++++++++-- tests/testthat/mock_louper | 5 +++++ tests/testthat/mock_louper.bat | 5 +++++ tests/testthat/test-lib.R | 25 +++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100755 tests/testthat/mock_louper create mode 100755 tests/testthat/mock_louper.bat create mode 100644 tests/testthat/test-lib.R diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index 8b892a2..3057315 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -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() @@ -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")) + } +} diff --git a/tests/testthat/mock_louper b/tests/testthat/mock_louper new file mode 100755 index 0000000..847dbae --- /dev/null +++ b/tests/testthat/mock_louper @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +echo "-----------RUNNING MOCK LOUPER EXECUTABLE--------------" +echo Arguments passed to the script: "$@" +echo "-------------------------------------------------------" diff --git a/tests/testthat/mock_louper.bat b/tests/testthat/mock_louper.bat new file mode 100755 index 0000000..64a028b --- /dev/null +++ b/tests/testthat/mock_louper.bat @@ -0,0 +1,5 @@ +@echo off + +echo "-----------RUNNING MOCK LOUPER.BAT EXECUTABLE--------------" +echo Arguments passed to the script: %* +echo "-----------------------------------------------------------" diff --git a/tests/testthat/test-lib.R b/tests/testthat/test-lib.R new file mode 100644 index 0000000..908e786 --- /dev/null +++ b/tests/testthat/test-lib.R @@ -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") +})