From 0f3c9ac52b15fad5cefe588bc8e23d97ec30ae9d Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 18:40:48 -0700 Subject: [PATCH 01/15] =?UTF-8?q?=F0=9F=A7=AA=F0=9F=9B=91=20Test=20`add=5F?= =?UTF-8?q?description()`:=20Iris=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testthat/test-add_description.R | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/testthat/test-add_description.R diff --git a/tests/testthat/test-add_description.R b/tests/testthat/test-add_description.R new file mode 100644 index 00000000..25163772 --- /dev/null +++ b/tests/testthat/test-add_description.R @@ -0,0 +1,48 @@ +# 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( + list( + "name" = "Sepal.Length", + "type" = "number", + "description" = descriptions[1] + ), + list( + "name" = "Sepal.Width", + "type" = "number", + "description" = descriptions[2] + ), + list( + "name" = "Petal.Length", + "type" = "number", + "description" = descriptions[3] + ), + list( + "name" = "Petal.Width", + "type" = "number", + "description" = 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) +}) From 6b6b74aad7f40cfa00cd8786b4aeadfd231393b8 Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 19:47:17 -0700 Subject: [PATCH 02/15] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20function?= =?UTF-8?q?=20`.add=5Ffield=5Fbasic=5Fmetadata()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testthat/test-add_description.R | 33 +++++++++++---------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/tests/testthat/test-add_description.R b/tests/testthat/test-add_description.R index 25163772..2d242da8 100644 --- a/tests/testthat/test-add_description.R +++ b/tests/testthat/test-add_description.R @@ -1,3 +1,11 @@ +.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) @@ -15,26 +23,10 @@ test_that("add_description(): Iris example", { "Iris species." ) expected <- list("fields" = list( - list( - "name" = "Sepal.Length", - "type" = "number", - "description" = descriptions[1] - ), - list( - "name" = "Sepal.Width", - "type" = "number", - "description" = descriptions[2] - ), - list( - "name" = "Petal.Length", - "type" = "number", - "description" = descriptions[3] - ), - list( - "name" = "Petal.Width", - "type" = "number", - "description" = descriptions[4] - ), + .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", @@ -46,3 +38,4 @@ test_that("add_description(): Iris example", { add_description(descriptions) expect_equal(obtained, expected) }) + From 444e388fc74b60f7cb6a3c49f74e57939fc9d5dd Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 19:54:39 -0700 Subject: [PATCH 03/15] =?UTF-8?q?=E2=9C=85=20Pass=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/add_description.R | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 R/add_description.R diff --git a/R/add_description.R b/R/add_description.R new file mode 100644 index 00000000..41439876 --- /dev/null +++ b/R/add_description.R @@ -0,0 +1,8 @@ +add_description <- function(old_schema, descriptions) { + schema <- old_schema + schema$fields <- purrr::imap( + schema$fields, + ~ c(.x, description = descriptions[.y]) + ) + return(schema) +} From 4145c2d6a8aaa184d1e364acf97db6a803ac5815 Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 19:55:17 -0700 Subject: [PATCH 04/15] =?UTF-8?q?=F0=9F=A7=AA=F0=9F=9B=91=20Test=20`edit?= =?UTF-8?q?=5Ffields()`:=20Iris=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testthat/test-add_description.R | 30 +++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-add_description.R b/tests/testthat/test-add_description.R index 2d242da8..055edd1c 100644 --- a/tests/testthat/test-add_description.R +++ b/tests/testthat/test-add_description.R @@ -1,9 +1,9 @@ .add_field_basic_metadata <- function(name, type, description) { list( - "name" = name, - "type" = type, - "description" = description - ) + "name" = name, + "type" = type, + "description" = description + ) } # Create a Data Package and add the "iris" data frame as a resource @@ -39,3 +39,25 @@ test_that("add_description(): Iris example", { 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" + ) + names(descriptions) <- field_names + obtained <- iris_schema |> edit_fields( + "description", + descriptions + ) + expect_equal(obtained, expected) +}) From 36983301b8df562d12f5a1b424d19a414803c5bf Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 19:57:41 -0700 Subject: [PATCH 05/15] =?UTF-8?q?=F0=9F=9A=A7=20Add=20empty=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/add_description.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/add_description.R b/R/add_description.R index 41439876..fb1217dd 100644 --- a/R/add_description.R +++ b/R/add_description.R @@ -6,3 +6,6 @@ add_description <- function(old_schema, descriptions) { ) return(schema) } + + +edit_fields <- function(old_schema, metadata_name, metadata) {} From f1f0c077ef1e2f2bb549b51ccd9da401afa0d81c Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 20:00:26 -0700 Subject: [PATCH 06/15] =?UTF-8?q?=F0=9F=AA=B2=E2=9C=85=20Add=20expected?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testthat/test-add_description.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-add_description.R b/tests/testthat/test-add_description.R index 055edd1c..bbde58d2 100644 --- a/tests/testthat/test-add_description.R +++ b/tests/testthat/test-add_description.R @@ -54,6 +54,18 @@ test_that("edit_fields(): Iris example", { "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", From 5b196c3f302bf521859224be42a073476323114f Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 20:05:06 -0700 Subject: [PATCH 07/15] =?UTF-8?q?=F0=9F=A7=AA=F0=9F=9B=91=20Test=20`get=5F?= =?UTF-8?q?field=5Fnames()`:=20Iris=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testthat/test-add_description.R | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/testthat/test-add_description.R b/tests/testthat/test-add_description.R index bbde58d2..a7a94b6f 100644 --- a/tests/testthat/test-add_description.R +++ b/tests/testthat/test-add_description.R @@ -73,3 +73,16 @@ test_that("edit_fields(): Iris example", { ) 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) +}) From 968670b61ea4d0bc3e1fad89dde8d45f69596bfc Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 20:07:46 -0700 Subject: [PATCH 08/15] =?UTF-8?q?=E2=9C=85=20Pass=20test:=20`get=5Ffield?= =?UTF-8?q?=5Fnames()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/add_description.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/add_description.R b/R/add_description.R index fb1217dd..2b0ca3b3 100644 --- a/R/add_description.R +++ b/R/add_description.R @@ -9,3 +9,8 @@ add_description <- function(old_schema, descriptions) { edit_fields <- function(old_schema, metadata_name, metadata) {} + + +get_field_names <- function(old_schema) { + comprehenr::to_vec(for (field in old_schema$fields) field$name) +} From 1f4bef05179430b2e957f00c7ab7897c156c1cfe Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 20:26:32 -0700 Subject: [PATCH 09/15] =?UTF-8?q?=F0=9F=A7=AA=F0=9F=9B=91=20Test=20`edit?= =?UTF-8?q?=5Ffield=5Ffrom=5Fname()`:=20=20Sepal.Length=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testthat/test-add_description.R | 29 ++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-add_description.R b/tests/testthat/test-add_description.R index a7a94b6f..b5bcb34b 100644 --- a/tests/testthat/test-add_description.R +++ b/tests/testthat/test-add_description.R @@ -75,7 +75,34 @@ test_that("edit_fields(): Iris example", { }) -test_that("get_field_names",{ +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", From 03d043995d1ca4b0af03766dcacaf439d429ee19 Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 20:31:22 -0700 Subject: [PATCH 10/15] =?UTF-8?q?=F0=9F=9A=A7=20Add=20empty=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/add_description.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/add_description.R b/R/add_description.R index 2b0ca3b3..1903d9f7 100644 --- a/R/add_description.R +++ b/R/add_description.R @@ -11,6 +11,9 @@ add_description <- function(old_schema, descriptions) { edit_fields <- function(old_schema, metadata_name, metadata) {} +edit_field_from_name <- function(old_schema, field_name, metadata_name, metadata) {} + + get_field_names <- function(old_schema) { comprehenr::to_vec(for (field in old_schema$fields) field$name) } From 4c0ff365970f2b887585b6984c893308b08ce40d Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 21:01:58 -0700 Subject: [PATCH 11/15] =?UTF-8?q?=F0=9F=9A=A7=20REpeat=20interface=20of=20?= =?UTF-8?q?`edit=5Ffields`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testthat/test-add_description.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-add_description.R b/tests/testthat/test-add_description.R index b5bcb34b..2e252e5a 100644 --- a/tests/testthat/test-add_description.R +++ b/tests/testthat/test-add_description.R @@ -97,7 +97,7 @@ test_that("edit_field_from_name()", { "constraints" = list("enum" = c("setosa", "versicolor", "virginica")) ) )) - obtained <- iris_schema |> edit_field_from_name("Sepal.Length", description, "description") + obtained <- iris_schema |> edit_field_from_name("Sepal.Length", "description", description) expect_equal(obtained, expected) }) From f6721bdb98235ed0641e0908603a6fae40d336be Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 21:02:28 -0700 Subject: [PATCH 12/15] =?UTF-8?q?=E2=9C=85=20Pass=20test:=20`edit=5Ffield?= =?UTF-8?q?=5Ffrom=5Fname()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/add_description.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/add_description.R b/R/add_description.R index 1903d9f7..4c35fd40 100644 --- a/R/add_description.R +++ b/R/add_description.R @@ -11,7 +11,14 @@ add_description <- function(old_schema, descriptions) { edit_fields <- function(old_schema, metadata_name, metadata) {} -edit_field_from_name <- function(old_schema, field_name, metadata_name, metadata) {} +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) { From 60a972cdd5019f769390155e6586cd896af32f19 Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 21:14:33 -0700 Subject: [PATCH 13/15] =?UTF-8?q?=E2=9C=85=20Pass=20test:=20`edit=5Ffields?= =?UTF-8?q?()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/add_description.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/add_description.R b/R/add_description.R index 4c35fd40..5c38f24a 100644 --- a/R/add_description.R +++ b/R/add_description.R @@ -8,7 +8,14 @@ add_description <- function(old_schema, descriptions) { } -edit_fields <- function(old_schema, metadata_name, metadata) {} +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) { From 138094e79860a5ed98d658d9af8b24372cdf7343 Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Tue, 19 Sep 2023 21:34:48 -0700 Subject: [PATCH 14/15] =?UTF-8?q?=E2=9E=95=F0=9F=94=A7=20Add=20dependency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 2cfa953b..f133b1a4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,6 +28,7 @@ URL: https://github.com/frictionlessdata/frictionless-r, BugReports: https://github.com/frictionlessdata/frictionless-r/issues Imports: assertthat, + comprehenr, dplyr, glue, httr, From 12ced370749da6c4aa2c7c827f8ee6f7e225c7f6 Mon Sep 17 00:00:00 2001 From: Nepo Rojas Date: Thu, 21 Sep 2023 07:56:55 -0700 Subject: [PATCH 15/15] =?UTF-8?q?=E2=9E=96=20Remove=20dependency=20`compre?= =?UTF-8?q?henr`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DESCRIPTION | 1 - R/add_description.R | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f133b1a4..2cfa953b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,7 +28,6 @@ URL: https://github.com/frictionlessdata/frictionless-r, BugReports: https://github.com/frictionlessdata/frictionless-r/issues Imports: assertthat, - comprehenr, dplyr, glue, httr, diff --git a/R/add_description.R b/R/add_description.R index 5c38f24a..04c5b7db 100644 --- a/R/add_description.R +++ b/R/add_description.R @@ -29,5 +29,5 @@ edit_field_from_name <- function(old_schema, field_name, metadata_name, metadata get_field_names <- function(old_schema) { - comprehenr::to_vec(for (field in old_schema$fields) field$name) + purrr::map_chr(old_schema$fields, ~ .$name) }