From 8fc84186b6a12f8d9ed7daaba6cf2dcefbe0293f Mon Sep 17 00:00:00 2001
From: Daniel Vedder <daniel.vedder@idiv.de>
Date: Mon, 2 Jan 2023 17:25:01 +0100
Subject: [PATCH] Wrote basic I/O tests

---
 test/io_tests.jl          | 25 ++++++++++++++++++++++++-
 test/runtests.jl          |  5 ++++-
 test/test_parameters.toml |  3 ++-
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/test/io_tests.jl b/test/io_tests.jl
index c7d10ce..19a6637 100644
--- a/test/io_tests.jl
+++ b/test/io_tests.jl
@@ -3,4 +3,27 @@
 ### This tests the core input and output functions.
 ###
 
-#TODO
+@testset "Model configuration" begin
+    # `test_parameters.toml` is read in in `runtests.jl`
+    @test param("core.configfile") == TESTPARAMETERS
+    @test param("core.startdate") == Date(2020, 1, 1)
+    @test param("nature.targetspecies") == ["Wolpertinger", "Wyvern"]
+    #TODO test commandline parameters
+end
+
+@testset "Output functions" begin
+    # test that the output directory is created with all files
+    logstring = "Setting up output directory results_testsuite_$(Dates.today())_s1"
+    @test_logs (:info, logstring) Persephone.setupdatadir()
+    @test isdir(param("core.outdir"))
+    @test isfile(joinpath(param("core.outdir"), param("core.landcovermap")))
+    @test isfile(joinpath(param("core.outdir"), param("core.farmfieldsmap")))
+    @test isfile(joinpath(param("core.outdir"), param("core.configfile")))
+    @test isfile(joinpath(param("core.outdir"), Persephone.LOGFILE))
+    # check whether the overwrite warning/protection works
+    logstring = "Overwriting existing output directory $(param("core.outdir"))."
+    @test_logs (:warn, logstring) match_mode=:any Persephone.setupdatadir()
+    #TODO test overwrite protection (requires parameter mutability)
+    rm(param("core.outdir"), force=true, recursive=true)
+    #TODO test that creating a DataOutput works, and outputs data with the required frequency
+end
diff --git a/test/runtests.jl b/test/runtests.jl
index 5af8e83..e185c55 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -9,10 +9,13 @@ Pkg.activate("..")
 using Persephone
 using Test
 using Random
+using Dates
 using Agents
 
+const TESTPARAMETERS = "test_parameters.toml"
+
 @testset "Persephone Tests" begin
-    Persephone.initsettings("test_parameters.toml")
+    Persephone.initsettings(TESTPARAMETERS)
     @testset "Core model" begin
         include("io_tests.jl")
         include("landscape_tests.jl")
diff --git a/test/test_parameters.toml b/test/test_parameters.toml
index f03b079..464a6a9 100644
--- a/test/test_parameters.toml
+++ b/test/test_parameters.toml
@@ -8,7 +8,8 @@ configfile = "test_parameters.toml" # location of the configuration file
 landcovermap = "landcover_jena.tif" # location of the landcover map
 farmfieldsmap = "fields_jena.tif" # location of the field geometry map
 outdir = "results_testsuite" # location and name of the output folder
-loglevel = "debug" # verbosity level: "debug", "info", "quiet"
+overwrite = true # overwrite the output directory? (true/false/"ask")
+loglevel = "info" # verbosity level: "debug", "info", "quiet"
 seed = 1 # seed value for the RNG (0 -> random value)
 # dates to start and end the simulation
 startdate = 2020-01-01
-- 
GitLab