Skip to content

Commit

Permalink
Merge 53161af into b6d3308
Browse files Browse the repository at this point in the history
  • Loading branch information
damianooldoni authored Nov 9, 2022
2 parents b6d3308 + 53161af commit c11d6d7
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 40 deletions.
17 changes: 12 additions & 5 deletions tests/testthat/test-add_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ test_that("add_resource() returns error on invalid or empty data frame", {
})

test_that("add_resource() returns error if CSV file cannot be found", {
testthat::skip_if_offline()
p <- example_package
df_csv <- test_path("data/df.csv")
schema <- create_schema(data.frame("col_1" = c(1, 2), "col_2" = c("a", "b")))
Expand All @@ -98,11 +99,6 @@ test_that("add_resource() returns error if CSV file cannot be found", {
"Can't find file at `no_such_file.csv`.",
fixed = TRUE
)
expect_error(
add_resource(p, "new", "http://example.com/no_such_file.csv"),
"Can't find file at `http://example.com/no_such_file.csv`.",
fixed = TRUE
)
expect_error(
add_resource(p, "new", c(df_csv, "no_such_file.csv")),
"Can't find file at `no_such_file.csv`.",
Expand All @@ -118,9 +114,16 @@ test_that("add_resource() returns error if CSV file cannot be found", {
"Can't find file at `no_such_file_1.csv`.",
fixed = TRUE
)
testthat::skip_if_offline()
expect_error(
add_resource(p, "new", "http://example.com/no_such_file.csv"),
"Can't find file at `http://example.com/no_such_file.csv`.",
fixed = TRUE
)
})

test_that("add_resource() returns error on mismatching schema and data", {
testthat::skip_if_offline()
p <- example_package
df <- data.frame("col_1" = c(1, 2), "col_2" = c("a", "b"))
df_csv <- test_path("data/df.csv")
Expand Down Expand Up @@ -155,6 +158,7 @@ test_that("add_resource() returns error on mismatching schema and data", {
})

test_that("add_resource() adds resource", {
testthat::skip_if_offline()
p <- example_package
df <- data.frame("col_1" = c(1, 2), "col_2" = c("a", "b"))
df_csv <- test_path("data/df.csv")
Expand Down Expand Up @@ -183,6 +187,7 @@ test_that("add_resource() adds resource", {
})

test_that("add_resource() uses provided schema (list or path) or creates one", {
testthat::skip_if_offline()
p <- create_package()
df <- data.frame("col_1" = c(1, 2), "col_2" = c("a", "b"))
df_csv <- test_path("data/df.csv")
Expand Down Expand Up @@ -218,6 +223,7 @@ test_that("add_resource() uses provided schema (list or path) or creates one", {

test_that("add_resource() can add resource from data frame, readable by
read_resource()", {
testthat::skip_if_offline()
p <- example_package
df <- data.frame("col_1" = c(1, 2), "col_2" = c("a", "b"))
p <- add_resource(p, "new", df)
Expand All @@ -226,6 +232,7 @@ test_that("add_resource() can add resource from data frame, readable by

test_that("add_resource() can add resource from local, relative, absolute,
remote or compressed CSV file, readable by read_resource()", {
testthat::skip_if_offline()
p <- example_package
schema <- get_schema(p, "deployments")

Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-check_package.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("check_package() returns TRUE on valid Data Package", {
testthat::skip_if_offline()
expect_true(check_package(example_package))
})

Expand Down Expand Up @@ -33,6 +34,7 @@ test_that("check_package() returns error on incorrect Data Package", {
})

test_that("check_package() returns error if resources have no name", {
testthat::skip_if_offline()
p <- example_package
p$resources[[2]]$name <- NULL
expect_error(
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-check_schema.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("check_schema() returns TRUE on valid Table Schema", {
testthat::skip_if_offline()
p <- example_package
# Can't obtain df using read_resource(), because that function uses
# check_schema() (in get_schema()) internally, which is what we want to test
Expand Down
62 changes: 38 additions & 24 deletions tests/testthat/test-read_package.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
test_that("read_package() returns a valid Data Package, whether reading path or
url", {
# Load example package (locally and remotely) and a valid minimal one
test_that("read_package() returns a valid Data Package reading path", {
# Load example package locally and a valid minimal one
p_path <- system.file("extdata", "datapackage.json", package = "frictionless")
p_url <- file.path("https://raw.githubusercontent.com/frictionlessdata/",
"frictionless-r/main/inst/extdata/datapackage.json")
minimal_path <- test_path("data/valid_minimal.json")
p_local <- suppressMessages(read_package(p_path))
p_remote <- suppressMessages(read_package(p_url))
p_minimal <- suppressMessages(read_package(minimal_path))

# Returns a list with required properties
expect_true(check_package(p_local))
expect_true(check_package(p_remote))
expect_true(check_package(p_minimal))

# Package has correct resources
resource_names <- c("deployments", "observations", "media")
expect_identical(resources(p_local), resource_names)
expect_identical(resources(p_remote), resource_names)
expect_identical(resources(p_minimal), resource_names)

# Package has correct "directory", containing root dir of datapackage.json
expect_identical(p_local$directory, gsub("/datapackage.json", "", p_path))
expect_identical(p_remote$directory, gsub("/datapackage.json", "", p_url))
expect_identical(p_minimal$directory, "data")
})

test_that("read_package() returns a valid Data Package reading url", {
testthat::skip_if_offline()
# Load example package remotely
p_url <- file.path("https://raw.githubusercontent.com/frictionlessdata/",
"frictionless-r/main/inst/extdata/datapackage.json")
p_remote <- suppressMessages(read_package(p_url))

# Returns a list with required properties
expect_true(check_package(p_remote))

# Package has correct resources
resource_names <- c("deployments", "observations", "media")
expect_identical(resources(p_remote), resource_names)

# Package has correct "directory", containing root dir of datapackage.json
expect_identical(p_remote$directory, gsub("/datapackage.json", "", p_url))
})

test_that("read_package() shows message about usage norms", {
# Load example package and a minimal valid one a URL in "id"
p_path <- system.file("extdata", "datapackage.json", package = "frictionless")
Expand Down Expand Up @@ -62,17 +73,12 @@ test_that("read_package() returns error on missing file and properties", {
fixed = TRUE
)

# No file
# No file locally
expect_error(
read_package("nofile.json"),
"Can't find file at `nofile.json`",
fixed = TRUE
)
expect_error(
read_package("http://example.com/nofile.json"),
"Can't find file at `http://example.com/nofile.json`.",
fixed = TRUE
)

# Not a json file
expect_error(
Expand Down Expand Up @@ -111,19 +117,27 @@ test_that("read_package() returns error on missing file and properties", {
),
fixed = TRUE
)

# No file remotely
testthat::skip_if_offline()
expect_error(
read_package("http://example.com/nofile.json"),
"Can't find file at `http://example.com/nofile.json`.",
fixed = TRUE
)
})

test_that("read_package() allows descriptor at absolute or relative parent
path", {
relative_path <- "../testthat/data/valid_minimal.json"
expect_true(
check_package(suppressMessages(read_package(relative_path)))
)
absolute_path <- normalizePath("data/valid_minimal.json")
expect_true(
check_package(suppressMessages(read_package(absolute_path)))
)
})
relative_path <- "../testthat/data/valid_minimal.json"
expect_true(
check_package(suppressMessages(read_package(relative_path)))
)
absolute_path <- normalizePath("data/valid_minimal.json")
expect_true(
check_package(suppressMessages(read_package(absolute_path)))
)
})

test_that("read_package() allows YAML descriptor", {
expect_true(
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-read_resource.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("read_resource() returns a tibble", {
testthat::skip_if_offline()
p <- example_package
df <- data.frame("col_1" = c(1, 2), "col_2" = c("a", "b"))
p <- add_resource(p, "new", df)
Expand All @@ -20,6 +21,7 @@ test_that("read_resource() returns error on incorrect Data Package", {
})

test_that("read_resource() returns error on incorrect resource", {
testthat::skip_if_offline()
p <- example_package

# No such resource
Expand Down Expand Up @@ -155,13 +157,15 @@ test_that("read_resource() returns error on incorrect resource", {
})

test_that("read_resource() can read newly added data (ignoring schema)", {
testthat::skip_if_offline()
p <- example_package
df <- data.frame("col_1" = c(1, 2), "col_2" = c("a", "b"))
p <- add_resource(p, "new", df)
expect_identical(read_resource(p, "new"), dplyr::as_tibble(df))
})

test_that("read_resource() can read inline data (ignoring schema)", {
testthat::skip_if_offline()
p <- example_package
expected_resource <- readr::read_csv(
test_path("data/media.csv"),
Expand All @@ -178,6 +182,7 @@ test_that("read_resource() can read inline data (ignoring schema)", {
})

test_that("read_resource() can read local files", {
testthat::skip_if_offline()
p <- example_package
resource <- read_resource(p, "deployments") # local resource, remote package

Expand All @@ -188,6 +193,7 @@ test_that("read_resource() can read local files", {
})

test_that("read_resource() can read remote files", {
testthat::skip_if_offline()
p <- example_package
resource <- read_resource(p, "deployments") # local resource, remote package

Expand All @@ -201,6 +207,7 @@ test_that("read_resource() can read remote files", {

test_that("read_resource() can read safe local and remote Table Schema,
including YAML", {
testthat::skip_if_offline()
p <- example_package
resource <- read_resource(p, "deployments")
p$directory <- "."
Expand Down Expand Up @@ -255,6 +262,7 @@ test_that("read_resource() can read safe local and remote Table Schema,
})

test_that("read_resource() can read safe local and remote CSV dialect", {
testthat::skip_if_offline()
p <- example_package
resource <- read_resource(p, "deployments")
p$directory <- "."
Expand Down Expand Up @@ -307,6 +315,7 @@ test_that("read_resource() can read safe local and remote CSV dialect", {
})

test_that("read_resource() understands CSV dialect", {
testthat::skip_if_offline()
p <- example_package
resource <- read_resource(p, "deployments")

Expand Down Expand Up @@ -336,6 +345,7 @@ test_that("read_resource() understands CSV dialect", {
})

test_that("read_resource() understands missing values", {
testthat::skip_if_offline()
p <- example_package
resource <- read_resource(p, "deployments")

Expand All @@ -350,6 +360,7 @@ test_that("read_resource() understands missing values", {
})

test_that("read_resource() understands encoding", {
testthat::skip_if_offline()
p <- example_package
resource <- read_resource(p, "deployments")

Expand Down Expand Up @@ -438,6 +449,7 @@ test_that("read_resource() handles LF and CRLF line terminator characters", {
#
# read_delim() however only handles 2 line terminator characters (LF and CRLF)
# without explicitly indicating them, so dialect$lineTerminator is ignored
testthat::skip_if_offline()
p <- example_package
resource <- read_resource(p, "deployments") # This file has LF

Expand All @@ -449,6 +461,7 @@ test_that("read_resource() handles LF and CRLF line terminator characters", {
})

test_that("read_resource() can read compressed files", {
testthat::skip_if_offline()
p <- example_package
resource <- read_resource(p, "deployments")

Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-remove_resource.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("remove_resource() returns a valid Data Package", {
testthat::skip_if_offline()
p <- example_package
expect_true(check_package(remove_resource(p, "deployments")))
})
Expand All @@ -15,6 +16,7 @@ test_that("remove_resource() returns error on incorrect Data Package", {
})

test_that("remove_resource() returns error when resource not found", {
testthat::skip_if_offline()
p <- example_package
expect_error(
remove_resource(p, "no_such_resource"),
Expand All @@ -27,6 +29,7 @@ test_that("remove_resource() returns error when resource not found", {
})

test_that("remove_resource() removes resource", {
testthat::skip_if_offline()
p <- example_package

# Remove "deployments", keep "observations" and "media
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-resources.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("resources() returns a character vector of resource names", {
testthat::skip_if_offline()
p <- example_package
expect_identical(resources(p), c("deployments", "observations", "media"))

Expand All @@ -13,6 +14,7 @@ test_that("resources() returns a character vector of resource names", {
})

test_that("resources() returns error if resources have no name", {
testthat::skip_if_offline()
p <- example_package
p$resources[[2]]$name <- NULL
expect_error(
Expand Down
Loading

0 comments on commit c11d6d7

Please sign in to comment.