-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest-engine.R
More file actions
72 lines (59 loc) · 1.92 KB
/
Copy pathtest-engine.R
File metadata and controls
72 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
skip_if_not_installed("png")
skip_if_not_installed("rsvg")
skip_if_not_installed("V8")
run_query <- function(query, ...) {
opts <- knitr::opts_current$get()
opts$code <- query
extra_opts <- list(...)
if (length(extra_opts) > 0) {
opts[names(extra_opts)] <- extra_opts
}
ggsql_engine(opts)
}
data_file <- "mtcars.csv"
on.exit(unlink(data_file))
write.csv(mtcars, data_file)
test_that("engine can handle a query", {
query <- c(
paste0("SELECT mpg, disp FROM '", data_file, "'"),
"VISUALISE mpg AS x, disp AS y",
"DRAW point"
)
out <- run_query(query, dev = "png")
# We expect path to png file here, since output format for knitr is undetermined
expect_type(out, "character")
expect_length(out, 1L)
})
test_that("engine can handle a query without visualisation statement", {
query <- paste0("SELECT mpg, disp FROM '", data_file, "'")
out <- run_query(query)
expect_snapshot(cat(out))
})
test_that("engine does not return a table when merely creating data", {
skip("Currently SQL queries are not allowed to alter environment")
query <-
"COPY (
SELECT * FROM (VALUES
(5.2, 18.5),
(8.7, 22.3)
) AS t(x, y)
) TO 'data.csv' (HEADER, DELIMITER ',')"
out <- run_query(query)
expect_snapshot(cat(out))
})
test_that("we can knit a mixed-chunk document", {
skip_if_not_installed("withr")
# Create a temporary working directory that will be deleted after this test
dir <- withr::local_tempdir()
withr::local_dir(dir)
# We're copying the test file to working directory so side-effects,
# like creating new figure folders, are contained
basename <- "test_chunks.qmd"
doc <- system.file(basename, package = "ggsql")
in_file <- file.path(dir, basename)
file.copy(doc, in_file)
out_file <- file.path(dir, "test_chunks.md")
out <- knitr::knit(input = in_file, output = out_file, quiet = TRUE)
expect_equal(out_file, out)
expect_snapshot_file(out)
})