From 1e811c46af59bebe6b883e1d87e3eed1a36cc3cd Mon Sep 17 00:00:00 2001 From: MORIN Edouard <edouard.morin@developpement-durable.gouv.fr> Date: Fri, 28 Feb 2025 16:49:54 +0100 Subject: [PATCH] Passage en version 0.0.0.9010 Introduction lifecycle, correction pour snapshot commentaires. Correction doc. Resolution #31. --- DESCRIPTION | 9 +++-- NAMESPACE | 1 + NEWS.md | 2 ++ R/get_data.R | 4 +-- R/post_data.R | 10 +++++- README.Rmd | 2 +- README.md | 6 ++-- datalibaba.Rproj | 1 + dev/0_prepa_env_test.R | 7 ++-- man/post_data_dbi.Rd | 7 +++- tests/testthat/_snaps/5-get_data.md | 12 ++----- tests/testthat/_snaps/7-commenter_champs.md | 32 +++++++++++++++++ tests/testthat/_snaps/8-commenter_champs.md | 36 ------------------- tests/testthat/test-5-get_data.R | 19 +++++++++- ...ter_champs.R => test-7-commenter_champs.R} | 34 +++++++++--------- ...enter_table.R => test-8-commenter_table.R} | 0 16 files changed, 105 insertions(+), 77 deletions(-) create mode 100644 tests/testthat/_snaps/7-commenter_champs.md delete mode 100644 tests/testthat/_snaps/8-commenter_champs.md rename tests/testthat/{test-8-commenter_champs.R => test-7-commenter_champs.R} (96%) rename tests/testthat/{test-7-commenter_table.R => test-8-commenter_table.R} (100%) diff --git a/DESCRIPTION b/DESCRIPTION index d1391f6..b247759 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: datalibaba Title: Interface De Connexion a Une Base Posgres Postgis -Version: 0.0.0.9009 +Version: 0.0.0.9010 Authors@R: c(person(given = "Maël", family = "Theulière", @@ -33,11 +33,14 @@ Imports: magrittr, tibble, units, - tidyr + tidyr, + lifecycle Suggests: knitr, rmarkdown, - testthat (>= 3.0.0) + testthat (>= 3.0.0), + backports, + lubridate Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index 322fcd2..545021e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ export(poster_data) export(set_schema_rights) export(standard_var_pg) export(transferer_table_comments) +import(lifecycle) importFrom(DBI,Id) importFrom(DBI,dbConnect) importFrom(DBI,dbDisconnect) diff --git a/NEWS.md b/NEWS.md index 6b7029f..f6861a7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,5 @@ +# datalibaba (development version) + # datalibaba 0.0.0.9009 - Gestion des tables non géo dans la fonction `importer_data` : Ajout d'une ligne pour définir explicitement `query_sql_limit` dans la fonction importer_data (cas d'une table non géo). diff --git a/R/get_data.R b/R/get_data.R index 4393608..d24924a 100644 --- a/R/get_data.R +++ b/R/get_data.R @@ -91,8 +91,8 @@ importer_data <- function(table = NULL, schema = NULL, db = "public", server = if ("id_row__" %in% champs$column_name) { - data <- dplyr::arrange(.data = data, .data$id_row__) %>% - dplyr::select(-.data$id_row__) + data <- data %>% dplyr::arrange(id_row__) %>% + dplyr::select(-id_row__) } } diff --git a/R/post_data.R b/R/post_data.R index ea70036..fd10ece 100644 --- a/R/post_data.R +++ b/R/post_data.R @@ -274,18 +274,26 @@ post_data <- function(con, #' Chargement d'un dataset sur le serveur en dbi +#' @description +#' `r lifecycle::badge("deprecated")` +#' +#' Cette fonction a été dépréciée car non fonctionnelle en l'état +#' Elle désormais équivalente à post_data et il est conseillé d'utiliser +#' poster_data #' #' @param con Le connecteur de classe PosgreSQLConnection. #' @param data Un dataframe a importer. #' @param schema Le nom du schema sur le serveur de donnees. #' @param table Le nom de la table sur le serveur de donnees. #' @param overwrite TRUE si on veut ecraser le fichier deja present. +#' @import lifecycle #' @importFrom attempt stop_if stop_if_not #' @importFrom DBI dbWriteTable Id #' @importFrom glue glue #' #' @return NULL, la fonction lance le chargement du dataset present dans l'environnement R et son ecriture sur le serveur #' @export +#' @keywords internal #' post_data_dbi <- function(con = NULL, @@ -293,7 +301,7 @@ post_data_dbi <- function(con = NULL, schema = NULL, table = NULL, overwrite = FALSE) { - .Deprecated("poster_data") + lifecycle::deprecate_warn("0.0.0.9010", "post_data_dbi()", "poster_data()") post_data(con=con, data = data, schema = schema, table = table, overwrite = overwrite) } diff --git a/README.Rmd b/README.Rmd index 2d87b99..f768b77 100644 --- a/README.Rmd +++ b/README.Rmd @@ -17,7 +17,7 @@ knitr::opts_chunk$set( <!-- badges: start --> -mise_en_place_test: [](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/datalibaba/-/commits/mise_en_place_test)[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/datalibaba/-/commits/mise_en_place_test) +[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/datalibaba/-/commits/master)[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/datalibaba/-/commits/master) <!-- badges: end --> {datalibaba} est un ensemble de fonctions visant à faciliter l'alimentation du serveur de données du DREAL <datalab/> Pays de la Loire. diff --git a/README.md b/README.md index 9e1c294..e30c7ab 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ <!-- badges: start --> -mise_en_place_test: [](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/datalibaba/-/commits/mise_en_place_test)[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/datalibaba/-/commits/mise_en_place_test) +[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/datalibaba/-/commits/master)[](https://gitlab-forge.din.developpement-durable.gouv.fr/dreal-pdl/csd/datalibaba/-/commits/master) <!-- badges: end --> {datalibaba} est un ensemble de fonctions visant à faciliter diff --git a/datalibaba.Rproj b/datalibaba.Rproj index 69fafd4..8a479e3 100644 --- a/datalibaba.Rproj +++ b/datalibaba.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: 643276e3-9018-4b1c-85b4-c14054b6bc51 RestoreWorkspace: No SaveWorkspace: No diff --git a/dev/0_prepa_env_test.R b/dev/0_prepa_env_test.R index c62f82d..b0523ac 100644 --- a/dev/0_prepa_env_test.R +++ b/dev/0_prepa_env_test.R @@ -3,12 +3,15 @@ install.packages("odbc") if(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")){ con <- DBI::dbConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres") - if(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="test_datalibaba") != TRUE){ + if(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="test_datalibaba") == TRUE){ + DBI::dbExecute(con, "DROP DATABASE IF EXISTS test_datalibaba;") + } DBI::dbExecute(con, "CREATE DATABASE test_datalibaba WITH OWNER = runner ENCODING = 'UTF8' CONNECTION LIMIT = -1;") + dbDisconnect(con) con <- DBI::dbConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="test_datalibaba") DBI::dbExecute(con, "CREATE EXTENSION postgis;") DBI::dbExecute(con, "CREATE EXTENSION postgis_topology;") - } + #autre dbExecute si besoin de plus #si on veut lire un script sql mesreq.sql, le mettre dans /dev et executer # system(paste0("( echo dev/mesreq.sql ) | psql \"postgresql://runner:",Sys.getenv("BDD_RUNNER_PWD"),"@127.0.0.1:5432/test_datalibaba\"")) diff --git a/man/post_data_dbi.Rd b/man/post_data_dbi.Rd index 41cdd26..b703afe 100644 --- a/man/post_data_dbi.Rd +++ b/man/post_data_dbi.Rd @@ -27,5 +27,10 @@ post_data_dbi( NULL, la fonction lance le chargement du dataset present dans l'environnement R et son ecriture sur le serveur } \description{ -Chargement d'un dataset sur le serveur en dbi +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} + +Cette fonction a été dépréciée car non fonctionnelle en l'état +Elle désormais équivalente à post_data et il est conseillé d'utiliser +poster_data } +\keyword{internal} diff --git a/tests/testthat/_snaps/5-get_data.md b/tests/testthat/_snaps/5-get_data.md index 78ea15e..329e3d1 100644 --- a/tests/testthat/_snaps/5-get_data.md +++ b/tests/testthat/_snaps/5-get_data.md @@ -1,4 +1,4 @@ -# importer_data works +# importer_data no geo works Code importer_data(table = "test_iris", schema = "public", db = "test_datalibaba", @@ -15,14 +15,10 @@ 3 2025-02-27 14:29:32 4 2025-02-27 14:29:32 ---- +# importer_data geo works Code importer_data(table = "nc", schema = "public", db = "test_datalibaba", user = "test") - Condition - Warning: - Use of .data in tidyselect expressions was deprecated in tidyselect 1.2.0. - i Please use `"id_row__"` instead of `.data$id_row__` Output Simple feature collection with 100 features and 14 fields Geometry type: MULTIPOLYGON @@ -58,10 +54,6 @@ Code importer_data(table = "nc", schema = "public", db = "test_datalibaba", user = "test", limit = 4) - Condition - Warning: - Use of .data in tidyselect expressions was deprecated in tidyselect 1.2.0. - i Please use `"id_row__"` instead of `.data$id_row__` Output Simple feature collection with 4 features and 14 fields Geometry type: MULTIPOLYGON diff --git a/tests/testthat/_snaps/7-commenter_champs.md b/tests/testthat/_snaps/7-commenter_champs.md new file mode 100644 index 0000000..c84818e --- /dev/null +++ b/tests/testthat/_snaps/7-commenter_champs.md @@ -0,0 +1,32 @@ +# get_cols_comment works + + Code + get_cols_comment(table = "test_iris", schema = "public", db = "test_datalibaba", + user = "test", server = Sys.getenv("server")) + Output + nom_schema nom_table nom_col commentaire + 1 public test_iris species Commentlibaba ! + +# get_table_comments works + + Code + get_table_comments(table = "retest_iris", schema = "public", db = "test_datalibaba", + user = "test", server = Sys.getenv("server")) + Output + nom_schema nom_table commentaire nom_col + 1 public retest_iris <NA> <NA> + 2 public retest_iris libellé de Sepal.Length cc Sepal.Length + 3 public retest_iris libellé de Sepal.Width cc Sepal.Width + 4 public retest_iris libellé de Petal.Length cc Petal.Length + 5 public retest_iris libellé de Petal.Width cc Petal.Width + 6 public retest_iris libellé de Species cc Species + +# get_table_comment works + + Code + get_table_comment(table = "retest_iris", schema = "public", db = "test_datalibaba", + user = "test", server = Sys.getenv("server")) + Output + nom_schema nom_table commentaire + 1 public retest_iris <NA> + diff --git a/tests/testthat/_snaps/8-commenter_champs.md b/tests/testthat/_snaps/8-commenter_champs.md deleted file mode 100644 index 46dc530..0000000 --- a/tests/testthat/_snaps/8-commenter_champs.md +++ /dev/null @@ -1,36 +0,0 @@ -# get_table_comment works - - Code - get_table_comment(table = "test_iris", schema = "public", db = "test_datalibaba", - user = "test", server = Sys.getenv("server")) - Output - nom_schema nom_table - 1 public test_iris - commentaire - 1 la table de test complete\nCommentaire posté le 27/02/2025, par edouard.morin. - -# get_cols_comment works - - Code - get_cols_comment(table = "test_iris", schema = "public", db = "test_datalibaba", - user = "test", server = Sys.getenv("server")) - Output - nom_schema nom_table nom_col commentaire - 1 public test_iris species Commentlibaba ! - -# get_table_comments works - - Code - get_table_comments(table = "test_iris", schema = "public", db = "test_datalibaba", - user = "test", server = Sys.getenv("server")) - Output - nom_schema nom_table - 1 public test_iris - 2 public test_iris - commentaire - 1 la table de test complete\nCommentaire posté le 27/02/2025, par edouard.morin. - 2 Commentlibaba ! - nom_col - 1 <NA> - 2 species - diff --git a/tests/testthat/test-5-get_data.R b/tests/testthat/test-5-get_data.R index a833384..3b1f138 100644 --- a/tests/testthat/test-5-get_data.R +++ b/tests/testthat/test-5-get_data.R @@ -6,8 +6,9 @@ library(dplyr) library(sf) library(glue) library(backports) +library(withr) -test_that("importer_data works", { +test_that("importer_data no geo works", { if(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")){ Sys.setenv(port="5432") Sys.setenv(server="localhost") @@ -40,6 +41,22 @@ test_that("importer_data works", { "La table de propri\u00e9t\u00e9s des dataframes R est introuvable, un import standard sera r\u00e9alis\u00e9. ") # test basic expect_snapshot(importer_data(table = "test_iris", schema = "public", db = "test_datalibaba", user = "test", limit = 4, ecoSQL = FALSE)) + + }else{ + print(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")) + print('warning: connect_to_db - local database postgresql doesnt work !') + expect_equal(1,1) + } +}) + +test_that("importer_data geo works", { + withr::local_options(lifecycle_verbosity = "quiet") + if(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")){ + Sys.setenv(port="5432") + Sys.setenv(server="localhost") + Sys.setenv(user_test="runner") + Sys.setenv(pwd_test=Sys.getenv("BDD_RUNNER_PWD")) + # test geometry # pas pris en compte dans le coverage... pourquoi ?? expect_snapshot(importer_data(table = "nc", schema = "public", db = "test_datalibaba", user = "test")) expect_snapshot(importer_data(table = "nc", schema = "public", db = "test_datalibaba", user = "test", limit = 4)) diff --git a/tests/testthat/test-8-commenter_champs.R b/tests/testthat/test-7-commenter_champs.R similarity index 96% rename from tests/testthat/test-8-commenter_champs.R rename to tests/testthat/test-7-commenter_champs.R index adc6e4c..ca40328 100644 --- a/tests/testthat/test-8-commenter_champs.R +++ b/tests/testthat/test-7-commenter_champs.R @@ -66,22 +66,21 @@ test_that("post_dico_attr works", { } }) -test_that("get_table_comment works", { +test_that("get_cols_comment works", { if(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")){ Sys.setenv(port="5432") Sys.setenv(server="localhost") Sys.setenv(user_test="runner") Sys.setenv(pwd_test=Sys.getenv("BDD_RUNNER_PWD")) - expect_error(get_table_comment(table = "test_iris", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), + expect_error(get_cols_comment(table = "test_iris", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), "L\'argument schema n\'est pas renseign\u00e9. ") - expect_error(get_table_comment(schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), + expect_error(get_cols_comment(schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), "L\'argument table n\'est pas renseign\u00e9. ") - expect_error(get_table_comment(table = "titi_test", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), + expect_error(get_cols_comment(table = "titi_test", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), "Il n\'y a pas de table titi_test dans le schema public. ") - expect_snapshot(get_table_comment(table = "test_iris", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server"))) - + expect_snapshot(get_cols_comment(table = "test_iris", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server"))) }else{ print(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")) @@ -90,21 +89,14 @@ test_that("get_table_comment works", { } }) -test_that("get_cols_comment works", { +test_that("get_table_comments works", { if(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")){ Sys.setenv(port="5432") Sys.setenv(server="localhost") Sys.setenv(user_test="runner") Sys.setenv(pwd_test=Sys.getenv("BDD_RUNNER_PWD")) - expect_error(get_cols_comment(table = "test_iris", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), - "L\'argument schema n\'est pas renseign\u00e9. ") - expect_error(get_cols_comment(schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), - "L\'argument table n\'est pas renseign\u00e9. ") - expect_error(get_cols_comment(table = "titi_test", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), - "Il n\'y a pas de table titi_test dans le schema public. ") - - expect_snapshot(get_cols_comment(table = "test_iris", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server"))) + expect_snapshot(get_table_comments(table = "retest_iris", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server"))) }else{ print(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")) @@ -113,14 +105,22 @@ test_that("get_cols_comment works", { } }) -test_that("get_table_comments works", { +test_that("get_table_comment works", { if(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")){ Sys.setenv(port="5432") Sys.setenv(server="localhost") Sys.setenv(user_test="runner") Sys.setenv(pwd_test=Sys.getenv("BDD_RUNNER_PWD")) - expect_snapshot(get_table_comments(table = "test_iris", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server"))) + expect_error(get_table_comment(table = "test_iris", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), + "L\'argument schema n\'est pas renseign\u00e9. ") + expect_error(get_table_comment(schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), + "L\'argument table n\'est pas renseign\u00e9. ") + expect_error(get_table_comment(table = "titi_test", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server")), + "Il n\'y a pas de table titi_test dans le schema public. ") + + expect_snapshot(get_table_comment(table = "retest_iris", schema = "public", db = "test_datalibaba", user = "test", server = Sys.getenv("server"))) + }else{ print(DBI::dbCanConnect(RPostgres::Postgres(), host="localhost", user="runner", password=Sys.getenv("BDD_RUNNER_PWD"), dbname="postgres")) diff --git a/tests/testthat/test-7-commenter_table.R b/tests/testthat/test-8-commenter_table.R similarity index 100% rename from tests/testthat/test-7-commenter_table.R rename to tests/testthat/test-8-commenter_table.R -- GitLab