tests/testthat/test-stw_validate.R

library("tibble")

dict_good <- tribble(
  ~name, ~type,     ~description,    ~levels,
  "x",   "integer", "count",         NULL,
  "y",   "integer", "another count", NULL
)

dict_good <- stw_dict(dict_good)

dict_names_repeated <-
  add_row(dict_good, name = "x", type = "integer", description = "yolo")

dict_names_trivial <-
  add_row(dict_good, name = " ", type = "integer", description = "yolo")

dict_type_not_recognized <-
  add_row(dict_good, name = "z", type = "foo", description = "yolo")

dict_desc_trivial <-
  add_row(dict_good, name = "z", type = "integer", description = "")

meta_good <- diamonds_meta

meta_missing_name <- meta_good
meta_missing_name$name <- NULL

meta_missing_source <- meta_good
meta_missing_source$sources <- NULL

test_that("check works for dictionary", {

  withr::local_options(list(usethis.quiet = TRUE))

  get_valid <- function(x) {
    identical(attr(x, "is_valid"), TRUE)
  }

  # check we get the right result
  expect_true(get_valid(stw_check(dict_good)))
  expect_true(get_valid(stw_check(dict_type_not_recognized)))
  expect_false(get_valid(stw_check(dict_names_repeated)))
  expect_false(get_valid(stw_check(dict_names_trivial)))
  expect_false(get_valid(stw_check(dict_desc_trivial)))


  # TODO: sort out equivalent
  # check we return the item
  # expect_equivalent(stw_check(dict_good), dict_good)
})

test_that("check works for dictionary", {

  withr::local_options(list(usethis.quiet = TRUE))

  get_valid <- function(x) {
    identical(attr(x, "is_valid"), TRUE)
  }

  # check we get the right result
  expect_true(get_valid(stw_check(meta_good)))
  expect_true(get_valid(stw_check(meta_missing_source)))
  expect_false(get_valid(stw_check(meta_missing_name)))


  # check we return the item
  expect_identical(stw_check(meta_good), meta_good)
})

test_that("check side-effects are correct for dictionary", {

  expect_snapshot(stw_check(meta_good, verbosity = "all"))
  expect_snapshot(stw_check(meta_good, verbosity = "info"))
  expect_snapshot(stw_check(meta_good, verbosity = "error"))
  expect_snapshot(stw_check(meta_good, verbosity = "none"))

  expect_snapshot(stw_check(meta_missing_source, verbosity = "all"))
  expect_snapshot(stw_check(meta_missing_source, verbosity = "info"))
  expect_snapshot(stw_check(meta_missing_source, verbosity = "error"))
  expect_snapshot(stw_check(meta_missing_source, verbosity = "none"))

  expect_snapshot(stw_check(meta_missing_name, verbosity = "all"))
  expect_snapshot(stw_check(meta_missing_name, verbosity = "info"))
  expect_snapshot(stw_check(meta_missing_name, verbosity = "error"))
  expect_snapshot(stw_check(meta_missing_name, verbosity = "none"))

})

test_that("check side-effects are correct for meta", {

  expect_snapshot(stw_check(dict_good, verbosity = "all"))
  expect_snapshot(stw_check(dict_good, verbosity = "info"))
  expect_snapshot(stw_check(dict_good, verbosity = "error"))
  expect_snapshot(stw_check(dict_good, verbosity = "none"))

  expect_snapshot(stw_check(dict_type_not_recognized, verbosity = "all"))
  expect_snapshot(stw_check(dict_type_not_recognized, verbosity = "info"))
  expect_snapshot(stw_check(dict_type_not_recognized, verbosity = "error"))
  expect_snapshot(stw_check(dict_type_not_recognized, verbosity = "none"))

  expect_snapshot(stw_check(dict_names_repeated, verbosity = "all"))
  expect_snapshot(stw_check(dict_names_repeated, verbosity = "info"))
  expect_snapshot(stw_check(dict_names_repeated, verbosity = "error"))
  expect_snapshot(stw_check(dict_names_repeated, verbosity = "none"))

})

test_that("validate works", {

  # silent
  expect_silent(stw_validate(dict_good))
  expect_silent(stw_validate(meta_good))

  withr::local_options(list(usethis.quiet = TRUE))

  # returns argument
  # expect_identical(stw_validate(dict_good), dict_good)
  expect_identical(stw_validate(meta_good), meta_good)

  # throws error
  expect_error(stw_validate(dict_names_repeated))
  expect_error(stw_validate(meta_missing_name))


})
ijlyttle/steward documentation built on Jan. 5, 2021, 2:25 p.m.