tests/testthat/test.functions.r

foreach::registerDoSEQ()

test_that(" to_data_frame converte correttamente una serie", {
  tt <- stats::ts(runif(10), start = c(1990, 1), frequency = 4)
  df <-  to_data_frame(tt, "TEST")
  expect_true(identicalts(tt, from_data_frame(df)[[df$name]]))
})

test_that(" to_data_frame converte correttamente vettori", {
  tt <- runif(10)
  df <-  to_data_frame(tt, "TEST")
  expect_true(identicalts(tt, from_data_frame(df)$TEST))
})

test_that(" to_data_frame converte correttamente scalari", {
  tt <- 1
  df <-  to_data_frame(tt, "TEST")
  expect_true(identicalts(tt, from_data_frame(df)$TEST))
})


test_that(" to_data_frame converte correttamente missing", {
  tt <- NA
  df <-  to_data_frame(tt, "TEST")
  expect_true(identical(tt, from_data_frame(df)$TEST))
})

test_that(" to_data_frame converte correttamente vettori di missing", {
  tt <- c(NA, NA)
  df <-  to_data_frame(tt, "TEST")
  expect_true(identical(tt, from_data_frame(df)$TEST))
})

test_that("declutter_functions removes correctly functions from it's def", {
  f <- "function(A, B,C) { A = 1 }"
  f <- declutter_function(f)
  expect_equal(f, "A = 1")

  f <- function(A, B, C) {
    A = A + B + C
  }

  f <- declutter_function(f)
  expect_equal(f, "A = A + B + C")
})

test_that(".decluter_functions preserves comments", {
  f <- function(A) {
    # comment here
    A
  }

  f <- declutter_function(f)
  expect_equal(f, "# comment here\n    A")

  f <- "function(A) { # comment here\nA}"
  f <- declutter_function(f)
  expect_equal(f, "# comment here\nA")
})

test_that("assert_dag raises an exception with a cycle in network", {
  g <- igraph::graph.empty(directed = TRUE)
  g <- g + igraph::vertex("A")
  g <- g + igraph::vertex("B")
  g <- g + igraph::edge("A", "B")
  expect_error(assert_dag(g), NA)
  g <- g + igraph::edge("B", "A")
  expect_error(assert_dag(g), "cycles found")
})

test_that("delete deletes a GrafoDB", {
  for (tag in rilasci("test")$tag) delete_graph(tag)
  on.exit({
    for (tag in rilasci("test")$tag) delete_graph(tag)
  })
  g <- GrafoDB("test")g <- GrafoDB("test")
  df <- rilasci("test")
  expect_equal(nrow(df), 1)
})

test_that("delete handles exceptions", {
  skip_if_not_installed("mockery")


  g <- GrafoDB("test")

  mockery::stub(delete_graph, "DBI::dbCommit", function(...) stop("error test"))
  expect_error(delete_graph(g), "error test")
  expect_error(delete_graph("test"), "error test")
})

for (tag in rilasci("test")$tag) delete_graph(tag)


test_that("schema_from_env returns a consistent file", {
  test_file <- schema_from_env("test")
  prod_file <- schema_from_env("prod")
  collaudo_file <- schema_from_env("collaudo")

  expect_equal(basename(test_file), "schema-SQLite.sql")
  expect_equal(basename(prod_file), "schema-PostgreSQL.sql")
  expect_equal(basename(collaudo_file), "schema-PostgreSQL.sql")
})


test_that("funtion_as_string returns a string", {
  func_string <- "A = B + C"
  name <- "A"
  deps <- c("B", "C")
  expect_type(function_as_string(func_string, name, deps), "character")
})

test_that("funtion_as_string returns a string of the function def", {
  func_string <- "A = B + C"
  name <- "A"
  deps <- c("B", "C")
  x <- function_as_string(func_string, name, deps)

  expected <- "proxy <- function(B, C) {
   A = B + C
   A
}"

  expect_equal(x, expected)
})

test_that("funtion_as_string returns a string of the function def, single param", {
  func_string <- "A = B"
  name <- "A"
  deps <- c("B")
  x <- function_as_string(func_string, name, deps)

  expected <- "proxy <- function(B) {
   A = B
   A
}"

  expect_equal(x, expected)
})

test_that("rilasci without params yields all GrafoDB releases", {
  GrafoDB("primo")
  GrafoDB("secondo")

  expect_error(df <- rilasci(), NA)
  expect_equal(df$tag, c("primo", "secondo"))
})
giupo/GrafoDB documentation built on Oct. 12, 2022, 9:43 a.m.