tests/testthat/test-connection-cleanup.R

context("connection-cleanup")

test_that("connection is cleaned up if reload called when in a transaction", {
  ## In case there are any database connections waiting on garbage collection
  gc()

  path <- build_git_demo("example", "dettl_config.yml")
  con <- prepare_example_postgres_db()
  ## Close this connection as the import creates a new one below
  DBI::dbDisconnect(con)

  import <- dettl(file.path(path, "example"), db_name = "psql_test")
  con <- import$get_connection()

  ## Start a transaction
  import$begin_transaction()
  connections <- get_connections(con)
  connection_no_start <- nrow(connections)

  ## Reload the database connection
  expect_message(import$reload(), "Rolling back active transaction")

  ## No 'idle in transaction' connections exist and number of connections is
  ## constant
  new_connection <- import$get_connection()
  connections <- get_connections(new_connection)
  expect_equal(nrow(connections), connection_no_start)
  expect_false(any(connections$state == "idle in transaction", na.rm = TRUE))

  ## Old connection has been closed
  expect_false(DBI::dbIsValid(con))
})

test_that("error during disconnect is caught", {
  gc()

  path <- build_git_demo("example", "dettl_config.yml")
  con <- prepare_example_postgres_db()

  import <- dettl(file.path(path, "example"), db_name = "psql_test")

  mock_db_disconnect <- mockery::mock(stop("Error closing connection"))

  with_mock("DBI::dbDisconnect" = mock_db_disconnect, {
    expect_message(import$reload(), "While disconnecting from db, ignored error:
Error closing connection")
  })
})
vimc/dettl documentation built on Oct. 6, 2022, 2:13 p.m.