From c42af38447528cb4bf1eb20cf3d17e6ac6398845 Mon Sep 17 00:00:00 2001
From: Daniel Vedder <daniel.vedder@idiv.de>
Date: Thu, 31 Aug 2023 12:28:33 +0200
Subject: [PATCH] Started fixing tests

---
 src/Persefone.jl          |  2 +-
 src/crop/farmplot.jl      | 11 +++++++++++
 src/nature/nature.jl      | 12 ++++++------
 test/landscape_tests.jl   |  3 ++-
 test/nature_tests.jl      |  8 ++++----
 test/runtests.jl          |  3 +++
 test/test_parameters.toml |  4 ++--
 7 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/src/Persefone.jl b/src/Persefone.jl
index 7b666ad..a2c0a71 100644
--- a/src/Persefone.jl
+++ b/src/Persefone.jl
@@ -60,7 +60,7 @@ export
     @habitat,
     @landcover,
     @cropheight,
-    @croptype,
+    @cropname,
     @distanceto,
     @distancetoedge,
     @countanimals,
diff --git a/src/crop/farmplot.jl b/src/crop/farmplot.jl
index 0178506..af15dc5 100644
--- a/src/crop/farmplot.jl
+++ b/src/crop/farmplot.jl
@@ -176,6 +176,17 @@ function croptype(pos::Tuple{Int64,Int64}, model::AgentBasedModel)
               model[model.landscape[pos...].fieldid].croptype
 end
 
+"""
+    cropname(model, position)
+
+Return the name of the crop at this position, or nothing if there is no crop here
+(utility wrapper).
+"""
+function cropname(pos::Tuple{Int64,Int64}, model::AgentBasedModel)
+    ismissing(model.landscape[pos...].fieldid) ? nothing :
+              model[model.landscape[pos...].fieldid].croptype.name
+end
+
 """
     cropheight(model, position)
 
diff --git a/src/nature/nature.jl b/src/nature/nature.jl
index e63c993..105644f 100644
--- a/src/nature/nature.jl
+++ b/src/nature/nature.jl
@@ -287,7 +287,7 @@ and a position, and returns `true` or `false` depending on the conditions
 specified in the macro body.
 
 Several utility macros can be used within the body of `@habitat` as a short-hand for
-common expressions: [`@landcover`](@ref), [`@croptype`](@ref), [`@cropheight`](@ref),
+common expressions: [`@landcover`](@ref), [`@cropname`](@ref), [`@cropheight`](@ref),
 [`@distanceto`](@ref), [`@distancetoedge`](@ref), [`@countanimals`](@ref).
 The variables `model` and `pos` can be used for checks that don't have a macro available.
 
@@ -297,7 +297,7 @@ Two example uses of `@habitat` might look like this:
 movementhabitat = @habitat(@landcover() in (grass agriculture soil))
 
 nestinghabitat = @habitat((@landcover() == grass || 
-                           (@landcover() == agriculture && @croptype() != maize &&
+                           (@landcover() == agriculture && @cropname() != "maize" &&
                             @cropheight() < 10)) &&
                           @distanceto(forest) > 20)
 ```
@@ -333,14 +333,14 @@ macro landcover()
 end
 
 """
-    @croptype
+    @cropname
 
-Return the local croptype, or nothing if there is no crop here.
+Return the name of the local croptype, or nothing if there is no crop here.
 This is a utility wrapper that can only be used nested within [`@phase`](@ref)
 or [`@habitat`](@ref).
 """
-macro croptype()
-    :(croptype($(esc(:pos)), $(esc(:model))))
+macro cropname()
+    :(cropname($(esc(:pos)), $(esc(:model))))
 end
 
 """
diff --git a/test/landscape_tests.jl b/test/landscape_tests.jl
index 32f9f65..1fa2ac5 100644
--- a/test/landscape_tests.jl
+++ b/test/landscape_tests.jl
@@ -8,7 +8,8 @@
     landscape = Ps.initlandscape(TESTSETTINGS["world.landcovermap"],
                                  TESTSETTINGS["world.farmfieldsmap"])
     space = GridSpace(size(landscape), periodic=false)
-    properties = Dict{Symbol,Any}(:landscape=>landscape, :settings=>TESTSETTINGS)
+    properties = Dict{Symbol,Any}(:landscape=>landscape, :settings=>TESTSETTINGS,
+                                  :date=>TESTSETTINGS["core.startdate"])
     model = AgentBasedModel(FarmPlot, space, properties=properties, warn=false)
     # these tests are specific to the Jena maps
     @test_logs (:info, "Initialised 2092 farm plots.") match_mode=:any Ps.initfields!(model)
diff --git a/test/nature_tests.jl b/test/nature_tests.jl
index d862f58..614780d 100644
--- a/test/nature_tests.jl
+++ b/test/nature_tests.jl
@@ -8,12 +8,12 @@
     model = smalltestlandscape()
     model.landscape[6,6] = Pixel(Ps.agriculture, 1, [])
     species::Dict{String,Any} = Dict("name"=>"test_animal")
-    add_agent!((6,6), FarmPlot, model, [(6,6)], Ps.wheat, 1.2, 3.4)
+    add_agent!((6,6), FarmPlot, model, [(6,6)], Ps.wheat, 1.2, 3.4) #FIXME
     add_agent!((3,3), Animal, model, species, Ps.male, 1)
     add_agent!((4,4), Animal, model, species, Ps.female, 1)
     # create a set of habitat descriptors
     h1 = @habitat(@landcover() == Ps.water)
-    h2 = @habitat(@croptype() == Ps.wheat &&
+    h2 = @habitat(@cropname() == "wheat" &&
                   @cropheight() < 2)
     h3 = @habitat(@distanceto(Ps.water) > 2 &&
                   @distancetoedge() <= 2)
@@ -77,7 +77,7 @@ end
         @phase life begin
             @debug "$(Persefone.animalid(animal)) is swimming happily in its pond."
             @respond Persefone.pesticide @kill(@trait(pesticidemortality), "poisoning")
-            @respond Persefone.harvest @setphase(drought)
+            @respond Persefone.harvesting @setphase(drought)
             @debug "Animal: $animal"
             if @trait(sex) == Persefone.female && @countanimals() < 3 &&
                 @trait(age) >= @trait(ageofmaturity) && @landcover() == Persefone.water
@@ -103,7 +103,7 @@ end
     @test Ps.countanimals((1,1), model, radius=4) == 0
     @test Ps.countanimals(pond, model) == 2
     @test model[1].age == 0
-    createevent!(model, [pond], Ps.harvest)
+    createevent!(model, [pond], Ps.harvesting)
     @test_logs((:debug, "Mermaid 1 is swimming happily in its pond."),
                (:debug, "Mermaid 2 is swimming happily in its pond."),
                min_level=Logging.Debug, match_mode=:any,
diff --git a/test/runtests.jl b/test/runtests.jl
index 9023b6b..cfa6d69 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -45,9 +45,12 @@ function smalltestlandscape()
     weather = Ps.initweather(TESTSETTINGS["world.weatherfile"],
                              TESTSETTINGS["core.startdate"],
                              TESTSETTINGS["core.enddate"])
+    crops = Ps.readcropparameters(TESTSETTINGS["crop.cropfile"],
+                                  TESTSETTINGS["crop.growthfile"])
     properties = Dict{Symbol,Any}(:date=>TESTSETTINGS["core.startdate"],
                                   :landscape=>landscape,
                                   :weather=>weather,
+                                  :crops=>crops,
                                   :events=>Vector{FarmEvent}(),
                                   :logger=>global_logger(),
                                   :dataoutputs=>Vector{DataOutput}(),
diff --git a/test/test_parameters.toml b/test/test_parameters.toml
index ba7d5d0..ac63e7c 100644
--- a/test/test_parameters.toml
+++ b/test/test_parameters.toml
@@ -32,6 +32,6 @@ insectmodel = ["season", "habitat", "pesticides"] # which factors affect insect
 	
 [crop]
 cropmodel = "almass" # crop growth model to use, "almass" or "aquacrop"
-cropfile = "data/crop_data_general.csv" # file with general crop parameters
-growthfile = "data/almass_crop_growth_curves.csv" # file with crop growth parameters
+cropfile = "crop_data_general.csv" # file with general crop parameters
+growthfile = "almass_crop_growth_curves.csv" # file with crop growth parameters
 
-- 
GitLab