|
| 1 | + |
| 2 | +#------------------------------------------------------------------------------- |
| 3 | +# Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved. |
| 4 | +# |
| 5 | +# This program and the accompanying materials |
| 6 | +# are made available under the terms of the GNU Public License v3.0. |
| 7 | +# |
| 8 | +# You should have received a copy of the GNU General Public License |
| 9 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 10 | +#------------------------------------------------------------------------------- |
| 11 | + |
| 12 | +# |
| 13 | +# Set up |
| 14 | +# |
| 15 | + |
| 16 | +## When .loadServersideObject is called, the actual data exists two levels below the function, |
| 17 | +## i.e. data in global env --> ds function --> .loadServersideObject. We recreate this in |
| 18 | +## the test environment with a wrapper function. |
| 19 | +.dsFunctionWrapper <- function(x) { |
| 20 | + .loadServersideObject(x) |
| 21 | +} |
| 22 | + |
| 23 | +# context("utils::smk::setup") |
| 24 | +test_that(".loadServersideObject() returns existing object", { |
| 25 | + test_df <- data.frame(a = 1:3) |
| 26 | + result <- .dsFunctionWrapper("test_df") |
| 27 | + expect_identical(result, test_df) |
| 28 | +}) |
| 29 | + |
| 30 | +test_that(".loadServersideObject() throws error for missing object", { |
| 31 | + expect_error( |
| 32 | + .dsFunctionWrapper("test_df"), |
| 33 | + regexp = "does not exist" |
| 34 | + ) |
| 35 | +}) |
| 36 | + |
| 37 | +test_that(".checkClass() passes for correct class", { |
| 38 | + df <- data.frame(a = 1) |
| 39 | + expect_invisible( |
| 40 | + .checkClass(df, "df", c("data.frame", "matrix")) |
| 41 | + ) |
| 42 | +}) |
| 43 | + |
| 44 | +test_that(".checkClass() throws informative error for wrong class with one target class", { |
| 45 | + x <- list(a = 1) |
| 46 | + expect_error( |
| 47 | + .checkClass(x, "x", "data.frame"), |
| 48 | + regexp = "The server-side object must be of type data.frame. 'x' is type list." |
| 49 | + ) |
| 50 | +}) |
| 51 | + |
| 52 | +test_that(".checkClass() throws informative error for wrong class with three target classes", { |
| 53 | + x <- list(a = 1) |
| 54 | + expect_error( |
| 55 | + .checkClass(x, "x", c("data.frame", "matrix", "unicorn")), |
| 56 | + regexp = "The server-side object must be of type data.frame, matrix or unicorn. 'x' is type list." |
| 57 | + ) |
| 58 | +}) |
| 59 | + |
| 60 | +test_that(".checkClass() throws informative error for three target classes and three actual classes", { |
| 61 | + x <- tibble(a = 1) |
| 62 | + expect_error( |
| 63 | + .checkClass(x, "x", c("Boolean", "unicorn", "donkey")), |
| 64 | + regexp = "The server-side object must be of type Boolean, unicorn or donkey. 'x' is type tbl_df, tbl and data.frame." |
| 65 | + ) |
| 66 | +}) |
| 67 | + |
| 68 | +# context("utils::smk::shutdown") |
| 69 | +# context("utils::smk::done") |
0 commit comments