|
| 1 | +library(testthat) |
| 2 | +library(DSI) |
| 3 | +library(cli) |
| 4 | + |
| 5 | +library(testthat) |
| 6 | +library(mockery) |
| 7 | +library(cli) |
| 8 | +library(testthat) |
| 9 | +library(mockery) |
| 10 | +library(cli) |
| 11 | + |
| 12 | +if (!isClass("DSConnection")) { |
| 13 | + setClass("DSConnection", contains = "VIRTUAL") |
| 14 | +} |
| 15 | + |
| 16 | +setClass("MockDSConnection", contains = "DSConnection") |
| 17 | +mock_ds_conn <- new("MockDSConnection") |
| 18 | + |
| 19 | +test_that(".get_datasources retrieves connections when input is NULL", { |
| 20 | + |
| 21 | + mock_connections <- list( |
| 22 | + server1 = mock_ds_conn, |
| 23 | + server2 = mock_ds_conn |
| 24 | + ) |
| 25 | + |
| 26 | + stub(.get_datasources, "datashield.connections_find", mock_connections) |
| 27 | + |
| 28 | + result <- .get_datasources(NULL) |
| 29 | + |
| 30 | + expect_type(result, "list") |
| 31 | + expect_length(result, 2) |
| 32 | + expect_named(result, c("server1", "server2")) |
| 33 | + expect_true(is(result$server1, "DSConnection")) |
| 34 | +}) |
| 35 | + |
| 36 | +test_that(".get_datasources returns input when provided", { |
| 37 | + |
| 38 | + input_datasources <- list(A = "connA", B = "connB") |
| 39 | + |
| 40 | + result <- with_mocked_bindings( |
| 41 | + datashield.connections_find = function() stop("Should not be called!"), |
| 42 | + .get_datasources(input_datasources), |
| 43 | + .package = "dsBaseClient" |
| 44 | + ) |
| 45 | + |
| 46 | + expect_equal(result, input_datasources) |
| 47 | +}) |
| 48 | + |
| 49 | +test_that(".verify_datasources passes with valid DSConnection list", { |
| 50 | + |
| 51 | + valid_datasources <- list( |
| 52 | + conn_list1 = mock_ds_conn, |
| 53 | + conn_list2 = mock_ds_conn |
| 54 | + ) |
| 55 | + |
| 56 | + expect_no_error(.verify_datasources(valid_datasources)) |
| 57 | + expect_null(.verify_datasources(valid_datasources)) |
| 58 | +}) |
| 59 | + |
| 60 | +test_that(".verify_datasources aborts with invalid object types", { |
| 61 | + |
| 62 | + invalid_datasources <- list( |
| 63 | + conn_list1 = mock_ds_conn, |
| 64 | + conn_list2 = "not_a_connection" |
| 65 | + ) |
| 66 | + |
| 67 | + expect_error( |
| 68 | + .verify_datasources(invalid_datasources) |
| 69 | + ) |
| 70 | +}) |
| 71 | + |
| 72 | +test_that(".set_datasources works with valid input", { |
| 73 | + |
| 74 | + input_datasources <- list( |
| 75 | + mock_ds_conn |
| 76 | + ) |
| 77 | + |
| 78 | + result <- with_mocked_bindings( |
| 79 | + .get_datasources = function(d) d, |
| 80 | + .verify_datasources = function(d) {}, |
| 81 | + .set_datasources(input_datasources), |
| 82 | + .package = "dsBaseClient" |
| 83 | + ) |
| 84 | + |
| 85 | + expect_equal(result, input_datasources) |
| 86 | +}) |
| 87 | + |
| 88 | +test_that(".set_datasources calls .get_datasources and .verify_datasources", { |
| 89 | + |
| 90 | + get_called <- FALSE |
| 91 | + verify_called <- FALSE |
| 92 | + |
| 93 | + mock_get <- function(d) { |
| 94 | + get_called <<- TRUE |
| 95 | + return(list(list(mock_ds_conn))) |
| 96 | + } |
| 97 | + |
| 98 | + mock_verify <- function(d) { |
| 99 | + verify_called <<- TRUE |
| 100 | + } |
| 101 | + |
| 102 | + with_mocked_bindings( |
| 103 | + .get_datasources = mock_get, |
| 104 | + .verify_datasources = mock_verify, |
| 105 | + .set_datasources(NULL), |
| 106 | + .package = "dsBaseClient" |
| 107 | + ) |
| 108 | + |
| 109 | + expect_true(get_called) |
| 110 | + expect_true(verify_called) |
| 111 | +}) |
| 112 | + |
| 113 | +test_that(".set_datasources aborts if verification fails", { |
| 114 | + |
| 115 | + expect_error( |
| 116 | + with_mocked_bindings( |
| 117 | + .get_datasources = function(d) list(list("bad_conn")), |
| 118 | + .verify_datasources = function(d) cli::cli_abort("Verification failed mock"), |
| 119 | + .set_datasources(NULL), |
| 120 | + .package = "dsBaseClient" |
| 121 | + ) |
| 122 | + ) |
| 123 | +}) |
| 124 | + |
| 125 | +test_that(".check_df_name_provided passes when df is not NULL", { |
| 126 | + |
| 127 | + expect_no_error(.check_df_name_provided("D")) |
| 128 | + expect_null(.check_df_name_provided("D")) |
| 129 | + |
| 130 | + expect_no_error(.check_df_name_provided(data.frame(a=1))) |
| 131 | +}) |
| 132 | + |
| 133 | +test_that(".check_df_name_provided aborts when df is NULL", { |
| 134 | + |
| 135 | + expect_error( |
| 136 | + .check_df_name_provided(NULL) |
| 137 | + ) |
| 138 | +}) |
| 139 | + |
0 commit comments