-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest-add_description.R
More file actions
115 lines (106 loc) · 3.02 KB
/
Copy pathtest-add_description.R
File metadata and controls
115 lines (106 loc) · 3.02 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
.add_field_basic_metadata <- function(name, type, description) {
list(
"name" = name,
"type" = type,
"description" = description
)
}
# Create a Data Package and add the "iris" data frame as a resource
my_package <- create_package() %>%
add_resource(resource_name = "iris", data = iris)
iris_schema <- my_package %>%
get_schema("iris")
test_that("add_description(): Iris example", {
descriptions <- c(
"Sepal length in cm.",
"Sepal width in cm.",
"Pedal length in cm.",
"Pedal width in cm.",
"Iris species."
)
expected <- list("fields" = list(
.add_field_basic_metadata("Sepal.Length", "number", descriptions[1]),
.add_field_basic_metadata("Sepal.Width", "number", descriptions[2]),
.add_field_basic_metadata("Petal.Length", "number", descriptions[3]),
.add_field_basic_metadata("Petal.Width", "number", descriptions[4]),
list(
"name" = "Species",
"type" = "string",
"constraints" = list("enum" = c("setosa", "versicolor", "virginica")),
"description" = descriptions[5]
)
))
obtained <- iris_schema |>
add_description(descriptions)
expect_equal(obtained, expected)
})
test_that("edit_fields(): Iris example", {
descriptions <- c(
"Sepal length in cm.",
"Sepal width in cm.",
"Pedal length in cm.",
"Pedal width in cm.",
"Iris species."
)
field_names <- c(
"Sepal.Length",
"Sepal.Width",
"Petal.Length",
"Petal.Width",
"Species"
)
expected <- list("fields" = list(
.add_field_basic_metadata("Sepal.Length", "number", descriptions[1]),
.add_field_basic_metadata("Sepal.Width", "number", descriptions[2]),
.add_field_basic_metadata("Petal.Length", "number", descriptions[3]),
.add_field_basic_metadata("Petal.Width", "number", descriptions[4]),
list(
"name" = "Species",
"type" = "string",
"constraints" = list("enum" = c("setosa", "versicolor", "virginica")),
"description" = descriptions[5]
)
))
names(descriptions) <- field_names
obtained <- iris_schema |> edit_fields(
"description",
descriptions
)
expect_equal(obtained, expected)
})
test_that("edit_field_from_name()", {
description <- "Sepal length in cm."
expected <- list("fields" = list(
.add_field_basic_metadata("Sepal.Length", "number", description),
list(
"name" = "Sepal.Width",
"type" = "number"
),
list(
"name" = "Petal.Length",
"type" = "number"
),
list(
"name" = "Petal.Width",
"type" = "number"
),
list(
"name" = "Species",
"type" = "string",
"constraints" = list("enum" = c("setosa", "versicolor", "virginica"))
)
))
obtained <- iris_schema |> edit_field_from_name("Sepal.Length", "description", description)
expect_equal(obtained, expected)
})
test_that("get_field_names", {
expected_names <- c(
"Sepal.Length",
"Sepal.Width",
"Petal.Length",
"Petal.Width",
"Species"
)
obtained_names <- get_field_names(iris_schema)
expect_equal(expected_names, obtained_names)
})