tests/testthat/test_02_dataPreperation.R

context("Prepare data for Data Base")
library(eatGADS)


# load test data (df1, df2, pkList, fkList)
load(file = "c:/Benjamin_Becker/02_Repositories/packages/eatGADS/tests/testthat/helper_dbdata.rda")


### Merging dataframes and labels to one big list
test_that("Merging labels", {
  expect_equal(mergeLabels(df1 = df1, df2 = df2), expected_bigList)
})


### Checking primary and foreign keys
bigList <- mergeLabels(df1 = df1, df2 = df2)

test_that("No errors if called correctly ", {
  expect_silent(addKeys(bigList = bigList, pkList = pkList, fkList = fkList))
})


## expected errors
pkList2 <- list(df1 = "v1",
                df3 = "ID2")
pkList3 <- list(df1 = "v1",
                df2 = "ID2")
fkList2 <- list(df1 = list(References = "lala", Keys = NULL),
                df2 = list(References = "df1", Keys = "ID2"))
fkList3 <- list(df1 = list(References = NULL, Keys = NULL),
                df2 = list(References = NULL, Keys = "ID2"))


test_that("Errors are called correctly ", {
  expect_error(addKeys(bigList = bigList, pkList = pkList2, fkList = fkList), "dfList and pkList have to have identical structure and namings")
  expect_error(addKeys(bigList = bigList, pkList = pkList, fkList = fkList2), "Foreign key defined for first data frame")
  expect_error(addKeys(bigList = bigList, pkList = pkList, fkList = fkList3), "Foreign Key reference for df2 must be exactly one other data frame")

})


# tbd: further tests?


# should this test pass?
#   expect_error(addKeys(bigList = bigList, pkList = pkList3, fkList = fkList), "Foreign Key reference for df2 must be exactly one other data frame")
b-becker/eatGADS documentation built on May 24, 2019, 8:47 p.m.