tests/testthat/test-driver-databricks.R

test_that("databricks arguments use camelcase", {
  withr::local_envvar(DATABRICKS_TOKEN = "")

  args <- databricks_args(
    "foo",
    "bar",
    pwd = "pwd",
    uid = "user",
    driver = "driver"
  )
  expect_true(all(is_camel_case(names(args))))
})

test_that("manually supplied arguments override automatic", {
  withr::local_envvar(DATABRICKS_TOKEN = "abc")
  args <- databricks_args("x", "y", driver = "driver")
  expect_equal(args$authMech, 3)

  args <- databricks_args("x", "y", driver = "driver", authMech = 123)
  expect_equal(args$authMech, 123)
})

test_that("fallbacks to driver name", {
  local_mocked_bindings(
    databricks_default_driver_paths = function() character(),
    odbcListDrivers = function() list(name = c("bar", "Databricks"))
  )
  expect_equal(databricks_default_driver(), "Databricks")
})

test_that("errors if can't find driver", {
  local_mocked_bindings(
    databricks_default_driver_paths = function() character(),
    odbcListDrivers = function() list()
  )
  expect_snapshot(databricks_default_driver(), error = TRUE)
})

test_that("databricks host validates inputs", {
  expect_equal(databricks_host("https://my-host.com"), "my-host.com")
  expect_snapshot(databricks_host(""), error = TRUE)
})

test_that("user agent respects envvar", {
  local_mocked_bindings(packageVersion = function(...) "1.0.0")
  expect_equal(databricks_user_agent(), "r-odbc/1.0.0")

  withr::local_envvar(SPARK_CONNECT_USER_AGENT = "my-odbc/1.0.0")
  expect_equal(databricks_user_agent(), "my-odbc/1.0.0")
})

test_that("warns if auth fails", {
  withr::local_envvar(DATABRICKS_TOKEN = "")

  databricks_args1 <- function(...) {
    databricks_args("path", "host", driver = "driver", ...)
  }

  expect_snapshot(. <- databricks_args1())

  expect_silent(databricks_args1(uid = "uid", pwd = "pwd"))
  expect_silent(databricks_args1(authMech = 10))
})

test_that("uid and pwd suppress automated auth", {
  auth <- databricks_auth_args("host", uid = "uid", pwd = "pwd")
  expect_equal(auth, list(uid = "uid", pwd = "pwd", authMech = 3))
})

test_that("must supply both uid and pwd", {
  expect_snapshot(databricks_auth_args("host", uid = "uid"), error = TRUE)
})

test_that("supports PAT in env var", {
  withr::local_envvar(DATABRICKS_TOKEN = "abc")
  expect_equal(databricks_auth_args()$pwd, "abc")
})

test_that("supports OAuth M2M in env var", {
  withr::local_envvar(
    DATABRICKS_TOKEN = "",
    DATABRICKS_CLIENT_ID = "abc",
    DATABRICKS_CLIENT_SECRET = "def"
  )

  auth <- databricks_auth_args()
  expect_equal(auth$auth_client_id, "abc")
  expect_equal(auth$auth_client_secret, "def")
})

test_that("dbConnect method handles httpPath aliases (#787)", {
  local_mocked_bindings(
    databricks_args = function(...) stop("made it"),
    configure_spark = function(...) TRUE
  )

  expect_error(dbConnect(databricks(), HTTPPath = "boop"), "made it")
  expect_error(dbConnect(databricks(), httpPath = "boop"), "made it")
})

test_that("dbConnect method errors informatively re: httpPath (#787)", {
  local_mocked_bindings(configure_spark = function(...) TRUE)

  expect_snapshot(
    error = TRUE,
    dbConnect(databricks(), httpPath = "boop", HTTPPath = "bop")
  )

  expect_snapshot(error = TRUE, dbConnect(databricks(), HTTPPath = 1L))
  expect_snapshot(error = TRUE, dbConnect(databricks(), httpPath = 1L))
})
rstats-db/odbc documentation built on May 15, 2024, 9:45 a.m.