-
Notifications
You must be signed in to change notification settings - Fork 15
/
test-a-recording.R
83 lines (62 loc) · 2.33 KB
/
test-a-recording.R
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
73
74
75
76
77
78
79
80
81
82
83
# This should be run first so that the .dittodb_env environment is clean.
test_that("We can capture db interactions", {
# our state environment is empty
expect_null(.dittodb_env$curr_file_path)
expect_null(.dittodb_env$db_path)
start_db_capturing()
suppressMessages(con <- nycflights13_create_sqlite(verbose = FALSE))
# our state has been updated
expect_identical(.dittodb_env$db_path, file.path(temp_dir, "_memory_"))
# make a query
result <- dbSendQuery(con, "SELECT * FROM airlines")
# our state has been updated
expect_identical(
.dittodb_env$curr_file_path,
file.path(temp_dir, "_memory_/SELECT-b1fffc.R")
)
# get a query
dbFetch(result)
# ensure the file is saved
expect_true(file.exists(file.path(temp_dir, "_memory_/SELECT-b1fffc.R")))
# clear result, disconnect
dbClearResult(result)
dbDisconnect(con)
stop_db_capturing()
})
test_that("We can specify the path when starting capture", {
new_path <- file.path(temp_dir, "start_capture_path")
start_db_capturing(new_path)
suppressMessages(con <- nycflights13_create_sqlite())
# our state has been updated
expect_identical(.dittodb_env$db_path, file.path(new_path, "_memory_"))
dbDisconnect(con)
stop_db_capturing()
})
test_that("dbGetQuery error checking", {
# Check to make sure con was not created before start_db_capturing()
suppressMessages(con <- nycflights13_create_sqlite(verbose = FALSE))
start_db_capturing()
# Testthat sets this to "tests/testthat//_memory_"
# Setting this to NULL so it will mimic a developers experience
.dittodb_env$db_path <- NULL
regex_db <- "^Database capture failed"
error_get_query <- expect_error(
object = dbGetQuery(con, "SELECT * FROM airlines"),
regexp = regex_db
)
testthat_transition(
old = expect_error(dbListTables(con), regex_db),
new = expect_error(expect_error(dbListTables(con)), regex_db)
)
testthat_transition(
old = expect_error(dbListFields(con, "airlines"), regex_db),
new = expect_error(expect_error(dbListFields(con, "airlines")), regex_db)
)
testthat_transition(
old = expect_error(dbExistsTable(con, "airlines"), regex_db),
new = expect_error(expect_error(dbExistsTable(con, "airlines")), regex_db)
)
error_tbl <- expect_error(dplyr::tbl(con, "airlines"), regex_db)
suppressWarnings(dbDisconnect(con))
stop_db_capturing()
})