tests/testthat/test.db.r

foreach::registerDoSEQ()

test_that("initdb can handle an error", {
  skip_if_not_installed("mockery")
  mockery::stub(initdb, "getenv", function(...) stop("my error"))
  expect_error(initdb(build_connection()), "my error")
})


test_that("initdb_postgres calls system with the correct dbname (collaudo)", {
  skip_if_not_installed("mockery")
  mock <- mockery::mock("")
  mockery::stub(initdb_postgres, "system", function(x) {
    expect_true(grepl("grafo_test", x))
  })
  initdb_postgres(env = "collaudo")
})

test_that("initdb_postgres calls system with the correct dbname (prod)", {
  skip_if_not_installed("mockery")
  mock <- mockery::mock("")
  mockery::stub(initdb_postgres, "system", function(x) {
    expect_true(!grepl("grafo_test", x))
  })
  initdb_postgres(env = "prod")
})

test_that("disconnect doesn't call disconnect if env is 'test'", {
  skip_if_not_installed("mockery")
  mk <- mockery::mock(function(...) {
  })
  mockery::stub(disconnect, "dbDisconnect", mk)
  disconnect(NULL, env = "test")
  mockery::expect_called(mk, 0)
})


test_that("disconnect calls disconnect if env is not 'test'", {
  skip_if_not_installed("mockery")
  mk <- mockery::mock(function(...) {
  })
  mockery::stub(disconnect, "DBI::dbDisconnect", mk)
  disconnect(NULL, env = "cippalippa")
  mockery::expect_called(mk, 1)
})


test_that("schema_from_env fails if no schema file is found", {
  skip_if_not_installed("mockery")
  file_exists_mock <- mockery::mock(FALSE)
  mockery::stub(schema_from_env, "file.exists", file_exists_mock)
  mockery::stub(schema_from_env, "error", function(...) {})
  expect_error(schema_from_env(), "Schema file doesn't exists")
})

test_that("initdb calls initdb_postgresd for prod", {
  skip_if_not_installed("mockery")
  initdb_postgres_mock <- mockery::mock(1)
  initdb_sqlite_mock <- mockery::mock(0)

  mockery::stub(initdb, "initdb_postgres", initdb_postgres_mock)
  mockery::stub(initdb, "initdb_sqlite", initdb_sqlite_mock)

  expect_equal(expect_error(initdb(NULL, "prod"), NA), 1)

  mockery::expect_called(initdb_postgres_mock, 1)
  mockery::expect_called(initdb_sqlite_mock, 0)
})

test_that("when initdb_sqlite is in error, calls rollback", {
  skip_if_not_installed("mockery")

  rollback_mock <- mockery::mock(1)
  mockery::stub(initdb_sqlite, "DBI::dbRollback", rollback_mock)
  expect_error(initdb_sqlite(NULL, env = "test"))

  mockery::expect_called(rollback_mock, 1)
})

test_that("build_connection with env != (prod or test) raises an error", {
  skip_if_not_installed("mockery")
  mockery::stub(build_connection, "error", function(...) {})
  expect_error(build_connection(env = "NONESISTO"), "Unknown env")
})

test_that("sqlite_connect fails without RSQLite", {
  skip_if_not_installed("mockery")

  req_mock <- mockery::mock(FALSE)

  mockery::stub(sqlite_connect, "requireNamespace", req_mock)

  expect_error(sqlite_connect(), "Please install RSQLite")
  mockery::expect_called(req_mock, 1)
})

test_that("build_connection calls rcfutils::pg_connect with \"prod\" env", {
  skip_if_not_installed("mockery")
  postgres_connect_mock <- mockery::mock(1)

  mockery::stub(build_connection, "rcfutils::pg_connect", postgres_connect_mock)

  expect_equal(expect_error(build_connection(env = "prod"), NA), 1)

  mockery::expect_called(postgres_connect_mock, 1)
})
giupo/GrafoDB documentation built on Oct. 12, 2022, 9:43 a.m.