From da5933212f23c88f1f5428a16b484f54c378f561 Mon Sep 17 00:00:00 2001 From: aheinri5 <Anna@netzkritzler.de> Date: Tue, 24 Nov 2020 09:25:25 +0100 Subject: [PATCH] [tests] Added some new tests for read_in function --- tests/testthat/test-ogs6.R | 20 +++++ tests/testthat/test-read_in_gml.R | 30 ++++++- tests/testthat/test-read_in_prj.R | 117 ++++++++++++++++++++++++++++ tests/testthat/test-read_in_utils.R | 88 +++++++++++++++++++++ 4 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-ogs6.R create mode 100644 tests/testthat/test-read_in_prj.R create mode 100644 tests/testthat/test-read_in_utils.R diff --git a/tests/testthat/test-ogs6.R b/tests/testthat/test-ogs6.R new file mode 100644 index 0000000..42620e9 --- /dev/null +++ b/tests/testthat/test-ogs6.R @@ -0,0 +1,20 @@ + + + +test_that("OGS6 clear function works as expected", { + + ogs6_obj <- OGS6$new(sim_name = "sim", + sim_id = 1, + sim_path = "sim_path", + ogs_bin_path = "ogs_bin_path", + test_mode = TRUE) + + input_add(r2ogs6_parameter(name = "pressure0", + type = "Constant", + values = 1e5), + ogs6_obj) + + expect_equal(length(ogs6_obj$parameters), 1) + expect_warning(ogs6_obj$clear(c("pamameter", "parameters"))) + expect_equal(length(ogs6_obj$parameters), 0) +}) \ No newline at end of file diff --git a/tests/testthat/test-read_in_gml.R b/tests/testthat/test-read_in_gml.R index 1c67e48..6413567 100644 --- a/tests/testthat/test-read_in_gml.R +++ b/tests/testthat/test-read_in_gml.R @@ -1,10 +1,38 @@ test_that("read_in_points correctly reads point tibble from file", { - xml_doc <- validate_read_in_xml(system.file("extdata", "cube_1x1x1.gml", package = "r2ogs6")) + xml_doc <- validate_read_in_xml(system.file("extdata/flow_free_expansion", + "cube_1x1x1.gml", package = "r2ogs6")) point_tibble <- read_in_points(xml_doc) expect_equal(length(point_tibble), 4) expect_equal(length(point_tibble[[1]]), 8) expect_setequal(names(point_tibble), c("x", "y", "z", "name")) +}) + + +test_that("read_in_polylines correctly reads polyline list from file", { + + xml_doc <- validate_read_in_xml(system.file("extdata/flow_free_expansion", + "cube_1x1x1.gml", package = "r2ogs6")) + + polyline_list <- read_in_polylines(xml_doc) + + expect_equal(length(polyline_list), 12) + expect_equal(length(polyline_list[[1]]), 2) + expect_equal(length(polyline_list[[1]][[2]]), 2) +}) + + +test_that("read_in_surfaces correctly reads surface list from file", { + + xml_doc <- validate_read_in_xml(system.file("extdata/flow_free_expansion", + "cube_1x1x1.gml", package = "r2ogs6")) + + surface_list <- read_in_surfaces(xml_doc) + + expect_equal(length(surface_list), 6) + expect_equal(length(surface_list[[1]]), 3) + expect_equal(length(surface_list[[1]][[2]]), 3) + expect_equal(length(surface_list[[1]][[3]]), 3) }) \ No newline at end of file diff --git a/tests/testthat/test-read_in_prj.R b/tests/testthat/test-read_in_prj.R new file mode 100644 index 0000000..4b96e6e --- /dev/null +++ b/tests/testthat/test-read_in_prj.R @@ -0,0 +1,117 @@ + + +#processes + + + +test_that("read_in_media works", { + + prj_path <- (system.file("extdata/flow_free_expansion", + "flow_free_expansion.prj", package = "r2ogs6")) + + ogs6_obj <- OGS6$new(sim_name = "sim", + sim_id = 1, + sim_path = "sim_path", + ogs_bin_path = "ogs_bin_path", + test_mode = TRUE) + + read_in_media(ogs6_obj, prj_path) + + expect_equal(length(ogs6_obj$media), 1) +}) + + +test_that("read_in_time_loop works", { + + prj_path <- (system.file("extdata/flow_free_expansion", + "flow_free_expansion.prj", package = "r2ogs6")) + + ogs6_obj <- OGS6$new(sim_name = "sim", + sim_id = 1, + sim_path = "sim_path", + ogs_bin_path = "ogs_bin_path", + test_mode = TRUE) + + read_in_time_loop(ogs6_obj, prj_path) + + expect_equal(is.null(ogs6_obj$time_loop), FALSE) +}) + + +test_that("read_in_parameters works", { + + prj_path <- (system.file("extdata/flow_free_expansion", + "flow_free_expansion.prj", package = "r2ogs6")) + + ogs6_obj <- OGS6$new(sim_name = "sim", + sim_id = 1, + sim_path = "sim_path", + ogs_bin_path = "ogs_bin_path", + test_mode = TRUE) + + read_in_parameters(ogs6_obj, prj_path) + + expect_equal(length(ogs6_obj$parameters), 7) + expect_equal(ogs6_obj$parameters[[1]]$name, "E") + expect_equal(ogs6_obj$parameters[[1]]$type, "Constant") + expect_equal(ogs6_obj$parameters[[1]]$values, 10e9) +}) + +test_that("read_in_process_variables works", { + + prj_path <- (system.file("extdata/flow_free_expansion", + "flow_free_expansion.prj", package = "r2ogs6")) + + ogs6_obj <- OGS6$new(sim_name = "sim", + sim_id = 1, + sim_path = "sim_path", + ogs_bin_path = "ogs_bin_path", + test_mode = TRUE) + + read_in_process_variables(ogs6_obj, prj_path) + + expect_equal(length(ogs6_obj$process_variables), 2) +}) + +test_that("read_in_nonlinear_solvers works", { + + prj_path <- (system.file("extdata/flow_free_expansion", + "flow_free_expansion.prj", package = "r2ogs6")) + + ogs6_obj <- OGS6$new(sim_name = "sim", + sim_id = 1, + sim_path = "sim_path", + ogs_bin_path = "ogs_bin_path", + test_mode = TRUE) + + read_in_nonlinear_solvers(ogs6_obj, prj_path) + + expect_equal(length(ogs6_obj$nonlinear_solvers), 1) + expect_equal(ogs6_obj$nonlinear_solvers[[1]]$name, "basic_newton") + expect_equal(ogs6_obj$nonlinear_solvers[[1]]$type, "Newton") + expect_equal(ogs6_obj$nonlinear_solvers[[1]]$max_iter, 50) + expect_equal(ogs6_obj$nonlinear_solvers[[1]]$linear_solver, "general_linear_solver") +}) + + +test_that("read_in_linear_solvers works", { + + prj_path <- (system.file("extdata/flow_free_expansion", + "flow_free_expansion.prj", package = "r2ogs6")) + + ogs6_obj <- OGS6$new(sim_name = "sim", + sim_id = 1, + sim_path = "sim_path", + ogs_bin_path = "ogs_bin_path", + test_mode = TRUE) + + read_in_linear_solvers(ogs6_obj, prj_path) + + expect_equal(length(ogs6_obj$linear_solvers), 1) + expect_equal(ogs6_obj$linear_solvers[[1]]$name, "general_linear_solver") + expect_equal(ogs6_obj$linear_solvers[[1]]$lis, "-i bicgstab -p ilu -tol 1e-16 -maxiter 10000") + expect_equal(ogs6_obj$linear_solvers[[1]]$eigen, list(solver_type = "BiCGSTAB", + precon_type = "ILUT", + max_iteration_step = 10000, + error_tolerance = 1e-16)) +}) diff --git a/tests/testthat/test-read_in_utils.R b/tests/testthat/test-read_in_utils.R new file mode 100644 index 0000000..c2acbe2 --- /dev/null +++ b/tests/testthat/test-read_in_utils.R @@ -0,0 +1,88 @@ + + +# test_that("order_parameters works", { +# +# #... +# +# }) + + +test_that("make_call_from_parameters works", { + + parameters <- list(a = "hello", b = c(1, 2), c = "hola") + const_call <- make_call_from_parameters("myclass", parameters) + + expect_equal(const_call, "myclass(a = \"hello\", b = c(1, 2), c = \"hola\")") + +}) + + +test_that("guess_structure works", { + + prj_path <- (system.file("extdata/flow_free_expansion", + "flow_free_expansion.prj", package = "r2ogs6")) + + ogs6_obj <- OGS6$new(sim_name = "sim", + sim_id = 1, + sim_path = "sim_path", + ogs_bin_path = "ogs_bin_path", + test_mode = TRUE) + + read_in_parameters(ogs6_obj, prj_path) + + expect_equal(length(ogs6_obj$parameters), 7) + expect_equal(ogs6_obj$parameters[[1]]$name, "E") + expect_equal(ogs6_obj$parameters[[1]]$type, "Constant") + expect_equal(ogs6_obj$parameters[[1]]$values, 10e9) +}) + + +test_that("is_het_wrapper works", { + + ogs6_obj <- OGS6$new(sim_name = "sim", + sim_id = 1, + sim_path = "sim_path", + ogs_bin_path = "ogs_bin_path", + test_mode = TRUE) + + xml_doc <- xml2::read_xml(system.file("extdata/flow_free_expansion", + "flow_free_expansion.prj", package = "r2ogs6")) + + time_stepping_node <- xml2::xml_find_first(xml_doc, "//time_stepping") + + expect_equal(is_het_wrapper(time_stepping_node), TRUE) + +}) + + +test_that("get_grandchild_length_vector works", { + + xml_doc <- xml2::read_xml("<a><b><c>1.1</c></b><b><c>2.1</c><c>2.2</c></b></a>") + expect_equal(get_grandchild_length_vector(xml_doc), c(1, 2)) + +}) + + +test_that("list_from_nodeset works", { + + xml_doc_1 <- xml2::read_xml("<a><b>1</b><b>2</b><b>3</b></a>") + xml_nodeset_1 <- xml2::xml_children(xml_doc_1) + + xml_doc_2 <- xml2::read_xml("<a><b id=\"1\" name=\"first\"/><b id=\"2\" name=\"second\"/></a>") + xml_nodeset_2 <- xml2::xml_children(xml_doc_2) + + expect_equal(list_from_nodeset(xml_nodeset_1), list(b = "1", b = "2", b = "3")) + expect_equal(list_from_nodeset(xml_nodeset_2), list(b = c(id = "1", name = "first"), + b = c(id = "2", name = "second"))) +}) + + +test_that("find_read_in_func_call works", { + + node_name_1 <- "Mamma mia" + node_name_2 <- "timesteps" + + expect_equal(find_read_in_func_call(node_name_1), "") + expect_equal(find_read_in_func_call(node_name_2), "read_in_timesteps_node(xml_node)") + +}) \ No newline at end of file -- GitLab