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

Sanitise species names with partial matches of invalid terms correctly #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions R/bd_retrieve_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ sanitise_term <- function(searchTerm, searchType) {
pattern = "[^[:alnum:] ]",
x = searchTerm,
ignore.case = TRUE
) |
grepl("(true|false|nil)", searchTerm, ignore.case = TRUE)) {
)) {
stop(
"Illegal character detected! My apologies, but your search can only contain letters, numbers and white-space. Abbreviating genus names (e.g. 'B. subtilis') is not supported. Please spell out your searchTerm ('Bacillus subtilis'), don't use any 'special' characters and try again."
)
} else if (grepl("\\b(true|false|nil)\\b", searchTerm, ignore.case = TRUE)) {
stop(
"Invalid search term! My apologies, but your search can not contain only 'true', 'false' or 'nil."
)
} else if (identical(searchType, "taxon") & grepl("\\s", searchTerm)) {
gsub(pattern = "\\s", replacement = "/", searchTerm)
# expand "Taxon species" to "taxon/species"
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-sanitise_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ test_that("Taxon species is escaped to taxon/species", {

test_that("Normal searchTerm is return without sanitation", {
expect_identical(sanitise_term("Bacillus", "taxon"), "Bacillus")
expect_identical(sanitise_term("Cellulomonas xylanilytica", "taxon"), "Cellulomonas/xylanilytica")
})

test_that("Invalid search terms cause errors", {
expect_error(sanitise_term("true", "taxon"))
expect_error(sanitise_term("false", "taxon"))
expect_error(sanitise_term("nil", "taxon"))
})