R Under development (unstable) (2025-04-17 r88153 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(datapackage) > > source("helpers.R") > > # Some additional checks; we have functions returning a character in case of > # an error and TRUE otherwise. > expect_istrue <- function(x, ...) { + expect_equal( isTRUE(x), TRUE) + } > expect_nistrue <- function(x, ...) { + expect_equal( isTRUE(x), FALSE) + } > > > # ============================================================================= > # Check is all columns are present and not too many columns are present > > dr <- list( + name = "test", + schema = list( + fields = list( + list(name = "a", type = "integer"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3, b = letters[1:3]) > expect_istrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "exact", + fields = list( + list(name = "a", type = "integer"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3, b = letters[1:3]) > expect_istrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "exact", + fields = list( + list(name = "b", type = "string"), + list(name = "a", type = "integer") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3, b = letters[1:3]) > expect_nistrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "equal", + fields = list( + list(name = "b", type = "string"), + list(name = "a", type = "integer") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3, b = letters[1:3]) > expect_istrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "equal", + fields = list( + list(name = "a", type = "integer"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3) > expect_nistrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "superset", + fields = list( + list(name = "a", type = "integer"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3) > expect_istrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "equal", + fields = list( + list(name = "a", type = "integer"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3, b = letters[1:3], c = letters[1:3]) > expect_nistrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "subset", + fields = list( + list(name = "a", type = "integer"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3, b = letters[1:3], c = letters[1:3]) > expect_istrue(dp_check_dataresource(dta, dr)) > > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "subset", + fields = list( + list(name = "a", type = "integer"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3, c = letters[1:3]) > expect_nistrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "partial", + fields = list( + list(name = "a", type = "integer"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3, c = letters[1:3]) > expect_istrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fieldsMatch = "partial", + fields = list( + list(name = "a", type = "integer"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(d = 1:3, c = letters[1:3]) > expect_nistrue(dp_check_dataresource(dta, dr)) > > # ============================================================================= > # Check if dp_check_fields is called correctly; we will not check all option of > # dp_check_fields as this function is tested seperately > > dr <- list( + name = "test", + schema = list( + fields = list( + list(name = "a", type = "string"), + list(name = "b", type = "string") + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = 1:3, b = letters[1:3]) > expect_nistrue(dp_check_dataresource(dta, dr)) > > dr <- list( + name = "test", + schema = list( + fields = list( + list(name = "a", type = "integer", constraints = list(required = TRUE)), + list(name = "b", type = "string", constraints = list(required = TRUE)) + ) + ) + ) |> structure(class = "dataresource") > dta <- data.frame(a = c(1:3,NA), b = c(letters[1:3], NA)) > expect_nistrue(dp_check_dataresource(dta, dr)) > > expect_error(dp_check_dataresource(dta, dr, throw = TRUE)) > > > > proc.time() user system elapsed 0.17 0.03 0.18