Skip to content

Commit 8dd19fa

Browse files
Initial commit with updated testthat files
1 parent c06a606 commit 8dd19fa

14 files changed

+216
-2
lines changed

DESCRIPTION

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ Imports:
2929
ggplot2,
3030
kableExtra
3131
Suggests:
32-
BiocStyle,
32+
BiocStyle,
3333
knitr,
3434
rmarkdown,
3535
broom,
36-
jtools
36+
jtools,
37+
testthat (>= 3.0.0)
3738
Remotes:
3839
waldronlab/BugSigDBStats,
3940
waldronlab/bugsigdbr
@@ -43,3 +44,4 @@ VignetteBuilder: knitr
4344
RoxygenNote: 7.3.2
4445
Encoding: UTF-8
4546
URL: https://waldronlab.io/bugSigSimple
47+
Config/testthat/edition: 3

tests/testthat.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is part of the standard setup for testthat.
2+
# It is recommended that you do not modify it.
3+
#
4+
# Where should you do additional test configuration?
5+
# Learn more about the roles of various files in:
6+
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7+
# * https://testthat.r-lib.org/articles/special-files.html
8+
9+
library(testthat)
10+
library(bugSigSimple)
11+
12+
test_check("bugSigSimple")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#Function file: simple.R
2+
3+
4+
test_that("MPA.TAX.LEVELS contains correct taxonomic level abbreviations", {
5+
expected <- c("k", "p", "c", "o", "f", "g", "s", "t")
6+
names(expected) <- c("kingdom", "phylum", "class", "order",
7+
"family", "genus", "species", "strain")
8+
9+
expect_equal(MPA.TAX.LEVELS, expected)
10+
})
11+

tests/testthat/test-countBug.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#Function file: montecarlo.R
2+
3+
4+
test_that(".countBug returns max frequency of most common taxon", {
5+
relevant.sigs <- list(
6+
sig1 = c("Bacteroides", "Escherichia"),
7+
sig2 = c("Bacteroides", "Bifidobacterium")
8+
)
9+
10+
set.seed(123)
11+
result <- .countBug(relevant.sigs, c(2, 2))
12+
13+
expect_type(result, "integer")
14+
expect_true(result >= 1)
15+
expect_true(result <= 4) # Max possible count
16+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Function file: describe_curation.R
2+
3+
bsdb <- bugsigdbr::importBugSigDB()
4+
test_that("createStudyTable creates a table of all studies currently in data.frame", {
5+
expect_s3_class(createStudyTable(bsdb[1:10,]), "data.frame")
6+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Function file: describe_curation.R
2+
3+
bsdb <- bugsigdbr::importBugSigDB()
4+
test_that("createTaxonTable creates a table of most frequent taxa in a data.frame", {
5+
expect_s3_class(createTaxonTable(bsdb[1:10,]), "data.frame")
6+
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Function file: montecarlo.R
2+
3+
4+
# Mock .getTip function since it's not in global environment
5+
.getTip <- function(x) x
6+
7+
test_that("frequencySigs identifies the most frequently occuring taxa", {
8+
test_sigs <- list(
9+
"UP_sig1" = c("Bacteroides", "Escherichia"),
10+
"UP_sig2" = c("Bacteroides", "Bifidobacterium")
11+
)
12+
13+
result <- frequencySigs(test_sigs, n = 2)
14+
15+
expect_s3_class(result, "table")
16+
expect_equal(length(result), 2)
17+
expect_equal(names(result)[1], "Bacteroides")
18+
})

tests/testthat/test-getCriticalN.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#Function file: montecarlo.R
2+
3+
4+
test_that("getCriticalN returns 95th percentile threshold", {
5+
relevant.sigs <- list(
6+
sig1 = c("Bacteroides", "Escherichia"),
7+
sig2 = c("Bacteroides", "Bifidobacterium")
8+
)
9+
10+
set.seed(123)
11+
result <- getCriticalN(relevant.sigs, c(2, 2), nsim = 100)
12+
13+
expect_type(result, "double")
14+
expect_true(result >= 1)
15+
expect_true(result <= 4)
16+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#Function file: simple.R
2+
3+
4+
test_that("getMostFrequentTaxa returns the most frequently occurring taxa in a table of signatures", {
5+
# Create test data
6+
test_data <- data.frame(
7+
PMID = c("123", "456", "789"),
8+
`Abundance in Group 1` = c("increased", "decreased", "increased"),
9+
check.names = FALSE
10+
)
11+
12+
# Mock bugsigdbr::getSignatures to return test taxa
13+
mock_signatures <- list(
14+
"123" = c("k__Bacteria|g__Blautia", "k__Bacteria|g__Bacteroides"),
15+
"456" = c("k__Bacteria|g__Blautia", "k__Bacteria|g__Lactobacillus"),
16+
"789" = c("k__Bacteria|g__Blautia")
17+
)
18+
19+
with_mocked_bindings(
20+
getSignatures = function(...) mock_signatures,
21+
.package = "bugsigdbr",
22+
{
23+
result <- getMostFrequentTaxa(test_data, n = 5)
24+
25+
# Basic checks
26+
expect_true(is.table(result))
27+
expect_true(length(result) <= 5)
28+
expect_equal(as.numeric(result[1]), 3) # Blautia appears 3 times
29+
expect_equal(names(result[1]), "k__Bacteria|g__Blautia") # Check name
30+
expect_true(all(diff(result) <= 0)) # Sorted descending
31+
}
32+
)
33+
})

tests/testthat/test-getTip.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#Function file: simple.R
2+
3+
4+
# Firstly, test when there are multiple levels
5+
test_that(".getTip returns the last part of the string", {
6+
s <- "k__Bacteria|p__Firmicutes|g__Lactobacillus"
7+
8+
result <- .getTip(s)
9+
10+
expect_equal(result, "g__Lactobacillus")
11+
})
12+
13+
14+
# Secondly, test when there is only one level
15+
test_that(".getTip works when string has only one level", {
16+
s <- "k__Bacteria"
17+
result <- .getTip(s)
18+
expect_equal(result, "k__Bacteria")
19+
})

0 commit comments

Comments
 (0)