From eaa3be0bcd599d20e2a4f91fa0b977859fae4f38 Mon Sep 17 00:00:00 2001
From: Marco Matthies <71844+marcom@users.noreply.github.com>
Date: Wed, 18 Sep 2024 19:39:06 +0200
Subject: [PATCH] Fix failing unit tests

---
 src/core/input.jl                                           | 2 +-
 src/crop/cropmodels.jl                                      | 2 +-
 src/parameters.toml                                         | 1 -
 test/crop_tests.jl                                          | 6 +++---
 test/{ => cropparams-almass}/crop_data_general.csv          | 0
 .../crop_growth_curves.csv}                                 | 0
 test/io_tests.jl                                            | 2 +-
 test/runtests.jl                                            | 3 +--
 test/simulation_tests.jl                                    | 5 +++--
 test/test_parameters.toml                                   | 3 +--
 test/test_parameters_almass.toml                            | 4 +---
 test/test_parameters_simplecrop.toml                        | 5 +----
 12 files changed, 13 insertions(+), 20 deletions(-)
 rename test/{ => cropparams-almass}/crop_data_general.csv (100%)
 rename test/{almass_crop_growth_curves.csv => cropparams-almass/crop_growth_curves.csv} (100%)

diff --git a/src/core/input.jl b/src/core/input.jl
index b0a2b99..db2bdef 100644
--- a/src/core/input.jl
+++ b/src/core/input.jl
@@ -46,7 +46,7 @@ end
 Combines all configuration options to produce a single settings dict.
 Precedence: function arguments - commandline parameters - user config file - default values
 """
-function getsettings(configfile::String, userparams::Dict{String,Any}=Dict())
+function getsettings(configfile::String, userparams::Dict{String,Any}=Dict{String,Any}())
     # read in and merge configurations from the commandline, the default config file
     # and a config file supplied by the user (via the --configfile option)
     defaults::Dict{String, Any} = TOML.parsefile(PARAMFILE) |> flattenTOML
diff --git a/src/crop/cropmodels.jl b/src/crop/cropmodels.jl
index 83497fd..3d84e98 100644
--- a/src/crop/cropmodels.jl
+++ b/src/crop/cropmodels.jl
@@ -17,7 +17,7 @@ function initcropmodel(cropmodel::AbstractString, cropdirectory::AbstractString)
     elseif cropmodel == "simple"
         Tcroptype = SimpleCrop.CropType
         Tcropstate = SimpleCrop.CropState
-        crops_almass = ALMaSS.readcropparameters(cropfile, growthfile)
+        crops_almass = ALMaSS.readcropparameters(cropdirectory)
         crops = Dict(name => SimpleCrop.CropType(ct.name, ct.group, ct.minsowdate, ct.maxsowdate)
                      for (name, ct) in crops_almass)
     else
diff --git a/src/parameters.toml b/src/parameters.toml
index 0cb6ea7..1c60864 100644
--- a/src/parameters.toml
+++ b/src/parameters.toml
@@ -47,4 +47,3 @@ insectmodel = ["season", "habitat", "pesticides", "weather"] # factors affecting
 [crop]
 cropmodel = "almass" # crop growth model to use, "almass", "aquacrop", or "simple"
 cropdirectory = "data/crops/almass" # the directory storing all data files for the selected crop model
-
diff --git a/test/crop_tests.jl b/test/crop_tests.jl
index 9632b1d..106f385 100644
--- a/test/crop_tests.jl
+++ b/test/crop_tests.jl
@@ -6,13 +6,13 @@
 const TESTPARAM_ALMASS = joinpath(pkgdir(Persefone), "test", "test_parameters_almass.toml")
 const TESTPARAM_SIMPLECROP = joinpath(pkgdir(Persefone), "test", "test_parameters_simplecrop.toml")
 
-@testset for paramfile in (TESTPARAM_ALMASS, TESTPARAM_SIMPLECROP)
+@testset for configfile in (TESTPARAM_ALMASS, TESTPARAM_SIMPLECROP)
     @testset "Model initialisation" begin
-        model = initialise(paramfile)
+        model = initialise(; configfile)
         @test model isa AgricultureModel
     end
     @testset "Time step" begin
-        model = initialise(paramfile)
+        model = initialise(; configfile)
         stepsimulation!(model)
         @test model isa AgricultureModel
     end
diff --git a/test/crop_data_general.csv b/test/cropparams-almass/crop_data_general.csv
similarity index 100%
rename from test/crop_data_general.csv
rename to test/cropparams-almass/crop_data_general.csv
diff --git a/test/almass_crop_growth_curves.csv b/test/cropparams-almass/crop_growth_curves.csv
similarity index 100%
rename from test/almass_crop_growth_curves.csv
rename to test/cropparams-almass/crop_growth_curves.csv
diff --git a/test/io_tests.jl b/test/io_tests.jl
index deebec3..79d9fa1 100644
--- a/test/io_tests.jl
+++ b/test/io_tests.jl
@@ -95,7 +95,7 @@ end
 end
 
 @testset "Output visualisation" begin
-    model = initialise(TESTPARAMETERS)
+    model = initialise(; configfile=TESTPARAMETERS)
     stepsimulation!(model)
     @test_logs((:debug, "Visualising output."),
                min_level=Logging.Debug, match_mode=:any,
diff --git a/test/runtests.jl b/test/runtests.jl
index 7fb635a..f4401eb 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -44,8 +44,7 @@ function inittestmodel(smallmap=true)
                              TESTSETTINGS["core.startdate"],
                              TESTSETTINGS["core.enddate"])
     # TODO: support other crop models besides ALMaSS
-    crops = Ps.ALMaSS.readcropparameters(TESTSETTINGS["crop.cropfile"],
-                                         TESTSETTINGS["crop.growthfile"])
+    crops = Ps.ALMaSS.readcropparameters(TESTSETTINGS["crop.cropdirectory"])
     farmers = Vector{Farmer}()
     farmplots = Vector{Ps.FarmPlot{Ps.ALMaSS.CropState}}()
     model = AgricultureModel{Ps.ALMaSS.CropType,Ps.ALMaSS.CropState}(
diff --git a/test/simulation_tests.jl b/test/simulation_tests.jl
index b8bb76e..c188569 100644
--- a/test/simulation_tests.jl
+++ b/test/simulation_tests.jl
@@ -4,7 +4,7 @@
 ###
 
 @testset "Model initialisation" begin
-    model = initialise(TESTPARAMETERS, params=Dict("core.visualise"=>false))
+    model = initialise(; configfile=TESTPARAMETERS, params=Dict{String,Any}("core.visualise"=>false))
     @test typeof(model.settings) == Dict{String, Any}
     @test @param(core.visualise) == false
     @test model.date == Date(2022,2,1)
@@ -48,7 +48,8 @@ end
     Random.seed!(1)
     rand1 = rand()
     Random.seed!(1)
-    model = initialise(TESTPARAMETERS, 218)
+    model = initialise(; configfile=TESTPARAMETERS,
+                       params=Dict{String,Any}("core.seed" => 218))
     @test rand() == rand1 
     Random.seed!(1)
     @test @param(core.seed) == 218
diff --git a/test/test_parameters.toml b/test/test_parameters.toml
index 089712e..47c5a08 100644
--- a/test/test_parameters.toml
+++ b/test/test_parameters.toml
@@ -41,5 +41,4 @@ insectmodel = ["season", "habitat", "pesticides"] # which factors affect insect
 	
 [crop]
 cropmodel = "almass" # crop growth model to use, "almass", "aquacrop", or "simple"
-cropfile = "crop_data_general.csv" # file with general crop parameters
-growthfile = "almass_crop_growth_curves.csv" # file with crop growth parameters
\ No newline at end of file
+cropdirectory = "test/cropparams-almass/" # the directory storing all data files for the selected crop model
diff --git a/test/test_parameters_almass.toml b/test/test_parameters_almass.toml
index 159e59d..fa520c2 100644
--- a/test/test_parameters_almass.toml
+++ b/test/test_parameters_almass.toml
@@ -41,6 +41,4 @@ insectmodel = ["season", "habitat", "pesticides"] # which factors affect insect
 
 [crop]
 cropmodel = "almass" # crop growth model to use, "almass", "aquacrop", or "simple"
-cropfile = "crop_data_general.csv" # file with general crop parameters
-growthfile = "almass_crop_growth_curves.csv" # file with crop growth parameters
-
+cropdirectory = "test/cropparams-almass/" # the directory storing all data files for the selected crop model
diff --git a/test/test_parameters_simplecrop.toml b/test/test_parameters_simplecrop.toml
index b7c08f1..43bd550 100644
--- a/test/test_parameters_simplecrop.toml
+++ b/test/test_parameters_simplecrop.toml
@@ -41,7 +41,4 @@ insectmodel = ["season", "habitat", "pesticides"] # which factors affect insect
 
 [crop]
 cropmodel = "simple" # crop growth model to use, "almass", "aquacrop", or "simple"
-cropfile = "crop_data_general.csv" # file with general crop parameters
-# TODO: this parameter should be unnecessary, but removing it
-# currently gives an error
-growthfile = "almass_crop_growth_curves.csv" # file with crop growth parameters
+cropdirectory = "test/cropparams-almass/" # the directory storing all data files for the selected crop model
-- 
GitLab