From e1788fbdf4252f941685b4e7e21c057eb98e11cc Mon Sep 17 00:00:00 2001 From: John Kerl Date: Thu, 10 Oct 2024 15:23:12 -0400 Subject: [PATCH] tests/testthat/test-query-condition.R --- apis/r/R/QueryCondition.R | 13 +-- apis/r/tests/testthat/test-query-condition.R | 91 ++++++++++++++++++++ libtiledbsoma/test/common.cc | 15 ++-- 3 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 apis/r/tests/testthat/test-query-condition.R diff --git a/apis/r/R/QueryCondition.R b/apis/r/R/QueryCondition.R index fbaff115bc..bc65188136 100644 --- a/apis/r/R/QueryCondition.R +++ b/apis/r/R/QueryCondition.R @@ -55,9 +55,12 @@ parse_query_condition_new <- function( somactx ) { - stopifnot("The schema argument must be an Arrow Schema" = - is(schema, "ArrowObject") && - is(schema, "Schema")) + stopifnot( + "The schema argument must be an Arrow Schema" = + is(schema, "ArrowObject") && + is(schema, "Schema"), + "The argument must be a somactx object" = + is(somactx, "externalptr")) # ---------------------------------------------------------------- # Helpers for walking the parse tree @@ -201,12 +204,12 @@ setClass( # ================================================================ #' Creates a 'tiledbsoma_query_condition' object #' -#' @param ctx (optional) A TileDB Ctx object; if not supplied the default +#' @param somactx (optional) A TileDB Ctx object; if not supplied the default #' context object is retrieved #' @return A 'tiledbsoma_query_condition' object #' @export tiledbsoma_empty_query_condition <- function(somactx) { - stopifnot("The argument must be a ctx object" = is(ctx, "externalptr")) + stopifnot("The argument must be a somactx object" = is(somactx, "externalptr")) ptr <- libtiledbsoma_empty_query_condition(somactx) query_condition <- new("tiledbsoma_query_condition", ptr = ptr, init = FALSE) invisible(query_condition) diff --git a/apis/r/tests/testthat/test-query-condition.R b/apis/r/tests/testthat/test-query-condition.R new file mode 100644 index 0000000000..6979abd7a8 --- /dev/null +++ b/apis/r/tests/testthat/test-query-condition.R @@ -0,0 +1,91 @@ +# 15: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE), +# 16: arrow::field("i32", arrow::int32(), nullable = FALSE), +# 17: arrow::field("f64", arrow::float64(), nullable = FALSE), +# 18: arrow::field("utf8", arrow::large_utf8(), nullable = FALSE) +# 5: arrow::field("foo", arrow::int32(), nullable = FALSE), +# 6: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE), +# 7: arrow::field("bar", arrow::float64(), nullable = FALSE), +# 8: arrow::field("baz", arrow::large_utf8(), nullable = FALSE) +# 6:schema <- arrow::schema(arrow::field("soma_joinid", arrow::int64(), nullable = FALSE), +# 7: arrow::field("int", arrow::int32(), nullable = FALSE), +# 8: arrow::field("A", arrow::dictionary(index_type = arrow::int8(), value_type = arrow::utf8(), ordered = FALSE), nullable=FALSE) +# 10: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE), +# 11: arrow::field("int", arrow::int32(), nullable = FALSE), +# 12: arrow::field("string", arrow::string(), nullable = FALSE), +# 13: arrow::field( +# 8: arrow::field("foo", arrow::int32(), nullable = FALSE), +# 9: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE), +# 10: arrow::field("bar", arrow::float64(), nullable = FALSE), +# 11: arrow::field("baz", arrow::large_utf8(), nullable = FALSE) +# 7: arrow::field("foo", arrow::int32(), nullable = FALSE), +# 8: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE), +# 9: arrow::field("bar", arrow::float64(), nullable = FALSE), +# 10: arrow::field("baz", arrow::large_utf8(), nullable = FALSE) +# 15: arrow::field("soma_joinid", arrow::int64(), nullable = FALSE), +# 16: arrow::field("string_not_enum", arrow::string(), nullable = FALSE), + +test_that("DataFrame Factory", { + skip_if(!extended_tests()) + uri <- tempfile() + if (dir.exists(uri)) unlink(uri, recursive=TRUE) + + ctx <- soma_context() + + sch <- arrow::schema( + arrow::field("soma_joinid", arrow::int64()), + arrow::field("int32", arrow::int32()) + # arrow::field("float_column", arrow::float64(), nullable = bl), + # arrow::field("string_column", arrow::large_utf8(), nullable = bl) + # XXX MORE TO DO + ) + + sdf <- SOMADataFrameCreate(uri, sch, index_column_names = "soma_joinid") + expect_true(sdf$exists()) + expect_true(dir.exists(uri)) + + tbl <- arrow::arrow_table( + soma_joinid = 1L:10L, + int32 = 101L:110L, + schema = sch) + sdf$write(tbl) + sdf$close() + + sdf$reopen("READ") + + cases <- list( + 'soma_joinid > 5' <- function(df) { + expect_true(all(df$soma_joinid == 6:10)) + expect_true(all(df$int32 == 106:110)) + }, + 'soma_joinid == 10' <- function(df) { + expect_true(all(df$soma_joinid == 10)) + expect_true(all(df$int32 == 110)) + }, + 'soma_joinid > 4 && soma_joinid < 8' <- function(df) { + expect_true(all(df$soma_joinid == 4:7)) + }, + 'soma_joinid < 4 || soma_joinid > 8' <- function(df) { + expect_true(all(df$soma_joinid == c(1:3, 9:10))) + } + # louvain == "T cell" + # louvain %in% c("T cell", "B cell") + # louvain %nin% c("T cell", "B cell") + # XXX MORE TO DO + ) + + for (query_string in names(cases)) { + parsed <- do.call( + what = tiledbsoma:::parse_query_condition_new, + args = list(expr=str2lang(query_string), schema=sch, somactx=ctx)) + clib_value_filter <- parsed@ptr + + sr <- sr_setup(uri = sdf$uri, ctx, qc=clib_value_filter) + iter <- TableReadIter$new(sr) + tbl <- iter$read_next() + expect_true(iter$read_complete()) + df <- as.data.frame(tbl) + cases[[query_string]](df) + } + + sdf$close() +}) diff --git a/libtiledbsoma/test/common.cc b/libtiledbsoma/test/common.cc index 4ef735f81c..9f334e7b31 100644 --- a/libtiledbsoma/test/common.cc +++ b/libtiledbsoma/test/common.cc @@ -119,17 +119,12 @@ create_arrow_schema_and_index_columns( // Create index-column info only, no schema involving the attrs ArrowTable create_column_index_info(const std::vector& dim_infos) { for (auto info : dim_infos) { - LOG_DEBUG(fmt::format("create_column_index_info name={}", info.name)); - - LOG_DEBUG(fmt::format( - "create_column_index_info type={}", - tiledb::impl::to_str(info.tiledb_datatype))); - - LOG_DEBUG( - fmt::format("create_column_index_info dim_max={}", info.dim_max)); - LOG_DEBUG(fmt::format( - "create_column_index_info ucd={}", info.use_current_domain)); + "create_column_index_info name={} type={} dim_max={} ucd={}", + info.name, + tiledb::impl::to_str(info.tiledb_datatype), + info.dim_max, + info.use_current_domain)); } auto index_cols_info_schema = _create_index_cols_info_schema(dim_infos);