Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create function to edit field properties #153

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
33 changes: 33 additions & 0 deletions R/add_description.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
add_description <- function(old_schema, descriptions) {
schema <- old_schema
schema$fields <- purrr::imap(
schema$fields,
~ c(.x, description = descriptions[.y])
)
return(schema)
}


edit_fields <- function(old_schema, metadata_name, metadata) {
field_names <- names(metadata)
schema <- old_schema
for (name in field_names) {
schema <- edit_field_from_name(schema, name, metadata_name, metadata[[name]])
}
return(schema)
}


edit_field_from_name <- function(old_schema, field_name, metadata_name, metadata) {
field_names <- get_field_names(old_schema)
schema <- old_schema
names(schema$fields) <- field_names
schema$fields[[field_name]][metadata_name] <- metadata
names(schema$fields) <- NULL
return(schema)
}


get_field_names <- function(old_schema) {
purrr::map_chr(old_schema$fields, ~ .$name)
}
115 changes: 115 additions & 0 deletions tests/testthat/test-add_description.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
})