Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[r] Improve test-case field names for DataFrame #3067

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions apis/r/tests/testthat/helper-test-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,39 @@ create_arrow_schema <- function(foo_first = TRUE) {
bl <- FALSE
if (foo_first) {
arrow::schema(
arrow::field("foo", arrow::int32(), nullable = bl),
arrow::field("int_column", arrow::int32(), nullable = bl),
arrow::field("soma_joinid", arrow::int64(), nullable = bl),
arrow::field("bar", arrow::float64(), nullable = bl),
arrow::field("baz", arrow::large_utf8(), nullable = bl)
arrow::field("float_column", arrow::float64(), nullable = bl),
arrow::field("string_column", arrow::large_utf8(), nullable = bl)
)
} else {
arrow::schema(
arrow::field("soma_joinid", arrow::int64(), nullable = bl),
arrow::field("foo", arrow::int32(), nullable = bl),
arrow::field("bar", arrow::float64(), nullable = bl),
arrow::field("baz", arrow::large_utf8(), nullable = bl)
arrow::field("int_column", arrow::int32(), nullable = bl),
arrow::field("float_column", arrow::float64(), nullable = bl),
arrow::field("string_column", arrow::large_utf8(), nullable = bl)
)
}
}

create_arrow_table <- function(nrows = 10L, factors = FALSE) {
if (isTRUE(factors)) {
return(arrow::arrow_table(
foo = seq.int(nrows) + 1000L,
int_column = seq.int(nrows) + 1000L,
soma_joinid = bit64::seq.integer64(from = 0L, to = nrows - 1L),
bar = seq(nrows) + 0.1,
baz = as.character(seq.int(nrows) + 1000L),
float_column = seq(nrows) + 0.1,
string_column = as.character(seq.int(nrows) + 1000L),
grp = factor(c(
rep_len("lvl1", length.out = floor(nrows / 2)),
rep_len("lvl2", length.out = ceiling(nrows / 2))
))
))
}
arrow::arrow_table(
foo = seq.int(nrows) + 1000L,
int_column = seq.int(nrows) + 1000L,
soma_joinid = bit64::seq.integer64(from = 0L, to = nrows - 1L),
bar = seq(nrows) + 0.1,
baz = as.character(seq.int(nrows) + 1000L)
float_column = seq(nrows) + 0.1,
string_column = as.character(seq.int(nrows) + 1000L)
# schema = create_arrow_schema(false)
)
}
2 changes: 1 addition & 1 deletion apis/r/tests/testthat/helper-test-soma-objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ create_and_populate_soma_dataframe <- function(
uri,
nrows = 10L,
seed = 1,
index_column_names = "foo",
index_column_names = "int_column",
factors = FALSE,
mode = NULL
) {
Expand Down
10 changes: 5 additions & 5 deletions apis/r/tests/testthat/test-Arrow-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ test_that("Validating arrow data type compatibility", {
})

test_that("Validating arrow schema data type compatibility", {
from <- arrow::schema(foo = arrow::int32())
to <- arrow::schema(foo = arrow::int32())
from <- arrow::schema(int_column = arrow::int32())
to <- arrow::schema(int_column = arrow::int32())
expect_true(check_arrow_schema_data_types(from, to))

# Add incompatible fields
from$bar <- arrow::int16()
to$bar <- arrow::float16()
from$float_column <- arrow::int16()
to$float_column <- arrow::float16()
expect_error(
check_arrow_schema_data_types(from, to),
"Schemas are incompatible"
)

# Schemas with different fields
from$baz <- arrow::string()
from$string_column <- arrow::string()
expect_error(
check_arrow_schema_data_types(from, to),
"'from' and 'to' must have the same number of fields"
Expand Down
4 changes: 2 additions & 2 deletions apis/r/tests/testthat/test-EphemeralCollection.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ test_that("Ephemeral Collection mechanics", {
# expect_equal(readback_dataframe$object[], df2$object[])

# Add new dataframe to the collection
expect_error(collection$add_new_dataframe("new_df", create_arrow_schema(), "foo"))
expect_error(collection$add_new_dataframe("new_df", create_arrow_schema(), "int_column"))
expect_no_condition(collection$set(
SOMADataFrameCreate(file.path(uri, 'new_df'), create_arrow_schema(), 'foo'),
SOMADataFrameCreate(file.path(uri, 'new_df'), create_arrow_schema(), 'int_column'),
'new_df'
))
expect_s3_class(df3 <- collection$get("new_df"), 'SOMADataFrame')
Expand Down
12 changes: 6 additions & 6 deletions apis/r/tests/testthat/test-Factory.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ test_that("DataFrame Factory", {
# Check creation of a DF
asch <- create_arrow_schema(foo_first=FALSE)
expect_silent(d2 <- SOMADataFrameCreate(uri, schema = asch))
tbl <- arrow::arrow_table(soma_joinid = 1L:10L, foo = 1L:10L, bar = sqrt(1:10),
baz = letters[1:10], schema = asch)
tbl <- arrow::arrow_table(soma_joinid = 1L:10L, int_column = 1L:10L, float_column = sqrt(1:10),
string_column = letters[1:10], schema = asch)
d2$write(tbl)

# Check opening to read
Expand All @@ -25,10 +25,10 @@ test_that("DataFrame Factory with specified index_column_names", {

# Check creation of a DF
asch <- create_arrow_schema()
expect_error(d2 <- SOMADataFrameCreate(uri, index_column_names = "foo")) # misses schema
expect_silent(d2 <- SOMADataFrameCreate(uri, schema = asch, index_column_names = "foo"))
tbl <- arrow::arrow_table(foo = 1L:10L, soma_joinid = 1L:10L, bar = sqrt(1:10),
baz = letters[1:10], schema = asch)
expect_error(d2 <- SOMADataFrameCreate(uri, index_column_names = "int_column")) # misses schema
expect_silent(d2 <- SOMADataFrameCreate(uri, schema = asch, index_column_names = "int_column"))
tbl <- arrow::arrow_table(int_column = 1L:10L, soma_joinid = 1L:10L, float_column = sqrt(1:10),
string_column = letters[1:10], schema = asch)
d2$write(tbl)

# Check opening to read
Expand Down
18 changes: 9 additions & 9 deletions apis/r/tests/testthat/test-SCEOutgest.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test_that("Load SCE object from ExperimentQuery mechanics", {
expect_identical(dim(net), c(n_var, n_var))
# Test named
expect_warning(obj <- query$to_single_cell_experiment(
obs_index = 'baz',
obs_index = 'string_column',
var_index = 'quux'
))
expect_s4_class(obj, 'SingleCellExperiment')
Expand All @@ -93,7 +93,7 @@ test_that("Load SCE object from ExperimentQuery mechanics", {
)
expect_identical(
colnames(obj),
query$obs('baz')$concat()$GetColumnByName('baz')$as_vector()
query$obs('string_column')$concat()$GetColumnByName('string_column')$as_vector()
)
expect_identical(SingleCellExperiment::mainExpName(obj), 'RNA')
expect_identical(
Expand All @@ -104,7 +104,7 @@ test_that("Load SCE object from ExperimentQuery mechanics", {
query$obs_df$attrnames() %in% names(SingleCellExperiment::colData(obj))
))
expect_true(all(
setdiff(query$obs_df$attrnames(), 'baz') %in% names(SingleCellExperiment::colData(obj))
setdiff(query$obs_df$attrnames(), 'string_column') %in% names(SingleCellExperiment::colData(obj))
))
for (slot in SummarizedExperiment::assayNames(obj)) {
expect_s4_class(mat <- SummarizedExperiment::assay(obj, slot), 'dgTMatrix')
Expand Down Expand Up @@ -161,7 +161,7 @@ test_that("Load SCE object from ExperimentQuery mechanics", {
expect_error(query$to_single_cell_experiment(obs_index = FALSE))
expect_error(query$to_single_cell_experiment(obs_index = NA_character_))
expect_error(query$to_single_cell_experiment(obs_index = 1))
expect_error(query$to_single_cell_experiment(obs_index = c('baz', 'foo')))
expect_error(query$to_single_cell_experiment(obs_index = c('string_column', 'int_column')))
expect_error(query$to_single_cell_experiment(obs_index = 'tomato'))
# Test `var_index` assertions
expect_error(query$to_single_cell_experiment(var_index = FALSE))
Expand Down Expand Up @@ -307,7 +307,7 @@ test_that("Load SCE object from sliced ExperimentQuery", {
expect_identical(SingleCellExperiment::colPairNames(obj), 'connectivities')
expect_identical(SingleCellExperiment::rowPairNames(obj), 'network')
# Test named
expect_no_condition(obj <- query$to_single_cell_experiment(obs_index = 'baz', var_index = 'quux'))
expect_no_condition(obj <- query$to_single_cell_experiment(obs_index = 'string_column', var_index = 'quux'))
expect_s4_class(obj, 'SingleCellExperiment')
expect_identical(dim(obj), c(n_var_slice, n_obs_slice))
expect_identical(
Expand All @@ -316,7 +316,7 @@ test_that("Load SCE object from sliced ExperimentQuery", {
)
expect_identical(
colnames(obj),
query$obs('baz')$concat()$GetColumnByName('baz')$as_vector()
query$obs('string_column')$concat()$GetColumnByName('string_column')$as_vector()
)
expect_identical(SingleCellExperiment::mainExpName(obj), 'RNA')
expect_identical(
Expand Down Expand Up @@ -355,7 +355,7 @@ test_that("Load SCE object from indexed ExperimentQuery", {
on.exit(experiment$close())
# Create the query
obs_value_filter <- paste0(
sprintf("baz == '%s'", obs_label_values),
sprintf("string_column == '%s'", obs_label_values),
collapse = "||"
)
var_value_filter <- paste0(
Expand Down Expand Up @@ -394,7 +394,7 @@ test_that("Load SCE object from indexed ExperimentQuery", {
expect_identical(SingleCellExperiment::rowPairNames(obj), 'network')
# Test named
expect_no_condition(
obj <- query$to_single_cell_experiment(obs_index = 'baz', var_index = 'quux')
obj <- query$to_single_cell_experiment(obs_index = 'string_column', var_index = 'quux')
)
expect_s4_class(obj, 'SingleCellExperiment')
expect_identical(dim(obj), c(n_var_select, n_obs_select))
Expand All @@ -404,7 +404,7 @@ test_that("Load SCE object from indexed ExperimentQuery", {
)
expect_identical(
colnames(obj),
query$obs('baz')$concat()$GetColumnByName('baz')$as_vector()
query$obs('string_column')$concat()$GetColumnByName('string_column')$as_vector()
)
expect_identical(SingleCellExperiment::mainExpName(obj), 'RNA')
expect_identical(
Expand Down
18 changes: 9 additions & 9 deletions apis/r/tests/testthat/test-SOMAAxisQuery.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ test_that("SOMAAxisQuery", {
expect_null(query$value_filter)
expect_null(query$coords)

query <- SOMAAxisQuery$new(value_filter = "foo")
expect_equal(query$value_filter, "foo")
query <- SOMAAxisQuery$new(value_filter = "int_column")
expect_equal(query$value_filter, "int_column")
expect_null(query$coords)

query <- SOMAAxisQuery$new(coords = list(foo = 1L:2L))
query <- SOMAAxisQuery$new(coords = list(int_column = 1L:2L))
expect_null(query$value_filter)
expect_equal(query$coords, list(foo = 1L:2L))
expect_equal(query$coords, list(int_column = 1L:2L))

query <- SOMAAxisQuery$new(value_filter = "foo", coords = list(foo = 1L:2L))
expect_equal(query$value_filter, "foo")
expect_equal(query$coords, list(foo = 1L:2L))
query <- SOMAAxisQuery$new(value_filter = "int_column", coords = list(int_column = 1L:2L))
expect_equal(query$value_filter, "int_column")
expect_equal(query$coords, list(int_column = 1L:2L))

# Bare vector is wrapped in a list
query <- SOMAAxisQuery$new(coords = 1L:2L)
Expand All @@ -31,7 +31,7 @@ test_that("SOMAAxisQuery", {
"'value_filter' must be a scalar character"
)
expect_error(
SOMAAxisQuery$new(value_filter = c("foo", "bar")),
SOMAAxisQuery$new(value_filter = c("int_column", "float_column")),
"'value_filter' must be a scalar character"
)
expect_error(
Expand All @@ -40,7 +40,7 @@ test_that("SOMAAxisQuery", {
)

expect_error(
SOMAAxisQuery$new(coords = list(foo = letters)),
SOMAAxisQuery$new(coords = list(int_column = letters)),
"'coords' must be a list of numeric vectors"
)

Expand Down
24 changes: 12 additions & 12 deletions apis/r/tests/testthat/test-SOMACollection.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_that("SOMACollection basics", {
subcollection$close()

# Add another dataframe to the collection, this time using add_new_dataframe
collection$add_new_dataframe("new_df", create_arrow_schema(), "foo")$close()
collection$add_new_dataframe("new_df", create_arrow_schema(), "int_column")$close()
df3 <- collection$get("new_df")
df3 <- SOMADataFrameOpen(df3$uri)
expect_true(df3$soma_type == "SOMADataFrame")
Expand Down Expand Up @@ -115,12 +115,12 @@ test_that("Platform config and context are respected by add_ methods", {

# Set params in the config and context
cfg <- PlatformConfig$new()
cfg$set("tiledb", "test", "foo", "bar")
cfg$get("tiledb", "test", "foo")
cfg$set("tiledb", "test", "int_column", "float_column")
cfg$get("tiledb", "test", "int_column")

ctx <- SOMATileDBContext$new()
ctx$set("foo", "bar")
ctx$get("foo")
ctx$set("int_column", "float_column")
ctx$get("int_column")

# Create an empty collection
collection <- SOMACollectionCreate(
Expand All @@ -138,18 +138,18 @@ test_that("Platform config and context are respected by add_ methods", {
# Verify the config and context params were inherited
collection$open("READ", internal_use_only = "allowed_use")
expect_equal(
collection$get("sdf1")$platform_config$get("tiledb", "test", "foo"),
"bar"
collection$get("sdf1")$platform_config$get("tiledb", "test", "int_column"),
"float_column"
)
expect_equal(
collection$get("sdf1")$tiledbsoma_ctx$get("foo"),
"bar"
collection$get("sdf1")$tiledbsoma_ctx$get("int_column"),
"float_column"
)
collection$close()

# Method-level config params override instance params
collection$open("WRITE", internal_use_only = "allowed_use")
cfg$set("tiledb", "test", "foo", "baz")
cfg$set("tiledb", "test", "int_column", "string_column")
sdf2 <- collection$add_new_dataframe(
key = "sdf2",
schema = tbl$schema,
Expand All @@ -159,8 +159,8 @@ test_that("Platform config and context are respected by add_ methods", {
sdf2$write(tbl)

expect_equal(
collection$get("sdf2")$platform_config$get("tiledb", "test", "foo"),
"baz"
collection$get("sdf2")$platform_config$get("tiledb", "test", "int_column"),
"string_column"
)
collection$close()
})
Loading
Loading