tests/testthat/test_select.R

skip_if_no_db()

# Shared connection object
conn = get_scidb_connection()

RefArray = conn$create_array(dbutils$random_array_name(), "<fa:string, fb:int32, fc:double> [da; db]", .temp = T)
DataContent = data.frame(fa = letters[1:3], fb = 1:3, fc = 3.14 * 1:3, da = 1:3, db = 11:13, stringsAsFactors = F)
conn$array_from_df(DataContent)$change_schema(RefArray)$overwrite(RefArray)$execute()

df_equal = function(actual_df, expected_df) {
  expect_equal(
    actual_df %>% dplyr::arrange(!!! sapply(names(actual_df), as.name)),
    expected_df
  )
}

test_that("select and return data frame with dimensions", {
  withr::local_options(stringsAsFactors = FALSE)

  df_equal(RefArray$select('fa', 'fb')$to_df_all(), DataContent %>% dplyr::select('da', 'db', 'fa', 'fb'))
  df_equal(RefArray$select('fa', 'fb', 'da')$to_df_all(), DataContent %>% dplyr::select('da', 'db', 'fa', 'fb'))
})

test_that("select and return data frame of attrs", {
  withr::local_options(stringsAsFactors = FALSE)

  df_equal(RefArray$select('fa', 'fb')$to_df(), DataContent %>% dplyr::select('fa', 'fb'))
  df_equal(RefArray$select('fa', 'fb', 'da')$to_df(), DataContent %>% dplyr::select('fa', 'fb', 'da'))
  
  cols = c('fa', 'fb')
  df_equal(RefArray$select(cols, 'da')$to_df(), DataContent %>% dplyr::select('fa', 'fb', 'da'))
})

test_that("select nothing", {
  withr::local_options(stringsAsFactors = FALSE)

  # dimensions precede attributs
  df_equal(RefArray$select()$to_df_all(), DataContent %>% dplyr::select('da', 'db', dplyr::everything()))
  df_equal(RefArray$select()$to_df(), DataContent %>% dplyr::select('fa', 'fb', 'fc'))
})

test_that("error cases: select", {
  withr::local_options(stringsAsFactors = FALSE)

  expect_error(RefArray$select("non-existent"), 'non-existent')
  expect_error(RefArray$select("non-existent", 'fa'), 'non-existent')
})

RefArray$remove_array()
Paradigm4/ArrayOpR documentation built on Dec. 11, 2023, 5:59 a.m.