Skip to content

Commit 1d00ef5

Browse files
committed
+test-path
1 parent 18878b3 commit 1d00ef5

2 files changed

Lines changed: 121 additions & 6 deletions

File tree

R/path.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,29 @@ NULL
2828

2929
#' @export
3030
#' @rdname path
31-
setMethod("path", "SpatialDataArray", \(object, ...)
32-
dirname(ZarrArray::path(data(object))))
31+
#' @importFrom ZarrArray path
32+
setMethod("path", "SpatialDataArray", \(object, ...) {
33+
pa <- tryCatch(path(data(object)), error=\(e) NULL)
34+
if (is.null(pa)) return(NA_character_)
35+
dirname(pa)
36+
})
3337

3438
#' @export
3539
#' @rdname path
36-
setMethod("path", "SpatialDataFrame", \(object, ...)
37-
attr(data(object), "source_path"))
40+
setMethod("path", "SpatialDataFrame", \(object, ...) {
41+
pa <- attr(data(object), "source_path")
42+
if (is.null(pa)) return(NA_character_)
43+
pa
44+
})
3845

3946
#' @export
4047
#' @rdname path
4148
#' @importFrom SingleCellExperiment int_metadata
42-
setMethod("path", "SingleCellExperiment", \(object, ...)
43-
int_metadata(object)$source_path)
49+
setMethod("path", "SingleCellExperiment", \(object, ...) {
50+
pa <- int_metadata(object)$source_path
51+
if (is.null(pa)) return(NA_character_)
52+
pa
53+
})
4454

4555
#' @export
4656
#' @rdname path

tests/testthat/test-path.R

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
zs <- file.path("extdata", "blobs.zarr")
2+
zs <- system.file(zs, package="spatialdataR")
3+
sd <- readSpatialData(zs)
4+
5+
test_that("path,image", {
6+
x <- path(image(sd))
7+
expect_length(x, 1)
8+
expect_is(x, "character")
9+
expect_true(file.info(x)$isdir)
10+
11+
x <- path(SpatialDataImage())
12+
expect_length(x, 1)
13+
expect_true(is.na(x))
14+
expect_is(x, "character")
15+
})
16+
17+
test_that("path,label", {
18+
x <- path(label(sd))
19+
expect_length(x, 1)
20+
expect_is(x, "character")
21+
expect_true(file.info(x)$isdir)
22+
23+
x <- path(SpatialDataLabel())
24+
expect_length(x, 1)
25+
expect_true(is.na(x))
26+
expect_is(x, "character")
27+
})
28+
29+
test_that("path,shape", {
30+
x <- path(shape(sd))
31+
expect_length(x, 1)
32+
expect_is(x, "character")
33+
expect_true(file.info(x)$isdir)
34+
expect_true(endsWith(x, ".parquet"))
35+
36+
x <- path(SpatialDataShape())
37+
expect_length(x, 1)
38+
expect_true(is.na(x))
39+
expect_is(x, "character")
40+
})
41+
42+
test_that("path,point", {
43+
x <- path(point(sd))
44+
expect_length(x, 1)
45+
expect_is(x, "character")
46+
expect_true(file.info(x)$isdir)
47+
expect_true(endsWith(x, ".parquet"))
48+
49+
x <- path(SpatialDataPoint())
50+
expect_length(x, 1)
51+
expect_true(is.na(x))
52+
expect_is(x, "character")
53+
})
54+
55+
test_that("path,table", {
56+
x <- path(table(sd))
57+
expect_length(x, 1)
58+
expect_is(x, "character")
59+
expect_true(file.info(x)$isdir)
60+
61+
x <- path(SpatialDataPoint())
62+
expect_length(x, 1)
63+
expect_true(is.na(x))
64+
expect_is(x, "character")
65+
})
66+
67+
test_that("path,sdata", {
68+
ls <- rownames(sd)
69+
es <- unlist(colnames(sd))
70+
ne <- length(es)
71+
72+
x <- path(sd, simplify=TRUE)
73+
expect_is(x, "data.frame")
74+
expect_equal(ncol(x), 3)
75+
expect_equal(nrow(x), ne)
76+
for (. in seq_along(x))
77+
expect_is(x[[.]], "character")
78+
expect_all_true(x[[1]] %in% ls)
79+
expect_all_true(x[[2]] %in% es)
80+
expect_all_true(file.exists(x[[3]]))
81+
82+
y <- sd
83+
label(y, 2) <- SpatialDataLabel()
84+
y <- path(y, simplify=TRUE)
85+
i <- y[[1]] == "labels" & y[[2]] == labelNames(sd)[2]
86+
expect_identical(i, is.na(y[[3]]))
87+
y[[3]][i] <- x[[3]][i]
88+
expect_identical(x, y)
89+
90+
x <- path(sd, simplify=FALSE)
91+
expect_is(x, "list")
92+
expect_length(unlist(x), ne)
93+
expect_equal(names(x), ls)
94+
for (l in names(x)) {
95+
expect_length(x[[l]], length(sd[[l]]))
96+
expect_equal(names(x[[l]]), names(sd[[l]]))
97+
for (e in names(x[[l]])) {
98+
y <- x[[l]][[e]]
99+
expect_length(y, 1)
100+
expect_is(y, "character")
101+
expect_true(file.exists(y))
102+
expect_equal(y, path(sd[[l]][[e]]))
103+
}
104+
}
105+
})

0 commit comments

Comments
 (0)