Skip to content

Instantly share code, notes, and snippets.

@DavidPatShuiFong
Last active January 26, 2021 00:51
Show Gist options
  • Select an option

  • Save DavidPatShuiFong/4073a4fd50a7b10c97c1516f1a4d3e0e to your computer and use it in GitHub Desktop.

Select an option

Save DavidPatShuiFong/4073a4fd50a7b10c97c1516f1a4d3e0e to your computer and use it in GitHub Desktop.
Testing behaviour of character to numeric conversion in dbplyr
df <- data.frame(x = c("1.3", "2x"))
# dbplyr::test_register_src(
# "mssql",
# odbc::dbConnect(
# odbc::odbc(), Driver = "ODBC Driver 17 for SQL Server",
# Server = "<server>", UID = "<uid>", PWD = "<password>")
# )
con <- src_test("mssql")
db <- copy_to(con, df, "##test")
out <- db %>% mutate(
integer = as.integer(x),
integer64 = as.integer64(x),
numeric = as.numeric(x),
) %>%
collect()
# testthat tests
expect_identical(out$integer, c(1L, NA))
expect_identical(out$integer64, bit64::as.integer64(c(1L, NA)))
expect_identical(out$numeric, c(1.3, NA))
@DavidPatShuiFong
Copy link
Author

modified version of Gist as originally written by Hadley Wickham. For testing purposes of dbplyr in the tidyverse (tidyverse/dbplyr#555)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment