Skip to content

Commit fddd590

Browse files
committed
test sparsevctrs functions in use
1 parent 28420f6 commit fddd590

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

tests/testthat/_snaps/sparsevctrs.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# to_sparse_data_frame() is used correctly
2+
3+
Code
4+
fit_xy(lm_spec, x = mtcars[, -1], y = mtcars[, 1])
5+
Condition
6+
Error in `to_sparse_data_frame()`:
7+
! x is not sparse
8+
9+
---
10+
11+
Code
12+
fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1])
13+
Condition
14+
Error in `to_sparse_data_frame()`:
15+
! x is spare, and sparse is not allowed
16+
17+
---
18+
19+
Code
20+
fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1])
21+
Condition
22+
Error in `to_sparse_data_frame()`:
23+
! x is spare, and sparse is allowed
24+
25+
# maybe_sparse_matrix() is used correctly
26+
27+
Code
28+
fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1])
29+
Condition
30+
Error in `maybe_sparse_matrix()`:
31+
! sparse vectors detected
32+
33+
---
34+
35+
Code
36+
fit_xy(lm_spec, x = mtcars[, -1], y = mtcars[, 1])
37+
Condition
38+
Error in `maybe_sparse_matrix()`:
39+
! no sparse vectors detected
40+
41+
---
42+
43+
Code
44+
fit_xy(lm_spec, x = as.data.frame(mtcars)[, -1], y = as.data.frame(mtcars)[, 1])
45+
Condition
46+
Error in `maybe_sparse_matrix()`:
47+
! no sparse vectors detected
48+
49+
---
50+
51+
Code
52+
fit_xy(lm_spec, x = tibble::as_tibble(mtcars)[, -1], y = tibble::as_tibble(
53+
mtcars)[, 1])
54+
Condition
55+
Error in `maybe_sparse_matrix()`:
56+
! no sparse vectors detected
57+

tests/testthat/test-sparsevctrs.R

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
test_that("sparse matrices can be passed to `fit_xy()", {
2+
hotel_data <- sparse_hotel_rates()
3+
4+
lm_spec <- linear_reg(penalty = 0) %>%
5+
set_engine("glmnet")
6+
7+
expect_no_error(
8+
lm_fit <- fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1])
9+
)
10+
})
11+
12+
test_that("to_sparse_data_frame() is used correctly", {
13+
local_mocked_bindings(
14+
to_sparse_data_frame = function(x, object) {
15+
if (methods::is(x, "sparseMatrix")) {
16+
if (allow_sparse(object)) {
17+
stop("x is spare, and sparse is allowed")
18+
} else {
19+
stop("x is spare, and sparse is not allowed")
20+
}
21+
}
22+
stop("x is not sparse")
23+
}
24+
)
25+
26+
hotel_data <- sparse_hotel_rates()
27+
28+
lm_spec <- linear_reg(penalty = 0) %>%
29+
set_engine("lm")
30+
31+
expect_snapshot(
32+
error = TRUE,
33+
fit_xy(lm_spec, x = mtcars[, -1], y = mtcars[, 1])
34+
)
35+
expect_snapshot(
36+
error = TRUE,
37+
fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1])
38+
)
39+
40+
lm_spec <- linear_reg(penalty = 0) %>%
41+
set_engine("glmnet")
42+
43+
expect_snapshot(
44+
error = TRUE,
45+
fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1])
46+
)
47+
})
48+
49+
test_that("maybe_sparse_matrix() is used correctly", {
50+
local_mocked_bindings(
51+
maybe_sparse_matrix = function(x) {
52+
if (any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))) {
53+
stop("sparse vectors detected")
54+
} else {
55+
stop("no sparse vectors detected")
56+
}
57+
}
58+
)
59+
60+
hotel_data <- sparse_hotel_rates()
61+
62+
lm_spec <- linear_reg(penalty = 0) %>%
63+
set_engine("glmnet")
64+
65+
expect_snapshot(
66+
error = TRUE,
67+
fit_xy(lm_spec, x = hotel_data[, -1], y = hotel_data[, 1])
68+
)
69+
expect_snapshot(
70+
error = TRUE,
71+
fit_xy(lm_spec, x = mtcars[, -1], y = mtcars[, 1])
72+
)
73+
expect_snapshot(
74+
error = TRUE,
75+
fit_xy(lm_spec, x = as.data.frame(mtcars)[, -1], y = as.data.frame(mtcars)[, 1])
76+
)
77+
expect_snapshot(
78+
error = TRUE,
79+
fit_xy(lm_spec, x = tibble::as_tibble(mtcars)[, -1], y = tibble::as_tibble(mtcars)[, 1])
80+
)
81+
})

0 commit comments

Comments
 (0)