From ef023c5ee0499f7e35b1b566bc58a86ca9fb055a Mon Sep 17 00:00:00 2001
From: Johannes Boog <johannesboog@yahoo.de>
Date: Wed, 7 Dec 2022 22:58:09 +0100
Subject: [PATCH] [tests] for read in and export of includes

---
 tests/testthat/test-export_prj.R    |  69 ++++++++++++++++++-
 tests/testthat/test-read_in_prj.R   | 103 +++++++++++++++++++++++++---
 tests/testthat/test-read_in_utils.R |   3 +-
 3 files changed, 162 insertions(+), 13 deletions(-)

diff --git a/tests/testthat/test-export_prj.R b/tests/testthat/test-export_prj.R
index d6b126b..42dc5ca 100644
--- a/tests/testthat/test-export_prj.R
+++ b/tests/testthat/test-export_prj.R
@@ -125,7 +125,7 @@ test_that("export_prj works for referenced *.msh files", {
 
 
 
-test_that("export_prj works for process$include", {
+test_that("export_prj works for /*$include", {
 
     # Get extdata directory and create folder for the test
     extdata_path <- system.file("extdata/", package = "r2ogs6")
@@ -155,6 +155,73 @@ test_that("export_prj works for process$include", {
                  sort(c("circle_1_axi.gml", "circle_1e1_axi.prj",
                    "line_1_lines_1e1.vtu", "SteadyStateDiffusion.xml")))
 
+    # Tidy up by deleting the folder we created
+    unlink(test_path, recursive = TRUE)
+})
+
+test_that("export_prj works for top-level include", {
+
+    # Get extdata directory and create folder for the test
+    extdata_path <- system.file("extdata/", package = "r2ogs6")
+    test_path <- paste0(extdata_path, "/export_prj_test")
+    dir.create(test_path)
+
+    # Define prj_path and OGS6 object, then read in .prj file
+    ogs6_obj <- OGS6$new(sim_name = "x_strain_y_flow",
+                         sim_path = test_path)
+    prj_path <-
+        system.file(
+            "extdata/benchmarks/OrthotropicEmbeddedFracturePermeability",
+            "x_strain_y_flow.prj", package = "r2ogs6")
+    incl_path <-
+        system.file(
+            "extdata/benchmarks/OrthotropicEmbeddedFracturePermeability",
+            "cube_strain_flow_data.include", package = "r2ogs6")
+
+    # tests without reading the include
+    read_in_prj(ogs6_obj, prj_path, read_in_gml = F)
+    # export of prj only
+    export_prj(ogs6_obj, copy_ext_files = F)
+    expect_equal(list.files(ogs6_obj$sim_path), "x_strain_y_flow.prj")
+    file.remove(paste0(ogs6_obj$sim_path, "x_strain_y_flow.prj"))
+    # export with copying referenced files
+    export_prj(ogs6_obj, copy_ext_files = T)
+    expect_equal(sort(list.files(ogs6_obj$sim_path)),
+                 sort(c("x_strain_y_flow.prj","cube_strain_flow_data.include")))
+    file.remove(paste0(ogs6_obj$sim_path, "x_strain_y_flow.prj"))
+    file.remove(paste0(ogs6_obj$sim_path, "cube_strain_flow_data.include"))
+
+
+    # tests with reading include
+    rm(ogs6_obj)
+    ref_path <-
+        system.file(
+            "extdata/benchmarks/OrthotropicEmbeddedFracturePermeability",
+            "ref_x_strain_y_flow.prj", package = "r2ogs6")
+
+    ogs6_obj <- OGS6$new(sim_name = "x_strain_y_flow", sim_path = test_path)
+    read_in_prj(ogs6_obj, prj_path, read_in_gml = F, read_includes = T)
+    # export of prj only
+    export_prj(ogs6_obj, copy_ext_files = F)
+    ref_xml <- xml2::read_xml(ref_path)
+    test_xml <- xml2::read_xml(paste0(ogs6_obj$sim_path,
+                                     "x_strain_y_flow.prj"))
+    ref <- xml2::as_list(ref_xml)
+    test <- xml2::as_list(test_xml)
+    test$OpenGeoSysProject$mesh[[1]] <-
+        basename(test$OpenGeoSysProject$mesh[[1]])
+    test$OpenGeoSysProject$geometry[[1]] <-
+        basename(test$OpenGeoSysProject$geometry[[1]])
+    expect_equal(ref, test)
+    expect_equal(list.files(ogs6_obj$sim_path), "x_strain_y_flow.prj")
+    file.remove(paste0(ogs6_obj$sim_path, "x_strain_y_flow.prj"))
+
+    # export with copying referenced files
+    export_prj(ogs6_obj, copy_ext_files = T)
+    expect_equal(sort(list.files(ogs6_obj$sim_path)),
+                 sort(c("cube_1x1x1.gml", "x_strain_y_flow.prj",
+                        "cube_1x1x1_quad.vtu")))
+
     # Tidy up by deleting the folder we created
     unlink(test_path, recursive = TRUE)
 })
\ No newline at end of file
diff --git a/tests/testthat/test-read_in_prj.R b/tests/testthat/test-read_in_prj.R
index f48a62a..684c6c6 100644
--- a/tests/testthat/test-read_in_prj.R
+++ b/tests/testthat/test-read_in_prj.R
@@ -4,12 +4,13 @@ test_that("read_in works for process objects", {
 
     prj_path <- (system.file("extdata/benchmarks/flow_free_expansion",
                              "flow_free_expansion.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/processes/process")
 
     expect_equal(length(ogs6_obj$processes), 1)
@@ -28,12 +29,13 @@ test_that("read_in works for medium objects", {
 
     prj_path <- (system.file("extdata/benchmarks/flow_free_expansion",
                              "flow_free_expansion.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/media/medium")
 
     expect_equal(length(ogs6_obj$media), 1)
@@ -44,12 +46,13 @@ test_that("read_in works for class objects with ellipsis", {
 
     prj_path <- (system.file("extdata/benchmarks/theis_well_pumping",
                              "theis.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/media/medium")
 
     expect_equal(ogs6_obj$
@@ -70,12 +73,13 @@ test_that("read_in works for class objects with ellipsis", {
     prj_path <- (system.file("extdata/benchmarks/SimpleSynthetics",
                              "DiffusionAndStorageAndAdvection.prj",
                              package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/media/medium")
 
     expect_equal(ogs6_obj$
@@ -113,12 +117,13 @@ test_that("read_in works for time_loop objects", {
 
     prj_path <- (system.file("extdata/benchmarks/flow_free_expansion",
                              "flow_free_expansion.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/time_loop")
 
     expect_equal(is.null(ogs6_obj$time_loop), FALSE)
@@ -132,12 +137,13 @@ test_that("read_in works for parameter objects", {
 
     prj_path <- (system.file("extdata/benchmarks/flow_free_expansion",
                              "flow_free_expansion.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/parameters/parameter")
 
     expect_equal(length(ogs6_obj$parameters), 7)
@@ -150,12 +156,13 @@ test_that("read_in works for process_variable objects", {
 
     prj_path <- (system.file("extdata/benchmarks/flow_free_expansion",
                              "flow_free_expansion.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/process_variables/process_variable")
 
     expect_equal(length(ogs6_obj$process_variables), 2)
@@ -165,12 +172,13 @@ test_that("read_in works for nonlinear_solver objects", {
 
     prj_path <- (system.file("extdata/benchmarks/flow_free_expansion",
                              "flow_free_expansion.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/nonlinear_solvers/nonlinear_solver")
 
     expect_equal(length(ogs6_obj$nonlinear_solvers), 1)
@@ -186,12 +194,13 @@ test_that("read_in works for linear_solver objects", {
 
     prj_path <- (system.file("extdata/benchmarks/flow_free_expansion",
                              "flow_free_expansion.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/linear_solvers/linear_solver")
 
     expect_equal(length(ogs6_obj$linear_solvers), 1)
@@ -206,12 +215,13 @@ test_that("read_in works with newline value separation", {
 
     prj_path <- (system.file("extdata/benchmarks/LiakopoulosHM",
                              "liakopoulos.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
     read_in(ogs6_obj,
-            prj_path,
+            xml_doc,
             "/OpenGeoSysProject/curves/curve")
 
     expect_equal(length(ogs6_obj$curves), 1)
@@ -222,13 +232,14 @@ test_that("read_in works with newline value separation", {
 })
 
 
-test_that("read_in_prj works for processes/include tags", {
+test_that("read_in_prj works for /*/include tags", {
 
     prj_path <- (system.file("extdata/benchmarks/Elliptic/circle_radius_1",
                              "circle_1e1_axi.prj", package = "r2ogs6"))
     incl_path <- (system.file("extdata/benchmarks/Elliptic/other_dir",
                              "SteadyStateDiffusion.xml", package = "r2ogs6"))
 
+    # test if include list will be set
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
@@ -238,8 +249,78 @@ test_that("read_in_prj works for processes/include tags", {
     expect_equal(length(ogs6_obj$processes), 1)
     expect_equal(names(ogs6_obj$processes)[[1]], "include")
     expect_equal(ogs6_obj$processes$include[["file"]], incl_path)
+
+    # test with reading content of included file
+    rm(ogs6_obj)
+    ogs6_obj <- OGS6$new(sim_name = "sim",
+                         sim_path = "sim_path")
+
+    read_in_prj(ogs6_obj, prj_path, read_includes = T)
+
+    expect_equal(ogs6_obj$processes[[1]]$name, "SteadyStateDiffusion")
+    expect_equal(ogs6_obj$processes[[1]]$type, "STEADY_STATE_DIFFUSION")
+    expect_equal(ogs6_obj$processes[[1]]$integration_order, 2)
+    expect_equal(ogs6_obj$processes[[1]]$process_variable[[1]], "pressure")
+    expect_equal(ogs6_obj$processes[[1]]$secondary_variables[[1]],
+                 c(internal_name = "darcy_velocity", output_name = "v"))
 })
 
+test_that("read_in_prj works for top-level include tags", {
+
+    prj_path <- (
+        system.file(
+            "extdata/benchmarks/OrthotropicEmbeddedFracturePermeability",
+            "x_strain_y_flow.prj", package = "r2ogs6"))
+    incl_path <- (
+        system.file(
+            "extdata/benchmarks/OrthotropicEmbeddedFracturePermeability",
+            "cube_strain_flow_data.include", package = "r2ogs6"))
+
+    # test if include list will be set
+    ogs6_obj <- OGS6$new(sim_name = "sim",
+                         sim_path = "sim_path")
+
+    read_in_prj(ogs6_obj,
+                prj_path)
+
+    expect_equal(length(ogs6_obj$include), 1)
+    expect_equal(ogs6_obj$include[["file"]], incl_path)
+
+    # test with reading content of included file
+    mesh_path <-
+        system.file(
+            "extdata/benchmarks/OrthotropicEmbeddedFracturePermeability",
+            "cube_1x1x1_quad.vtu", package = "r2ogs6")
+    gml_path <-
+        system.file(
+            "extdata/benchmarks/OrthotropicEmbeddedFracturePermeability",
+            "cube_1x1x1_quad.vtu", package = "r2ogs6")
+
+    rm(ogs6_obj)
+    ogs6_obj <- OGS6$new(sim_name = "sim",
+                         sim_path = "sim_path")
+
+    read_in_prj(ogs6_obj, prj_path, read_includes = T)
+
+    expect_equal(ogs6_obj$meshes[[1]]$path, mesh_path)
+    expect_equal(ogs6_obj$gml$name, "cube_1x1x1_geometry")
+    expect_equal(dim(ogs6_obj$gml$points), c(8,4))
+    expect_equal(ogs6_obj$processes[[1]]$name, "HM")
+    expect_equal(ogs6_obj$processes[[1]]$type, "HYDRO_MECHANICS")
+    expect_equal(ogs6_obj$processes[[1]]$integration_order, 3)
+    expect_equal(ogs6_obj$processes[[1]]$process_variable[[1]], "displacement")
+    expect_equal(ogs6_obj$processes[[1]]$process_variable[[2]], "pressure")
+    expect_equal(ogs6_obj$processes[[1]]$secondary_variables[[1]],
+                 c(internal_name = "sigma", output_name = "sigma"))
+    expect_equal(ogs6_obj$processes[[1]]$secondary_variables[[2]],
+                 c(internal_name = "epsilon", output_name = "epsilon"))
+    expect_equal(ogs6_obj$media[[1]]$phases[[1]]$type, "Gas")
+    expect_equal(ogs6_obj$media[[1]]$phases[[2]]$type, "Solid")
+    expect_equal(ogs6_obj$media[[1]]$properties[[1]]$name, "porosity")
+    expect_equal(ogs6_obj$media[[1]]$properties[[1]]$type, "Constant")
+    expect_equal(ogs6_obj$media[[1]]$properties[[1]]$value, 0.01)
+
+})
 
 test_that("read_in_prj works for EmbeddedFracturePermeability/cube.prj", {
 
diff --git a/tests/testthat/test-read_in_utils.R b/tests/testthat/test-read_in_utils.R
index 8b2e143..1c8f0d7 100644
--- a/tests/testthat/test-read_in_utils.R
+++ b/tests/testthat/test-read_in_utils.R
@@ -4,11 +4,12 @@ test_that("node_to_object works for simple r2ogs6 classes", {
 
     prj_path <- (system.file("extdata/benchmarks/flow_free_expansion",
                              "flow_free_expansion.prj", package = "r2ogs6"))
+    xml_doc <- validate_read_in_xml(prj_path)
 
     ogs6_obj <- OGS6$new(sim_name = "sim",
                          sim_path = "sim_path")
 
-    read_in(ogs6_obj, prj_path, "/OpenGeoSysProject/parameters/parameter")
+    read_in(ogs6_obj, xml_doc, "/OpenGeoSysProject/parameters/parameter")
 
     expect_equal(length(ogs6_obj$parameters), 7)
     expect_equal(ogs6_obj$parameters[[1]]$name, "E")
-- 
GitLab