Skip to content

Commit 6f19a0c

Browse files
authored
Add end-to-end test with mocked executable (#26)
1 parent 927e0cd commit 6f19a0c

5 files changed

Lines changed: 87 additions & 4 deletions

File tree

R/eula.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ eula <- function() {
1818
return(FALSE)
1919
}
2020

21-
dir.create(eula_data_dir(), showWarnings=FALSE, recursive = TRUE)
22-
file.create(eula_lock_file())
21+
eula_create()
2322

2423
invisible(TRUE)
2524
}
2625

26+
#' Create Eula lock file
27+
#' @noRd
28+
eula_create <- function() {
29+
dir.create(eula_data_dir(), showWarnings=FALSE, recursive = TRUE)
30+
file.create(eula_lock_file())
31+
}
32+
2733
#' Reset Eula by removing lock file
2834
#' @noRd
2935
eula_reset <- function() {

tests/testthat/helper.R

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
#' Create random barcode
2+
random_barcode <- function(size = 10) {
3+
paste0(sample(c("A", "C", "T", "G"), size, replace=TRUE), collapse="")
4+
}
5+
16
#' Create a sparse count_mat
27
#'
38
#' @importFrom Matrix rsparsematrix
4-
create_count_mat <- function(rows, cols) {
9+
create_count_mat <- function(rows, cols, valid_barcodes = FALSE) {
510
mat <- Matrix::rsparsematrix(rows, cols, 0.5, rand.x = function(n) as.integer(100*runif(n)))
611

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

1217
colnames <- as.character()
1318
if (cols > 0) {
14-
colnames <- paste0("col", 1:cols)
19+
if (valid_barcodes) {
20+
colnames <- lapply(rep(10, cols), random_barcode)
21+
} else {
22+
colnames <- paste0("col", 1:cols)
23+
}
1524
}
1625

1726
dimnames(mat) <- list(rownames, colnames)
1827
mat
1928
}
2029

30+
#' Create a dimensional reduction (projection) object
31+
create_dim_reduction <- function(count_mat, key) {
32+
barcode_count <- dim(count_mat)[2]
33+
34+
proj <- create_dense_mat(barcode_count, 2)
35+
36+
rownames(proj) <- colnames(count_mat)
37+
colnames(proj) <- c(paste0(key, 1), paste0(key, 2))
38+
39+
Seurat::CreateDimReducObject(
40+
embeddings = proj,
41+
key = paste0(key, "_"),
42+
assay = "rna",
43+
global = TRUE
44+
)
45+
}
46+
2147
#' Create a dense matrix
2248
create_dense_mat <- function(rows, cols) {
2349
count <- rows * cols
2450
matrix(runif(count), nrow=rows)
2551
}
52+
53+
get_executable_path <- function() {
54+
wd <- getwd()
55+
os <- get_system_os()
56+
if (os == "windows") {
57+
return(file.path(wd, "mock_louper.bat"))
58+
} else {
59+
return(file.path(wd, "mock_louper"))
60+
}
61+
}

tests/testthat/mock_louper

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
echo "-----------RUNNING MOCK LOUPER EXECUTABLE--------------"
4+
echo Arguments passed to the script: "$@"
5+
echo "-------------------------------------------------------"

tests/testthat/mock_louper.bat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@echo off
2+
3+
echo "-----------RUNNING MOCK LOUPER.BAT EXECUTABLE--------------"
4+
echo Arguments passed to the script: %*
5+
echo "-----------------------------------------------------------"

tests/testthat/test-lib.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
test_that("can run create_loupe_from_seurat", {
2+
barcode_count <- 3
3+
4+
count_mat <- create_count_mat(100, barcode_count, valid_barcodes = TRUE)
5+
proj <- create_dim_reduction(count_mat, "proj1")
6+
cluster <- factor(seq(barcode_count))
7+
8+
obj <- Seurat::CreateSeuratObject(count_mat, assay="rna")
9+
obj[["proj1"]] <- proj
10+
obj[["cluster1"]] <- cluster
11+
12+
# create eula lock file to avoid interactive setup
13+
eula_create()
14+
15+
x <- create_loupe_from_seurat(obj, executable_path = get_executable_path())
16+
expect(x, "create_loupe_from_seurat returns TRUE")
17+
})
18+
19+
test_that("can run create_loupe", {
20+
barcode_count <- 5
21+
count_mat <- create_count_mat(100, barcode_count, valid_barcodes = TRUE)
22+
proj <- create_dense_mat(barcode_count, 2)
23+
clusters <- list("f1" = factor(c("a", "c", "b", "a", "b"), levels=c("a", "b", "c"), ordered=TRUE))
24+
projections <- list("p1" = proj)
25+
26+
# create eula lock file to avoid interactive setup
27+
eula_create()
28+
29+
x <- create_loupe(count_mat, clusters = clusters, projections = projections, executable_path = get_executable_path())
30+
expect(x, "create_loupe returns TRUE")
31+
})

0 commit comments

Comments
 (0)