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

Use included SQL files instead of those included in etn #49

Open
wants to merge 22 commits into
base: live-test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3b64a1e
save script for testing for postman api test mismatches
PietrH Oct 2, 2024
63c9e66
Merge branch 'live-test' into 47-remove-dependency-on-etn
PietrH Oct 2, 2024
4fab76b
replace call to etn with call to self
PietrH Oct 2, 2024
e9d6311
Merge branch 'live-test' into 47-remove-dependency-on-etn
PietrH Oct 2, 2024
bff5658
add helper to check if credentials are of right form
PietrH Oct 2, 2024
264eb3e
Improve error messaging
PietrH Oct 2, 2024
a1ff927
Check if the credentials at least have the right shape
PietrH Oct 4, 2024
30301e2
Provide a more informative error message when creating the database c…
PietrH Oct 4, 2024
c9eaf12
use skip() instead of comments to disable tests: more explicit, harde…
PietrH Oct 4, 2024
f2fb3e5
Check if the credentials have the right shape before trying to connec…
PietrH Oct 4, 2024
db5ca3e
Check the shape of the credentials, not their validity
PietrH Oct 4, 2024
c50f85c
Check if the credentials are provided in the right shape
PietrH Oct 4, 2024
7233da0
Test error messages for failing to connect to the database
PietrH Oct 4, 2024
8fc16d3
devtools::document()
PietrH Oct 22, 2024
bea8a24
usethis::use_tidy_description()
PietrH Oct 22, 2024
eb54961
remove incorrect dubble assignment
PietrH Oct 22, 2024
2b6ef69
Add tests for error message
PietrH Oct 22, 2024
2462479
Merge branch 'live-test' into 47-remove-dependency-on-etn
PietrH Oct 22, 2024
db45606
usethis::use_testthat()
PietrH Oct 22, 2024
75c4d83
split up expectations into tests, add skip for known issue
PietrH Oct 22, 2024
61d66b8
add skip for known issue
PietrH Oct 22, 2024
28b04f0
Merge branch '47-remove-dependency-on-etn' of github.com:inbo/etnserv…
PietrH Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ Authors@R: c(
person("Research Institute for Nature and Forest (INBO)", role = "cph",
comment = "https://www.vlaanderen.be/inbo/en-gb/"),
person("LifeWatch Belgium", role = "fnd",
comment = "https://lifewatch.be"))
Description: Provides API endpoints to the European Tracking Network. Designed
to be used with OpenCPU and the 'etn' package.
comment = "https://lifewatch.be")
)
Description: Provides API endpoints to the European Tracking Network.
Designed to be used with OpenCPU and the 'etn' package.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports:
assertthat,
DBI,
Expand All @@ -28,3 +26,9 @@ Imports:
odbc,
readr,
stringr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
29 changes: 23 additions & 6 deletions R/connect_to_etn.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,28 @@
#' con <- connect_to_etn(username = "my_username", password = "my_password")
#' }
connect_to_etn <- function(username, password) {
connection <- DBI::dbConnect(
odbc::odbc(),
"ETN",
uid = paste("", tolower(username), "", sep = ""),
pwd = paste("", password, "", sep = "")
tryCatch(
{
# Attempt to connect to the database with the provided credentials
connection <- DBI::dbConnect(
odbc::odbc(),
"ETN",
uid = paste("", tolower(username), "", sep = ""),
pwd = paste("", password, "", sep = "")
)
return(connection)
},
error = function(e) {
# When the database connection fails, return the error message and some
# directions to try again. This is usually due to a wrong password, so
# let's include that as a clue in the error message.
stop(glue::glue(e$message,
"Failed to connect to the database.",
"Did you enter the right username/password?",
"Please try again.",
.sep = "\n"),
call. = FALSE)

}
)
return(connection)
}
3 changes: 3 additions & 0 deletions R/get_acoustic_deployments.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ get_acoustic_deployments <- function(
station_name = NULL,
open_only = FALSE) {

# Check if credentials object has right shape
check_credentials(credentials)

# create connection object
connection <-
connect_to_etn(credentials$username, credentials$password)
Expand Down
4 changes: 3 additions & 1 deletion R/get_acoustic_detections.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ get_acoustic_detections <- function(credentials = list(
station_name = NULL,
limit = FALSE) {

# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)
Expand Down Expand Up @@ -215,7 +217,7 @@ get_acoustic_detections <- function(credentials = list(
}

acoustic_tag_id_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "acoustic_tag_id.sql", package = "etn")),
readr::read_file(system.file("sql", "acoustic_tag_id.sql", package = "etnservice")),
.con = connection
)

Expand Down
6 changes: 5 additions & 1 deletion R/get_acoustic_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ get_acoustic_projects <- function(credentials = list(
password = Sys.getenv("pwd")
),
acoustic_project_code = NULL) {

# Check if credentials object has right shape
check_credentials(credentials)

# create connection object
connection <-
connect_to_etn(credentials$username, credentials$password)
Expand All @@ -53,7 +57,7 @@ get_acoustic_projects <- function(credentials = list(
}

project_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "project.sql", package = "etn")),
readr::read_file(system.file("sql", "project.sql", package = "etnservice")),
.con = connection
)

Expand Down
4 changes: 2 additions & 2 deletions R/get_acoustic_receivers.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ get_acoustic_receivers <- function(credentials = list(
}

receiver_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "receiver.sql", package = "etn")),
readr::read_file(system.file("sql", "receiver.sql", package = "etnservice")),
.con = connection
)
acoustic_tag_id_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "acoustic_tag_id.sql", package = "etn")),
readr::read_file(system.file("sql", "acoustic_tag_id.sql", package = "etnservice")),
.con = connection
)

Expand Down
6 changes: 5 additions & 1 deletion R/get_animal_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ get_animal_projects <- function(credentials = list(
password = Sys.getenv("pwd")
),
animal_project_code = NULL) {

# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

Expand All @@ -52,7 +56,7 @@ get_animal_projects <- function(credentials = list(
}

project_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "project.sql", package = "etn")),
readr::read_file(system.file("sql", "project.sql", package = "etnservice")),
.con = connection
)

Expand Down
6 changes: 5 additions & 1 deletion R/get_animals.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ get_animals <- function(credentials = list(
tag_serial_number = NULL,
animal_project_code = NULL,
scientific_name = NULL) {

# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

Expand Down Expand Up @@ -121,7 +125,7 @@ get_animals <- function(credentials = list(
}

tag_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "tag.sql", package = "etn")),
readr::read_file(system.file("sql", "tag.sql", package = "etnservice")),
.con = connection
)

Expand Down
6 changes: 5 additions & 1 deletion R/get_cpod_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ get_cpod_projects <- function(credentials = list(
password = Sys.getenv("pwd")
),
cpod_project_code = NULL) {

# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

Expand All @@ -52,7 +56,7 @@ get_cpod_projects <- function(credentials = list(
}

project_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "project.sql", package = "etn")),
readr::read_file(system.file("sql", "project.sql", package = "etnservice")),
.con = connection
)

Expand Down
5 changes: 4 additions & 1 deletion R/get_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ get_tags <- function(credentials = list(
tag_serial_number = NULL,
acoustic_tag_id = NULL) {

# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

Expand Down Expand Up @@ -116,7 +119,7 @@ get_tags <- function(credentials = list(
}

tag_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "tag.sql", package = "etn")),
readr::read_file(system.file("sql", "tag.sql", package = "etnservice")),
.con = connection
)

Expand Down
2 changes: 1 addition & 1 deletion R/list_acoustic_project_codes.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ list_acoustic_project_codes <- function(credentials = list(
connection <- connect_to_etn(credentials$username, credentials$password)

project_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "project.sql", package = "etn")),
readr::read_file(system.file("sql", "project.sql", package = "etnservice")),
.con = connection
)
query <- glue::glue_sql(
Expand Down
2 changes: 1 addition & 1 deletion R/list_acoustic_tag_ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ list_acoustic_tag_ids <- function(credentials = list(
)) {
connection <- connect_to_etn(credentials$username, credentials$password)
acoustic_tag_id_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "acoustic_tag_id.sql", package = "etn")),
readr::read_file(system.file("sql", "acoustic_tag_id.sql", package = "etnservice")),
.con = connection
)
query <- glue::glue_sql("
Expand Down
2 changes: 1 addition & 1 deletion R/list_animal_project_codes.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ list_animal_project_codes <- function(credentials = list(
connection <- connect_to_etn(credentials$username, credentials$password)

project_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "project.sql", package = "etn")),
readr::read_file(system.file("sql", "project.sql", package = "etnservice")),
.con = connection
)
query <- glue::glue_sql(
Expand Down
5 changes: 4 additions & 1 deletion R/list_cpod_project_codes.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ list_cpod_project_codes <- function(credentials = list(
password = Sys.getenv("pwd")
)) {

# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Check if we can make a connection
check_connection(connection)

project_query <- glue::glue_sql(
readr::read_file(system.file("sql", "project.sql", package = "etn")),
readr::read_file(system.file("sql", "project.sql", package = "etnservice")),
.con = connection
)
query <- glue::glue_sql(
Expand Down
2 changes: 1 addition & 1 deletion R/list_scientific_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ list_scientific_names <- function(credentials = list(
username = Sys.getenv("userid"),
password = Sys.getenv("pwd")
)) {
connection <- connection <- connect_to_etn(credentials$username, credentials$password)
connection <- connect_to_etn(credentials$username, credentials$password)
query <- glue::glue_sql(
"SELECT DISTINCT scientific_name FROM common.animal_release",
.con = connection
Expand Down
43 changes: 43 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,49 @@ get_credentials <-
stringr::str_glue('list(username = "{username}", password = "{password}")')
}

#' Check if the provided credentials are valid.
#'
#' This function checks if the provided credentials contain a "username" and "password" field,
#' and if both fields are of type character. It also verifies that the credentials object has a length of 2.
#'
#' @param credentials A list or data frame containing the credentials to be checked.
#'
#' @return TRUE if the credentials are valid, an error otherwise
#'
#' @examples
#' \dontrun{
#' credentials <- list(username = "john_doe", password = "password123")
#' check_credentials(credentials)
#' #> [1] TRUE
#' }
check_credentials <- function(credentials) {

assertthat::assert_that(
assertthat::has_name(credentials, "username"),
msg = "The credentials need to contain a 'username' field."
)

assertthat::assert_that(
assertthat::has_name(credentials, "password"),
msg = "The credentials need to contain a 'password' field."
)

assertthat::assert_that(
length(credentials) == 2,
msg = "The credentials object should have a length of 2."
)

assertthat::assert_that(
assertthat::is.string(credentials$username)
)

assertthat::assert_that(
assertthat::is.string(credentials$password)
)

return(TRUE)
}

#' Extract the OCPU temp key from a response object
#'
#' When posting a request to the opencpu api service without the json flag, a
Expand Down
1 change: 1 addition & 0 deletions inst/postman-helpers/find-postman-test-mismatch.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ library(httr2)

fn_to_test <- "list_acoustic_tag_ids"


# get reponse -------------------------------------------------------------


Expand Down
25 changes: 25 additions & 0 deletions man/check_credentials.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/tests.html
# * https://testthat.r-lib.org/reference/test_package.html#special-files

library(testthat)
library(etnservice)

test_check("etnservice")
11 changes: 11 additions & 0 deletions tests/testthat/test-connect_to_etn.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ test_that("connect_to_etn() allows to create a connection with passed credential
expect_true(isClass(connection, "PostgreSQL"))
DBI::dbDisconnect(connection)
})

test_that("connect_to_etn() returns a clear error when connecting to db fails",{
expect_error(connect_to_etn("only one argument"),
regexp = "Failed to connect to the database.")
expect_error(connect_to_etn(password = "missing username"),
regexp = "Failed to connect to the database.")
expect_error(connect_to_etn(username = "missing password"),
regexp = "Failed to connect to the database.")
expect_error(connect_to_etn(username = "", password = ""),
regexp = "Failed to connect to the database.")
})
8 changes: 7 additions & 1 deletion tests/testthat/test-get_acoustic_deployments.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ credentials <- list(
test_that("get_acoustic_deployments() returns error for incorrect connection", {
expect_error(
get_acoustic_deployments(credentials = "not_a_credentials"),
"Not a credentials object to database."
"The credentials need to contain a 'username' field",
fixed = TRUE
)
expect_error(
get_acoustic_deployments(credentials = list(username = "not a username",
password = "the wrong password")),
"Failed to connect to the database."
)
})

Expand Down
Loading