tests/testthat/test-dbWriteTable.R

context("dbWriteTable")

# test_that("can't override existing table with default options", {
#   con <- mariadbDefault()
#
#   x <- data.frame(col1 = 1:10, col2 = letters[1:10])
#   dbWriteTable(con, "t1", x, temporary = TRUE)
#   expect_error(dbWriteTable(con, "t1", x), "exists in database")
#   dbDisconnect(con)
# })

# Not generic enough for DBItest
test_that("dbWriteTable() throws error if constraint violated", {
  con <- mariadbDefault()
  on.exit(dbDisconnect(con))

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

  dbWriteTable(con, "t1", x[1:3, ], overwrite = 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")
})

test_that("dbAppendTable() throws error if constraint violated", {
  con <- mariadbDefault()
  on.exit(dbDisconnect(con))

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

  dbWriteTable(con, "t1", x[1:3, ], overwrite = 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")
})

# Available only in MariaDB
test_that("can read file from disk", {
  con <- mariadbDefault()
  on.exit(dbDisconnect(con))

  if (dbGetQuery(con, "SHOW VARIABLES LIKE 'local_infile'")$Value == "OFF") {
    skip("local_infile is set to OFF, can't test LOAD DATA INFILE")
  }

  expected <- data.frame(
    a = c(1:3, NA),
    b = c("x", "y", "z", "E"),
    stringsAsFactors = FALSE
  )

  dbWriteTable(con, "dat", "dat-n.bin", sep = "|", eol = "\n",
    temporary = TRUE, overwrite = TRUE)
  expect_equal(dbReadTable(con, "dat"), expected)
})

test_that("converts NaN and Inf to NULL", {
  con <- mariadbDefault()
  on.exit(dbDisconnect(con))

  x <- data.frame(col1 = c(-Inf, NA, 0, NaN, Inf))

  dbWriteTable(con, "t1", x, overwrite = TRUE, temporary = TRUE)
  expect_equal(dbReadTable(con, "t1"), data.frame(col1 = c(NA, NA, 0, NA, NA)))
})

test_that("write dates prior to 1970 (#232)", {
  con <- mariadbDefault()
  on.exit(dbDisconnect(con))

  x <- data.frame(col1 = as.Date("1970-01-01") + 2:-2)

  dbWriteTable(con, "t1", x, overwrite = TRUE, temporary = TRUE)
  expect_equal(dbReadTable(con, "t1"), x)
})

test_that("writing and reading JSON (#127)", {
  con <- mariadbDefault()
  on.exit(dbDisconnect(con))

  x <- data.frame(col1 = "[1,2,3]", stringsAsFactors = FALSE)

  dbWriteTable(con, "t1", x, field.types = c(col1 = "json"), overwrite = TRUE, temporary = TRUE)
  dbReadTable(con, "t1")

  expect_equal(dbReadTable(con, "t1"), x)
})

Try the RMariaDB package in your browser

Any scripts or data that you put into this service are public.

RMariaDB documentation built on Dec. 20, 2021, 9:06 a.m.