-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest-read_package.R
More file actions
147 lines (128 loc) · 4.46 KB
/
Copy pathtest-read_package.R
File metadata and controls
147 lines (128 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
test_that("read_package() returns a valid Data Package reading from path", {
# Load example package and a valid minimal one
p_path <- system.file("extdata", "v1", "datapackage.json", package = "frictionless")
minimal_path <- test_path("data/valid_minimal.json")
p_local <- read_package(p_path)
p_minimal <- read_package(minimal_path)
# Returns a list with required properties
expect_no_error(check_package(p_local))
expect_no_error(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_minimal), resource_names)
# Package has correct "directory", containing root dir of datapackage.json
expect_identical(
p_local$directory,
sub("/datapackage.json", "", p_path, fixed = TRUE)
)
expect_identical(p_minimal$directory, "data")
})
test_that("read_package() returns a valid Data Package reading from url", {
skip_if_offline()
# Load example package remotely
p_url <- file.path(
"https://raw.githubusercontent.com/frictionlessdata/frictionless-r/",
"main/inst/extdata/v1/datapackage.json"
)
p_remote <- read_package(p_url)
# Returns a list with required properties
expect_no_error(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,
sub("/datapackage.json", "", p_url, fixed = TRUE)
)
})
test_that("read_package() returns error on missing or invalid file", {
skip_if_offline()
# Incorrect type
expect_error(
read_package(list()),
class = "frictionless_error_file_invalid"
)
expect_error(
read_package(list()),
regexp = "`file` must be a path or URL to a 'datapackage.json' file.",
fixed = TRUE
)
# No file locally
expect_error(
read_package("nofile.json"),
class = "frictionless_error_path_not_found"
)
# Not a json file
expect_error(
read_package(
system.file("extdata", "v1", "deployments.csv", package = "frictionless")
),
regexp = "lexical error: invalid char in json text.",
fixed = FALSE
)
# No file remotely
expect_error(
read_package("https://example.com/nofile.json"),
class = "frictionless_error_url_not_found"
)
})
test_that("read_package() warns if resources are missing", {
# No resources property
expect_warning(
read_package(test_path("data/resources_missing.json")),
class = "frictionless_warning_file_without_resources"
)
expect_warning(
read_package(test_path("data/resources_missing.json")),
regexp = paste(
"`file` 'data/resources_missing.json' should have a resources property",
"containing at least one resource."
),
fixed = TRUE
)
expect_warning(
read_package(test_path("data/resources_missing.json")),
regexp = "Use `add_resource()` to add resources.",
fixed = TRUE
)
# Resources is empty list
expect_warning(
read_package(test_path("data/resources_empty.json")),
class = "frictionless_warning_file_without_resources"
)
})
test_that("read_package() allows descriptor at absolute or relative parent
path", {
relative_path <- "../testthat/data/valid_minimal.json"
expect_no_error(
check_package(read_package(relative_path))
)
absolute_path <- normalizePath("data/valid_minimal.json")
expect_no_error(
check_package(read_package(absolute_path))
)
})
test_that("read_package() allows YAML descriptor", {
expect_no_error(
check_package(read_package(test_path("data/valid_minimal.yml")))
)
})
test_that("read_package() converts JSON null to NULL", {
p_path <- system.file("extdata", "v1", "datapackage.json", package = "frictionless")
p <- read_package(p_path)
# { "image": null } is read as NULL (use chuck() to force error if missing)
expect_null(purrr::chuck(p, "image"))
})
test_that("read_package() with `attach=TRUE`", {
p_path <- system.file("extdata", "v1", "datapackage.json", package = "frictionless")
p <- read_package(p_path, attach = TRUE)
expect_s3_class(p$resources[[1]]$data, "data.frame")
p_url <- file.path(
"https://raw.githubusercontent.com/frictionlessdata/frictionless-r/",
"main/inst/extdata/v1/datapackage.json"
)
p_remote <- read_package(p_url, attach = TRUE)
expect_s3_class(p_remote$resources[[1]]$data, "data.frame")
})