Skip to content

Commit

Permalink
Merge pull request #255 from dpprdan/col-in-dbQuoteIdentifier
Browse files Browse the repository at this point in the history
test: Test for quoting columns with `dbQuoteIdentifier()`
  • Loading branch information
aviator-app[bot] authored Apr 1, 2024
2 parents 12006db + b54f98a commit a2c22b1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
3 changes: 0 additions & 3 deletions R/dbQuoteIdentifier_MariaDBConnection_Id.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#' @name mariadb-quoting
#' @usage NULL
dbQuoteIdentifier_MariaDBConnection_Id <- function(conn, x, ...) {
if (length(x@name) >= 3 && any(x@name[[length(x@name) - 2]] != "def")) {
stop('If a "catalog" component is supplied in `Id()`, it must be equal to "def" everywhere.', call. = FALSE)
}
SQL(paste0(dbQuoteIdentifier(conn, x@name), collapse = "."))
}

Expand Down
99 changes: 99 additions & 0 deletions tests/testthat/test-dbQuoteIdentifier.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
test_that("quoting string", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, "Robert'); DROP TABLE Students;--")
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"`Robert'); DROP TABLE Students;--`")
})

test_that("quoting SQL", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, SQL("Robert'); DROP TABLE Students;--"))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"Robert'); DROP TABLE Students;--")
})

test_that("quoting Id", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, Id(schema = 'Robert', table = 'Students;--'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"`Robert`.`Students;--`")
})

test_that("quoting Id with column, #254", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, Id(schema = 'Robert', table = 'Students;--', column = "dormitory"))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"`Robert`.`Students;--`.`dormitory`")
})

test_that("quoting Id with column, unordered", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, Id(column = "dormitory", table = 'Students;--'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"`Students;--`.`dormitory`")
})

test_that("quoting errors", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

expect_error(Id(table = 'Robert', table = 'Students;--'))
})

test_that("unquoting identifier - SQL with quotes", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

expect_equal(dbUnquoteIdentifier(con, SQL('`Students;--`')),
list(Id(table = 'Students;--')))

expect_equal(dbUnquoteIdentifier(con, SQL('`Robert`.`Students;--`')),
list(Id(schema = 'Robert', table = 'Students;--')))

expect_equal(dbUnquoteIdentifier(con, SQL('`Rob``ert`.`Students;--`')),
list(Id(schema = 'Rob`ert', table = 'Students;--')))

expect_equal(dbUnquoteIdentifier(con, SQL('`Rob.ert`.`Students;--`')),
list(Id(schema = 'Rob.ert', table = 'Students;--')))

expect_error(dbUnquoteIdentifier(con, SQL('`Robert.`Students`')),
"^Can't unquote")
})

test_that("unquoting identifier - SQL without quotes", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

expect_equal(dbUnquoteIdentifier(con, SQL('Students')),
list(Id(table = 'Students')))

expect_equal(dbUnquoteIdentifier(con, SQL('Robert.Students')),
list(Id(schema = 'Robert', table = 'Students')))

expect_error(dbUnquoteIdentifier(con, SQL('Rob``ert.Students')),
"^Can't unquote")
})

test_that("unquoting identifier - Id", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

expect_equal(dbUnquoteIdentifier(con,
Id(schema = 'Robert', table = 'Students;--')),
list(Id(schema = 'Robert', table = 'Students;--')))
})
4 changes: 2 additions & 2 deletions tests/testthat/test-dbWriteTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_that("dbWriteTable() throws error if constraint violated", {

x <- data.frame(col1 = 1:10, col2 = letters[1:10])

dbWriteTable(con, "t1", x[1:3, ], overwrite = TRUE)
dbWriteTable(con, "t1", x[1:3, ], overwrite = TRUE, temporary = TRUE)
dbExecute(con, "CREATE UNIQUE INDEX t1_c1_c2_idx ON t1(col1, col2(1))")
expect_error(dbWriteTable(con, "t1", x, append = TRUE), "added 7 rows|Duplicate entry")
})
Expand All @@ -25,7 +25,7 @@ test_that("dbAppendTable() throws error if constraint violated", {

x <- data.frame(col1 = 1:10, col2 = letters[1:10])

dbWriteTable(con, "t1", x[1:3, ], overwrite = TRUE)
dbWriteTable(con, "t1", x[1:3, ], overwrite = TRUE, temporary = TRUE)
dbExecute(con, "CREATE UNIQUE INDEX t1_c1_c2_idx ON t1(col1, col2(1))")
expect_error(dbAppendTable(con, "t1", x), "added 7 rows|Duplicate entry")
})
Expand Down

0 comments on commit a2c22b1

Please sign in to comment.