Skip to content

Commit

Permalink
test: more robust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
js2264 committed Oct 10, 2023
1 parent c340492 commit 2d7728d
Show file tree
Hide file tree
Showing 4 changed files with 362 additions and 26 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ S3method(arrange,GInteractions)
S3method(as_ginteractions,data.frame)
S3method(as_ginteractions,default)
S3method(count,GInteractions)
S3method(count,GroupedGInteractions)
S3method(count_overlaps,GInteractions)
S3method(count_overlaps,PinnedGInteractions)
S3method(count_overlaps_directed,GInteractions)
Expand Down
146 changes: 146 additions & 0 deletions tests/testthat/test-classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,150 @@ test_that("classes work", {
gi |> ungroup() |> expect_error("GInteractions")
show(ggi) |> expect_no_error()

## DelegatingGInteractions
expect_identical(
anchors1(apgi),
new("GRanges", seqnames = new("Rle", values = structure(1L, levels = c("chr1",
"chr2"), class = "factor"), lengths = 4L, elementMetadata = NULL,
metadata = list()), ranges = new("IRanges", start = c(11L,
11L, 11L, 11L), width = c(10L, 10L, 20L, 20L), NAMES = NULL,
elementType = "ANY", elementMetadata = NULL, metadata = list()),
strand = new("Rle", values = structure(1:2, levels = c("+",
"-", "*"), class = "factor"), lengths = c(2L, 2L), elementMetadata = NULL,
metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1",
"chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA,
NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame",
rownames = NULL, nrows = 4L, elementType = "ANY", elementMetadata = NULL,
metadata = list(), listData = structure(list(), names = character(0))),
elementType = "ANY", metadata = list())
)
expect_identical(
ranges1(apgi),
new("IRanges", start = c(11L, 11L, 11L, 11L), width = c(10L,
10L, 20L, 20L), NAMES = NULL, elementType = "ANY", elementMetadata = NULL,
metadata = list())
)
expect_identical(
seqnames1(apgi),
new("Rle", values = structure(1L, levels = c("chr1", "chr2"), class = "factor"),
lengths = 4L, elementMetadata = NULL, metadata = list())
)
expect_identical(
start1(apgi),
c(11L, 11L, 11L, 11L)
)
expect_identical(
end1(apgi),
c(20L, 20L, 30L, 30L)
)
expect_identical(
width1(apgi),
c(10L, 10L, 20L, 20L)
)
expect_identical(
strand1(apgi),
new("Rle", values = structure(1:2, levels = c("+", "-", "*"), class = "factor"),
lengths = c(2L, 2L), elementMetadata = NULL, metadata = list())
)
expect_identical(
anchors2(apgi),
new("GRanges", seqnames = new("Rle", values = structure(1:2, levels = c("chr1",
"chr2"), class = "factor"), lengths = c(3L, 1L), elementMetadata = NULL,
metadata = list()), ranges = new("IRanges", start = c(21L,
51L, 51L, 51L), width = c(10L, 5L, 5L, 10L), NAMES = NULL, elementType = "ANY",
elementMetadata = NULL, metadata = list()), strand = new("Rle",
values = structure(1:2, levels = c("+", "-", "*"), class = "factor"),
lengths = c(2L, 2L), elementMetadata = NULL, metadata = list()),
seqinfo = new("Seqinfo", seqnames = c("chr1", "chr2"), seqlengths = c(NA_integer_,
NA_integer_), is_circular = c(NA, NA), genome = c(NA_character_,
NA_character_)), elementMetadata = new("DFrame", rownames = NULL,
nrows = 4L, elementType = "ANY", elementMetadata = NULL,
metadata = list(), listData = structure(list(), names = character(0))),
elementType = "ANY", metadata = list())
)
expect_identical(
ranges2(apgi),
new("IRanges", start = c(21L, 51L, 51L, 51L), width = c(10L,
5L, 5L, 10L), NAMES = NULL, elementType = "ANY", elementMetadata = NULL,
metadata = list())
)
expect_identical(
seqnames2(apgi),
new("Rle", values = structure(1:2, levels = c("chr1", "chr2"), class = "factor"),
lengths = c(3L, 1L), elementMetadata = NULL, metadata = list())
)
expect_identical(
start2(apgi),
c(21L, 51L, 51L, 51L)
)
expect_identical(
end2(apgi),
c(30L, 55L, 55L, 60L)
)
expect_identical(
width2(apgi),
c(10L, 5L, 5L, 10L)
)
expect_identical(
strand2(apgi),
new("Rle", values = structure(1:2, levels = c("+", "-", "*"), class = "factor"),
lengths = c(2L, 2L), elementMetadata = NULL, metadata = list())
)
expect_identical(
anchors(apgi),
list(first = new("GRanges", seqnames = new("Rle", values = structure(1L, levels = c("chr1",
"chr2"), class = "factor"), lengths = 4L, elementMetadata = NULL,
metadata = list()), ranges = new("IRanges", start = c(11L,
11L, 11L, 11L), width = c(10L, 10L, 20L, 20L), NAMES = NULL,
elementType = "ANY", elementMetadata = NULL, metadata = list()),
strand = new("Rle", values = structure(1:2, levels = c("+",
"-", "*"), class = "factor"), lengths = c(2L, 2L), elementMetadata = NULL,
metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1",
"chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA,
NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame",
rownames = NULL, nrows = 4L, elementType = "ANY", elementMetadata = NULL,
metadata = list(), listData = structure(list(), names = character(0))),
elementType = "ANY", metadata = list()), second = new("GRanges",
seqnames = new("Rle", values = structure(1:2, levels = c("chr1",
"chr2"), class = "factor"), lengths = c(3L, 1L), elementMetadata = NULL,
metadata = list()), ranges = new("IRanges", start = c(21L,
51L, 51L, 51L), width = c(10L, 5L, 5L, 10L), NAMES = NULL,
elementType = "ANY", elementMetadata = NULL, metadata = list()),
strand = new("Rle", values = structure(1:2, levels = c("+",
"-", "*"), class = "factor"), lengths = c(2L, 2L), elementMetadata = NULL,
metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1",
"chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA,
NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame",
rownames = NULL, nrows = 4L, elementType = "ANY", elementMetadata = NULL,
metadata = list(), listData = structure(list(), names = character(0))),
elementType = "ANY", metadata = list()))
)
expect_identical(
regions(apgi),
new("GRanges", seqnames = new("Rle", values = structure(1:2, levels = c("chr1",
"chr2"), class = "factor"), lengths = c(5L, 1L), elementMetadata = NULL,
metadata = list()), ranges = new("IRanges", start = c(11L,
21L, 51L, 11L, 51L, 51L), width = c(10L, 10L, 5L, 20L, 5L, 10L
), NAMES = NULL, elementType = "ANY", elementMetadata = NULL,
metadata = list()), strand = new("Rle", values = structure(1:2, levels = c("+",
"-", "*"), class = "factor"), lengths = c(3L, 3L), elementMetadata = NULL,
metadata = list()), seqinfo = new("Seqinfo", seqnames = c("chr1",
"chr2"), seqlengths = c(NA_integer_, NA_integer_), is_circular = c(NA,
NA), genome = c(NA_character_, NA_character_)), elementMetadata = new("DFrame",
rownames = NULL, nrows = 6L, elementType = "ANY", elementMetadata = NULL,
metadata = list(), listData = structure(list(), names = character(0))),
elementType = "ANY", metadata = list())
)
expect_identical(
seqinfo(apgi),
new("Seqinfo", seqnames = c("chr1", "chr2"), seqlengths = c(NA_integer_,
NA_integer_), is_circular = c(NA, NA), genome = c(NA_character_,
NA_character_))
)
expect_identical(
mcols(apgi)[,2],
c("cis", "cis", "cis", "trans")
)
show(apgi) |> expect_no_error()

})
186 changes: 170 additions & 16 deletions tests/testthat/test-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,110 @@ test_that("dplyr functions work", {
## tally/count

ggi |> count(strand1) |> expect_no_error()
ggi |> tally() |> expect_no_error()
gi |> count(strand1) |> expect_s4_class("DataFrame")
gi |> tally() |> expect_error()
expect_identical(
ggi |> count(group),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2), n = c(2L, 2L)))
)
expect_identical(
ggi |> count(type),
new("DFrame", rownames = NULL, nrows = 3L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2, 2), type = c("cis", "cis", "trans"),
n = c(2L, 1L, 1L)))
)
expect_identical(
ggi |> tally(),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2), n = c(2L, 2L)))
)
expect_identical(
ggi |> count(strand1),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2), strand1 = new("Rle", values = structure(1:2, levels = c("+",
"-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL,
metadata = list()), n = c(2L, 2L)))
)
expect_identical(
gi |> count(strand1),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
strand1 = new("Rle", values = structure(1:2, levels = c("+",
"-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL,
metadata = list()), n = c(2L, 2L)))
)
expect_identical(
ggi |> count(strand1, wt = score),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2), strand1 = new("Rle", values = structure(1:2, levels = c("+",
"-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL,
metadata = list()), n = c(0.736002816120163, 1.23265417455696
))),
tolerance = 1e-4
)
expect_identical(
gi |> count(strand1, wt = score),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
strand1 = new("Rle", values = structure(1:2, levels = c("+",
"-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL,
metadata = list()), n = c(0.736002816120163, 1.23265417455696
))),
tolerance = 1e-4
)
expect_identical(
ggi |> count(strand1, wt = score, sort = TRUE),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(2, 1), strand1 = new("Rle", values = structure(2:1, levels = c("+",
"-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL,
metadata = list()), n = c(1.23265417455696, 0.736002816120163
))),
tolerance = 1e-4
)
expect_identical(
gi |> count(strand1, wt = score, sort = TRUE),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
strand1 = new("Rle", values = structure(2:1, levels = c("+",
"-", "*"), class = "factor"), lengths = c(1L, 1L), elementMetadata = NULL,
metadata = list()), n = c(1.23265417455696, 0.736002816120163
))),
tolerance = 1e-4
)

## filter

expect_identical(gi |> filter(strand1 == '-') |> length(), 2L)
expect_identical(
gi |> filter(strand1 == '-') |> ranges1(),
new("IRanges", start = c(11L, 11L), width = c(20L, 20L), NAMES = NULL,
elementType = "ANY", elementMetadata = NULL, metadata = list())
)
expect_identical(
gi |> filter(strand1 == '+') |> ranges1(),
new("IRanges", start = c(11L, 11L), width = c(10L, 10L), NAMES = NULL,
elementType = "ANY", elementMetadata = NULL, metadata = list())
)
ggi |> filter(strand1 == '+') |> expect_error()

## mutate

gi |> mutate(strand1 = '-') |> expect_s4_class("GInteractions")
gi |> mutate(xxx = 1) |> expect_s4_class("GInteractions")
expect_identical(
gi |> mutate(strand1 = '-') |> strand1(),
new("Rle", values = structure(2L, levels = c("+", "-", "*"), class = "factor"),
lengths = 4L, elementMetadata = NULL, metadata = list())
)
expect_identical(
gi |> mutate(xxx = 1) |> mcols() |> subset(, c(2, 3)),
new("DFrame", rownames = NULL, nrows = 4L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
type = c("cis", "cis", "cis", "trans"), xxx = c(1, 1,
1, 1)))
)
expect_identical(
mutate(gi, xxx = IRanges::RleList(c(1, 2), c(3, 4)))$xxx,
IRanges::RleList(c(1, 2), c(3, 4), c(1, 2), c(3, 4))
Expand Down Expand Up @@ -58,24 +149,87 @@ test_that("dplyr functions work", {
)

## slice
gi |> slice(1:3) |> expect_s4_class("GInteractions")
expect_identical(
gi |> slice(1:3) |> start1(),
c(11L, 11L, 11L)
)
gi |> slice(1:5) |> expect_error()
gi |> slice('error') |> expect_error()

## summarize
ggi |> summarize(m = mean(score))
ggi |> summarise(m = mean(score))
expect_identical(
ggi |> summarize(m = mean(score)),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2), m = c(0.368001408060081, 0.616327087278478
))),
tolerance = 1e-4
)
expect_identical(
ggi |> summarize(m = table(type)),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2), m = new("CompressedIntegerList", elementType = "integer",
elementMetadata = NULL, metadata = list(), unlistData = c(cis = 2L,
trans = 0L, cis = 1L, trans = 1L), partitioning = new("PartitioningByEnd",
end = c(2L, 4L), NAMES = c("1", "2"), elementType = "ANY",
elementMetadata = NULL, metadata = list()))))
)
expect_identical(
ggi |> summarize(m = table(type), n = table(group)),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2), m = new("CompressedIntegerList", elementType = "integer",
elementMetadata = NULL, metadata = list(), unlistData = c(cis = 2L,
trans = 0L, cis = 1L, trans = 1L), partitioning = new("PartitioningByEnd",
end = c(2L, 4L), NAMES = c("1", "2"), elementType = "ANY",
elementMetadata = NULL, metadata = list())),
n = new("CompressedIntegerList", elementType = "integer",
elementMetadata = NULL, metadata = list(), unlistData = c(`1` = 2L,
`2` = 0L, `1` = 0L, `2` = 2L), partitioning = new("PartitioningByEnd",
end = c(2L, 4L), NAMES = c("1", "2"), elementType = "ANY",
elementMetadata = NULL, metadata = list()))))
)

})

test_that("dplyr group functions work", {

group_data(ggi) |> expect_s4_class("DataFrame")
group_keys(ggi) |> expect_s4_class("DataFrame")
expect_identical(group_indices(ggi), S4Vectors::Rle(c(1L, 1L, 2L, 2L)))
expect_identical(group_vars(ggi), "group")
expect_identical(groups(ggi), list(rlang::sym("group")))
expect_identical(group_size(ggi), c(2L, 2L))
expect_identical(n_groups(ggi), 2L)
expect_identical(
group_data(ggi),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2), .rows = new("CompressedIntegerList",
elementType = "integer", elementMetadata = NULL,
metadata = list(), unlistData = 1:4, partitioning = new("PartitioningByEnd",
end = c(2L, 4L), NAMES = NULL, elementType = "ANY",
elementMetadata = NULL, metadata = list()))))
)
expect_identical(
group_keys(ggi),
new("DFrame", rownames = NULL, nrows = 2L, elementType = "ANY",
elementMetadata = NULL, metadata = list(), listData = list(
group = c(1, 2)))
)
expect_identical(
group_indices(ggi),
S4Vectors::Rle(c(1L, 1L, 2L, 2L))
)
expect_identical(
group_vars(ggi),
"group"
)
expect_identical(
groups(ggi),
list(rlang::sym("group"))
)
expect_identical(
group_size(ggi),
c(2L, 2L)
)
expect_identical(
n_groups(ggi),
2L
)

})
Loading

0 comments on commit 2d7728d

Please sign in to comment.