File: spec-meta-is-valid.R

package info (click to toggle)
r-cran-dbitest 1.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,216 kB
  • sloc: sh: 10; makefile: 2
file content (66 lines) | stat: -rw-r--r-- 2,458 bytes parent folder | download | duplicates (2)
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
#' spec_meta_is_valid
#' @family meta specifications
#' @usage NULL
#' @format NULL
#' @keywords NULL
spec_meta_is_valid <- list(
  is_valid_formals = function() {
    # <establish formals of described functions>
    expect_equal(names(formals(dbIsValid)), c("dbObj", "..."))
  },

  is_valid_connection = function(ctx) {
    #' @return
    #' `dbIsValid()` returns a logical scalar,
    #' `TRUE` if the object specified by `dbObj` is valid,
    #' `FALSE` otherwise.
    con <- connect(ctx)
    #' A [DBIConnection-class] object is initially valid,
    expect_true(expect_visible(dbIsValid(con)))
    expect_error(dbDisconnect(con), NA)
    #' and becomes invalid after disconnecting with [dbDisconnect()].
    expect_false(expect_visible(dbIsValid(con)))
  },
  #
  is_valid_stale_connection = function(ctx, invalid_con) {
    #' For an invalid connection object (e.g., for some drivers if the object
    #' is saved to a file and then restored), the method also returns `FALSE`.
    expect_false(expect_visible(dbIsValid(invalid_con)))
  },
  #
  is_valid_result_query = function(con) {
    query <- trivial_query()
    res <- dbSendQuery(con, query)
    on.exit(dbClearResult(res))
    #' A [DBIResult-class] object is valid after a call to [dbSendQuery()],
    expect_true(expect_visible(dbIsValid(res)))
    expect_error(dbFetch(res), NA)
    #' and stays valid even after all rows have been fetched;
    expect_true(expect_visible(dbIsValid(res)))
    dbClearResult(res)
    on.exit(NULL)
    #' only clearing it with [dbClearResult()] invalidates it.
    expect_false(dbIsValid(res))
  },
  #
  is_valid_result_statement = function(con, table_name) {
    query <- paste0("CREATE TABLE ", table_name, " (a ", dbDataType(con, 1L), ")")
    res <- dbSendStatement(con, query)
    on.exit(dbClearResult(res))
    #' A [DBIResult-class] object is also valid after a call to [dbSendStatement()],
    expect_true(expect_visible(dbIsValid(res)))
    #' and stays valid after querying the number of rows affected;
    expect_error(dbGetRowsAffected(res), NA)
    expect_true(expect_visible(dbIsValid(res)))
    dbClearResult(res)
    on.exit(NULL)
    #' only clearing it with [dbClearResult()] invalidates it.
    expect_false(dbIsValid(res))
  },

  #' If the connection to the database system is dropped (e.g., due to
  #' connectivity problems, server failure, etc.), `dbIsValid()` should return
  #' `FALSE`. This is not tested automatically.

  NULL
)