tests/testthat/test.domain_tests.R

library(testthat)
library(val)
library(readr)
library(dplyr)

context("Domain Validation Functions")

## NB - we encode our test data as a string to avoid the complexity of
## persisting it as a table of some kind.
## Here we read it back in.
## (Turns out read_csv doesn't read from textConnections)
## We use block here to create a temporary context so we don't
## make a mess with our tempnames
test_dm <- block({
    tempname <- tempfile();
    write(test_data$DM, file=tempname);
    val_read_csv(tempname);
});

bad_test_dm <- test_dm %>% select(-DOMAIN);
bad_domain_test_dm <- test_dm %>% mutate(DOMAIN = "dm");

## test state functions are pure
## and thus we can reuse the fresh dm test state
## in all tests.
dm_state <- fresh_state(test_dm, "ok");

## Modify a state's data set. This should never
## happen in real life, thus we define the function here.
validation_dip_data <- function(validation_state,f){
    update_state(validation_state, data=f(validation_state$data));
}

test_that("That the DM domain is known and that the test function works.",
          {
            expect_identical("ok",
                            check_domain_known()(dm_state)$status);
            expect_identical("halted",
                             check_domain_known()(fresh_state(bad_domain_test_dm,"ok"))$status);
          })

test_that("Test that the domain exists test function works.",
          {
              expect_identical("ok",
                        check_domain_presence(dm_state)$status);
              expect_identical("halted",
                               check_domain_presence(fresh_state(bad_test_dm,"ok"))$status)
          })
Vincent-Toups/bacpac_val documentation built on Dec. 2, 2022, 10:20 a.m.