Skip to content

Commit 35be04b

Browse files
committed
fixed tests
Refactor is_range_slice_ungrouped_detected function to accept .data as the first argument for improved clarity. Update corresponding tests to reflect argument order change and ensure correct functionality with various input scenarios.
1 parent ecb1bf9 commit 35be04b

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

R/utilities.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ update_SE_from_tibble <- function(.data_mutated, se, column_belonging = NULL) {
443443
#' @param .by The .by argument from slice
444444
#' @return Logical indicating if this is an ungrouped range slice
445445
#' @noRd
446-
is_range_slice_ungrouped_detected <- function(slice_args, .data, .by= NULL) {
446+
is_range_slice_ungrouped_detected <- function(.data, slice_args, .by= NULL) {
447447
# First check if this is a range slice
448448
is_range_slice <- any(sapply(slice_args, function(x) {
449449
if (is.numeric(x)) {
@@ -459,8 +459,12 @@ is_range_slice_ungrouped_detected <- function(slice_args, .data, .by= NULL) {
459459
}
460460

461461
# If it is a range slice, check if the data is grouped
462-
# For SummarizedExperiment objects, only check .by parameter
463-
is_grouped <- !quo_is_null(.by)
462+
# Check both .by parameter and if the data itself is grouped
463+
is_grouped_by_param <- !quo_is_null(.by)
464+
is_grouped_by_data <- inherits(.data, "grouped_df") ||
465+
(inherits(.data, "SummarizedExperiment") && !is.null(attr(.data, "groups")))
466+
467+
is_grouped <- is_grouped_by_param || is_grouped_by_data
464468

465469
# Return TRUE if ungrouped (should throw error)
466470
!is_grouped
@@ -477,7 +481,7 @@ slice_optimised <- function(.data, ..., .by = NULL, .preserve = FALSE) {
477481
# In this case, we should return a tibble with the equivalent data
478482
# Check if this is an ungrouped range slice
479483
slice_args <- list(...)
480-
if (is_range_slice_ungrouped_detected(slice_args, .data, .by)) {
484+
if (is_range_slice_ungrouped_detected(.data, slice_args, .by)) {
481485
# For range slices on ungrouped data, throw an error with a helpful message
482486
stop("tidySummarizedExperiment says: slice using a range doesn't work on ungrouped data. Please use .by parameter or convert to tibble with as_tibble() before using slice with ranges.")
483487
}
Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,108 @@
11
test_that("is_range_slice_ungrouped_detected works correctly", {
22
local_edition(3)
33

4+
# Load required libraries
5+
library(rlang)
6+
library(tidySummarizedExperiment)
7+
48
# Access the internal function
59
is_range_slice_ungrouped_detected <- tidySummarizedExperiment:::is_range_slice_ungrouped_detected
610

711
# Load test data
8-
library(tidySummarizedExperiment)
912
data(pasilla)
1013

1114
# Test with range slice (1:3) on ungrouped data
12-
expect_true(is_range_slice_ungrouped_detected(list(1:3), pasilla, NULL))
15+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(1:3), quo(NULL)))
1316

1417
# Test with single number on ungrouped data
15-
expect_false(is_range_slice_ungrouped_detected(list(1), pasilla, NULL))
16-
expect_false(is_range_slice_ungrouped_detected(list(5), pasilla, NULL))
18+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(1), quo(NULL)))
19+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(5), quo(NULL)))
1720

1821
# Test with multiple single numbers on ungrouped data
19-
expect_false(is_range_slice_ungrouped_detected(list(1, 2, 3), pasilla, NULL))
22+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(1, 2, 3), quo(NULL)))
2023

2124
# Test with mixed arguments (range and single) on ungrouped data
22-
expect_true(is_range_slice_ungrouped_detected(list(1:3, 5), pasilla, NULL))
23-
expect_true(is_range_slice_ungrouped_detected(list(1, 2:4), pasilla, NULL))
25+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(1:3, 5), quo(NULL)))
26+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(1, 2:4), quo(NULL)))
2427

2528
# Test with character arguments on ungrouped data
26-
expect_false(is_range_slice_ungrouped_detected(list("a", "b"), pasilla, NULL))
29+
expect_false(is_range_slice_ungrouped_detected(pasilla, list("a", "b"), quo(NULL)))
2730

2831
# Test with mixed numeric and character on ungrouped data
29-
expect_false(is_range_slice_ungrouped_detected(list(1, "a"), pasilla, NULL))
30-
expect_true(is_range_slice_ungrouped_detected(list(1:3, "a"), pasilla, NULL))
32+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(1, "a"), quo(NULL)))
33+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(1:3, "a"), quo(NULL)))
3134

3235
# Test with empty list on ungrouped data
33-
expect_false(is_range_slice_ungrouped_detected(list(), pasilla, NULL))
36+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(), quo(NULL)))
3437

3538
# Test with NULL on ungrouped data
36-
expect_false(is_range_slice_ungrouped_detected(list(NULL), pasilla, NULL))
39+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(quo(NULL)), quo(NULL)))
3740

3841
# Test with logical on ungrouped data
39-
expect_false(is_range_slice_ungrouped_detected(list(TRUE, FALSE), pasilla, NULL))
42+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(TRUE, FALSE), quo(NULL)))
4043

4144
# Test with complex range on ungrouped data
42-
expect_true(is_range_slice_ungrouped_detected(list(seq(1, 10, 2)), pasilla, NULL))
43-
expect_true(is_range_slice_ungrouped_detected(list(c(1, 3, 5, 7)), pasilla, NULL))
45+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(seq(1, 10, 2)), quo(NULL)))
46+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(c(1, 3, 5, 7)), quo(NULL)))
4447

4548
# Test with negative ranges on ungrouped data
46-
expect_true(is_range_slice_ungrouped_detected(list(-1:-3), pasilla, NULL))
47-
expect_true(is_range_slice_ungrouped_detected(list(1:-1), pasilla, NULL))
49+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(-1:-3), quo(NULL)))
50+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(1:-1), quo(NULL)))
4851

4952
# Test with decimal ranges on ungrouped data
50-
expect_true(is_range_slice_ungrouped_detected(list(seq(1, 3, 0.5)), pasilla, NULL))
53+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(seq(1, 3, 0.5)), quo(NULL)))
5154
})
5255

5356
test_that("is_range_slice_ungrouped_detected handles edge cases", {
5457
local_edition(3)
5558

59+
# Load required libraries
60+
library(rlang)
61+
library(tidySummarizedExperiment)
62+
5663
# Access the internal function
5764
is_range_slice_ungrouped_detected <- tidySummarizedExperiment:::is_range_slice_ungrouped_detected
5865

5966
# Load test data
60-
library(tidySummarizedExperiment)
6167
data(pasilla)
6268

6369
# Test with very large ranges on ungrouped data
64-
expect_true(is_range_slice_ungrouped_detected(list(1:1000), pasilla, NULL))
70+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(1:1000), quo(NULL)))
6571

6672
# Test with single element vector on ungrouped data (should be FALSE)
67-
expect_false(is_range_slice_ungrouped_detected(list(c(1)), pasilla, NULL))
73+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(c(1)), quo(NULL)))
6874

6975
# Test with named arguments on ungrouped data
70-
expect_true(is_range_slice_ungrouped_detected(list(x = 1:3), pasilla, NULL))
71-
expect_false(is_range_slice_ungrouped_detected(list(x = 1), pasilla, NULL))
76+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(x = 1:3), quo(NULL)))
77+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(x = 1), quo(NULL)))
7278

7379
# Test with nested lists on ungrouped data
74-
expect_false(is_range_slice_ungrouped_detected(list(list(1:3)), pasilla, NULL))
75-
expect_false(is_range_slice_ungrouped_detected(list(list(1)), pasilla, NULL))
80+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(list(1:3)), quo(NULL)))
81+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(list(1)), quo(NULL)))
7682
})
7783

7884
test_that("is_range_slice_ungrouped_detected handles grouping correctly", {
7985
local_edition(3)
8086

87+
# Load required libraries
88+
library(rlang)
89+
library(tidySummarizedExperiment)
90+
8191
# Access the internal function
8292
is_range_slice_ungrouped_detected <- tidySummarizedExperiment:::is_range_slice_ungrouped_detected
8393

8494
# Load test data
85-
library(tidySummarizedExperiment)
8695
data(pasilla)
8796

8897
# Test ungrouped data with range slice
89-
expect_true(is_range_slice_ungrouped_detected(list(1:3), pasilla, NULL))
98+
expect_true(is_range_slice_ungrouped_detected(pasilla, list(1:3), quo(NULL)))
9099

91100
# Test grouped data with range slice
92101
grouped_data <- pasilla |> group_by(.sample)
93-
expect_false(is_range_slice_ungrouped_detected(list(1:3), grouped_data, NULL))
102+
expect_false(is_range_slice_ungrouped_detected(grouped_data, list(1:3), quo(NULL)))
94103

95104
# Test with .by parameter
96-
expect_false(is_range_slice_ungrouped_detected(list(1:3), pasilla, quote(.sample)))
105+
expect_false(is_range_slice_ungrouped_detected(pasilla, list(1:3), quo(.sample)))
97106
})
98107

99108
test_that("slice_optimised handles .by parameter correctly", {
@@ -115,7 +124,7 @@ test_that("slice_optimised handles .by parameter correctly", {
115124

116125
# Test with single slice (should work)
117126
result <- slice_optimised(pasilla, 1, .by = NULL, .preserve = FALSE)
118-
expect_s3_class(result, "SummarizedExperiment")
127+
expect_s4_class(result, "SummarizedExperiment")
119128
})
120129

121130
test_that("slice method works correctly with range detection", {
@@ -133,7 +142,7 @@ test_that("slice method works correctly with range detection", {
133142

134143
# Test single slice on ungrouped data - should work
135144
result <- pasilla |> dplyr::slice(1)
136-
expect_s3_class(result, "SummarizedExperiment")
145+
expect_s4_class(result, "SummarizedExperiment")
137146

138147
# Test range slice on grouped data - should work
139148
result_grouped <- pasilla |>
@@ -144,5 +153,5 @@ test_that("slice method works correctly with range detection", {
144153
# Test range slice with .by parameter - should work
145154
result_by <- pasilla |>
146155
dplyr::slice(1:3, .by = .sample)
147-
expect_s3_class(result_by, "grouped_df")
156+
expect_s4_class(result_by, "SummarizedExperiment")
148157
})

0 commit comments

Comments
 (0)