|
1 |
| -context("check if a database table contains a variable") |
2 |
| -describe("check_dbtable_variable()", { |
| 1 | +test_that("checks if table is a single character", { |
| 2 | + skip_on_cran() |
| 3 | + skip_if(as.logical(Sys.getenv("CHECKLIST_OS", FALSE))) |
3 | 4 | junk <- "junk"
|
4 | 5 | table <- "location"
|
5 | 6 | variable <- c("external_code", "description")
|
6 | 7 | error <- TRUE
|
7 |
| - it("checks if table is a single character", { |
8 |
| - skip_on_cran() |
9 |
| - skip_if(isTRUE(Sys.getenv("CHECKLIST_OS", FALSE))) |
10 |
| - channel <- connect_ut_db() |
11 |
| - expect_error( |
12 |
| - check_dbtable_variable( |
13 |
| - table = integer(0), |
14 |
| - variable = variable, |
15 |
| - channel = channel, |
16 |
| - error = error |
17 |
| - ), |
18 |
| - "table is not a string" |
19 |
| - ) |
20 |
| - expect_error( |
21 |
| - check_dbtable_variable( |
22 |
| - table = NA, |
23 |
| - variable = variable, |
24 |
| - channel = channel, |
25 |
| - error = error |
26 |
| - ), |
27 |
| - "table is not a string" |
28 |
| - ) |
29 |
| - expect_error( |
30 |
| - check_dbtable_variable( |
31 |
| - table = character(2), |
32 |
| - variable = variable, |
33 |
| - channel = channel, |
34 |
| - error = error |
35 |
| - ), |
36 |
| - "table is not a string" |
37 |
| - ) |
38 |
| - expect_error( |
39 |
| - check_dbtable_variable( |
40 |
| - table = character(0), |
41 |
| - variable = variable, |
42 |
| - channel = channel, |
43 |
| - error = error |
44 |
| - ), |
45 |
| - "table is not a string" |
46 |
| - ) |
47 |
| - DBI::dbDisconnect(channel) |
48 |
| - }) |
| 8 | + channel <- connect_ut_db() |
| 9 | + expect_error( |
| 10 | + check_dbtable_variable( |
| 11 | + table = integer(0), variable = variable, channel = channel, |
| 12 | + error = error |
| 13 | + ), |
| 14 | + "table is not a string" |
| 15 | + ) |
| 16 | + expect_error( |
| 17 | + check_dbtable_variable( |
| 18 | + table = NA, variable = variable, channel = channel, error = error |
| 19 | + ), |
| 20 | + "table is not a string" |
| 21 | + ) |
| 22 | + expect_error( |
| 23 | + check_dbtable_variable( |
| 24 | + table = character(2), variable = variable, channel = channel, |
| 25 | + error = error |
| 26 | + ), |
| 27 | + "table is not a string" |
| 28 | + ) |
| 29 | + expect_error( |
| 30 | + check_dbtable_variable( |
| 31 | + table = character(0), variable = variable, channel = channel, |
| 32 | + error = error |
| 33 | + ), |
| 34 | + "table is not a string" |
| 35 | + ) |
| 36 | + DBI::dbDisconnect(channel) |
| 37 | +}) |
49 | 38 |
|
50 |
| - it("checks if table exists", { |
51 |
| - skip_on_cran() |
52 |
| - skip_if(isTRUE(Sys.getenv("CHECKLIST_OS", FALSE))) |
53 |
| - channel <- connect_ut_db() |
54 |
| - expect_that( |
55 |
| - check_dbtable_variable( |
56 |
| - table = junk, |
57 |
| - variable = variable, |
58 |
| - channel = channel, |
59 |
| - error = error |
60 |
| - ), |
61 |
| - throws_error( |
62 |
| - sprintf( |
63 |
| - "Table\\(s\\) %s not found in schema public on database", #nolint: nonportable_path_linter, line_length_linter. |
64 |
| - junk |
65 |
| - ) |
66 |
| - ) |
67 |
| - ) |
68 |
| - }) |
| 39 | +test_that("checks if table exists", { |
| 40 | + skip_on_cran() |
| 41 | + skip_if(as.logical(Sys.getenv("CHECKLIST_OS", FALSE))) |
| 42 | + junk <- "junk" |
| 43 | + table <- "location" |
| 44 | + variable <- c("external_code", "description") |
| 45 | + error <- TRUE |
| 46 | + channel <- connect_ut_db() |
| 47 | + expect_error( |
| 48 | + check_dbtable_variable( |
| 49 | + table = junk, variable = variable, channel = channel, error = error |
| 50 | + ), |
| 51 | + sprintf("Table\\(s\\) %s not found in schema public on database", junk) |
| 52 | + ) |
| 53 | +}) |
69 | 54 |
|
70 |
| - it("checks if channel is on ODBC connection", { |
71 |
| - skip_on_cran() |
72 |
| - skip_if(isTRUE(Sys.getenv("CHECKLIST_OS", FALSE))) |
73 |
| - channel <- connect_ut_db() |
74 |
| - expect_that( |
75 |
| - check_dbtable_variable( |
76 |
| - table = table, |
77 |
| - variable = variable, |
78 |
| - channel = junk, |
79 |
| - error = error |
80 |
| - ), |
81 |
| - throws_error("channel does not inherit from class DBIConnection") |
82 |
| - ) |
83 |
| - DBI::dbDisconnect(channel) |
84 |
| - }) |
| 55 | +test_that("checks if channel is on ODBC connection", { |
| 56 | + skip_on_cran() |
| 57 | + skip_if(as.logical(Sys.getenv("CHECKLIST_OS", FALSE))) |
| 58 | + junk <- "junk" |
| 59 | + table <- "location" |
| 60 | + variable <- c("external_code", "description") |
| 61 | + error <- TRUE |
| 62 | + channel <- connect_ut_db() |
| 63 | + expect_error( |
| 64 | + check_dbtable_variable( |
| 65 | + table = table, variable = variable, channel = junk, error = error |
| 66 | + ), |
| 67 | + "channel does not inherit from class DBIConnection" |
| 68 | + ) |
| 69 | + DBI::dbDisconnect(channel) |
| 70 | +}) |
85 | 71 |
|
86 |
| - it("checks if variable is a non-empty character without missing values", { |
87 |
| - skip_on_cran() |
88 |
| - skip_if(isTRUE(Sys.getenv("CHECKLIST_OS", FALSE))) |
89 |
| - channel <- connect_ut_db() |
90 |
| - expect_that( |
91 |
| - check_dbtable_variable( |
92 |
| - table = table, |
93 |
| - variable = integer(0), |
94 |
| - channel = channel, |
95 |
| - error = error |
96 |
| - ), |
97 |
| - throws_error("variable must be character") |
98 |
| - ) |
99 |
| - expect_that( |
100 |
| - check_dbtable_variable( |
101 |
| - table = table, |
102 |
| - variable = c(variable, NA), |
103 |
| - channel = channel, |
104 |
| - error = error |
105 |
| - ), |
106 |
| - throws_error( |
107 |
| - "missing values in object" |
108 |
| - ) |
109 |
| - ) |
110 |
| - expect_that( |
111 |
| - check_dbtable_variable( |
112 |
| - table = table, |
113 |
| - variable = character(0), |
114 |
| - channel = channel, |
115 |
| - error = error |
116 |
| - ), |
117 |
| - throws_error("'variable' must contain at least one value") |
118 |
| - ) |
119 |
| - DBI::dbDisconnect(channel) |
120 |
| - }) |
| 72 | +test_that( |
| 73 | + "checks if variable is a non-empty character without missing values", { |
| 74 | + skip_on_cran() |
| 75 | + skip_if(as.logical(Sys.getenv("CHECKLIST_OS", FALSE))) |
| 76 | + junk <- "junk" |
| 77 | + table <- "location" |
| 78 | + variable <- c("external_code", "description") |
| 79 | + error <- TRUE |
| 80 | + channel <- connect_ut_db() |
| 81 | + expect_error( |
| 82 | + check_dbtable_variable( |
| 83 | + table = table, variable = integer(0), channel = channel, error = error |
| 84 | + ), |
| 85 | + "variable must be character" |
| 86 | + ) |
| 87 | + expect_error( |
| 88 | + check_dbtable_variable( |
| 89 | + table = table, variable = c(variable, NA), channel = channel, |
| 90 | + error = error |
| 91 | + ), |
| 92 | + "missing values in object" |
| 93 | + ) |
| 94 | + expect_error( |
| 95 | + check_dbtable_variable( |
| 96 | + table = table, variable = character(0), channel = channel, error = error |
| 97 | + ), |
| 98 | + "'variable' must contain at least one value" |
| 99 | + ) |
| 100 | + DBI::dbDisconnect(channel) |
| 101 | +}) |
121 | 102 |
|
122 |
| - it("gives correct output", { |
123 |
| - skip_on_cran() |
124 |
| - skip_if(isTRUE(Sys.getenv("CHECKLIST_OS", FALSE))) |
125 |
| - channel <- connect_ut_db() |
126 |
| - expect_true( |
127 |
| - check_dbtable_variable( |
128 |
| - table = table, |
129 |
| - variable = variable, |
130 |
| - channel = channel, |
131 |
| - error = error |
132 |
| - ) |
| 103 | +test_that("gives correct output", { |
| 104 | + skip_on_cran() |
| 105 | + skip_if(as.logical(Sys.getenv("CHECKLIST_OS", FALSE))) |
| 106 | + junk <- "junk" |
| 107 | + table <- "location" |
| 108 | + variable <- c("external_code", "description") |
| 109 | + error <- TRUE |
| 110 | + channel <- connect_ut_db() |
| 111 | + expect_true( |
| 112 | + check_dbtable_variable( |
| 113 | + table = table, variable = variable, channel = channel, error = error |
133 | 114 | )
|
134 |
| - expect_false( |
135 |
| - check_dbtable_variable( |
136 |
| - table = table, |
137 |
| - variable = junk, |
138 |
| - channel = channel, |
139 |
| - error = FALSE |
140 |
| - ) |
| 115 | + ) |
| 116 | + expect_false( |
| 117 | + check_dbtable_variable( |
| 118 | + table = table, variable = junk, channel = channel, error = FALSE |
141 | 119 | )
|
142 |
| - expect_false( |
143 |
| - check_dbtable_variable( |
144 |
| - table = table, |
145 |
| - variable = junk, |
146 |
| - channel = channel, |
147 |
| - error = FALSE |
148 |
| - ) |
| 120 | + ) |
| 121 | + expect_false( |
| 122 | + check_dbtable_variable( |
| 123 | + table = table, variable = junk, channel = channel, error = FALSE |
149 | 124 | )
|
150 |
| - expect_error( |
151 |
| - check_dbtable_variable( |
152 |
| - table = table, |
153 |
| - variable = junk, |
154 |
| - channel = channel, |
155 |
| - error = TRUE |
156 |
| - ), |
157 |
| - paste0( |
158 |
| - "Variable\\(s\\) missing from '", table, "': ", #nolint: nonportable_path_linter, line_length_linter. |
159 |
| - paste(junk, collapse = ", ") |
160 |
| - ) |
| 125 | + ) |
| 126 | + expect_error( |
| 127 | + check_dbtable_variable( |
| 128 | + table = table, variable = junk, channel = channel, error = TRUE |
| 129 | + ), |
| 130 | + paste0( |
| 131 | + "Variable\\(s\\) missing from '", table, "': ", |
| 132 | + paste(junk, collapse = ", ") |
161 | 133 | )
|
162 |
| - DBI::dbDisconnect(channel) |
163 |
| - }) |
164 |
| - |
| 134 | + ) |
| 135 | + DBI::dbDisconnect(channel) |
165 | 136 | })
|
0 commit comments