Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
runtests.jl 3.58 KiB
### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe.
###
### This is the top-level file of the Persefone test suite. Execute this to run all tests.
###

using Pkg
Pkg.activate("..")

using Dates
using DataFrames
using Logging
using LoggingExtras
using Persefone
using Random
using StableRNGs
using Test

const Ps = Persefone

const TESTPARAMETERS = joinpath(pkgdir(Persefone), "test/test_parameters.toml")
const TESTSETTINGS = Ps.getsettings(TESTPARAMETERS)

import Unitful: cm, m, km, ha, mg, g, kg, Length, Area, Mass
const m² = m^2
const km² = km^2

"""
    inittestmodel(smallmap=true)

Initialise an SimulationModel for testing purposes.

`smallmap`: use a hypothetical small landscape rather than a real one?
"""
function inittestmodel(smallmap=true)
    if smallmap
        landscape = smalltestlandscape()
    else
        landscape = Ps.initlandscape(TESTSETTINGS["world.mapdirectory"],
                                     TESTSETTINGS["world.landcovermap"],
                                     TESTSETTINGS["world.farmfieldsmap"])
    end
    weather = Ps.initweather(joinpath(TESTSETTINGS["world.mapdirectory"],
                                      TESTSETTINGS["world.weatherfile"]),
                             TESTSETTINGS["core.startdate"],
                             TESTSETTINGS["core.enddate"])
    crops = Ps.ALMaSS.readcropparameters(TESTSETTINGS["crop.cropfile"],
                                         TESTSETTINGS["crop.growthfile"])
    model = AgricultureModel(TESTSETTINGS,
                             StableRNG(TESTSETTINGS["core.seed"]),
                             global_logger(),
                             Vector{DataOutput}(),
                             Dict{String, DataFrame}(),
                             TESTSETTINGS["core.startdate"],
                             landscape,
                             weather,
                             crops,
                             Vector{Farmer}(),
                             Vector{FarmPlot}(),
                             Vector{Union{Animal,Nothing}}(),
                             Vector{Pair{Animal, Date}}(),
                             Vector{FarmEvent}())
    model
end    

"""
    smalltestlandscape()

Create a 8x8 landscape with five land cover types for testing:

    A A A A A A A A
    A A A A A A A A
    A A A A A A A A
    A A A A A W A A
    F F G G G G G G
    F F G G G G G G
    F F G G G G G G
    B B B B B B B B
"""
function smalltestlandscape()
    landscape = Matrix{Pixel}(undef, 8, 8)
    for x in 1:8
        for y in 1:8
            (y in (1:4)) ? lc = Ps.agriculture :
                (x in (1:2)) ? lc = Ps.forest :
                (y == 8) ? lc = Ps.builtup :
                lc = Ps.grass
            landscape[x,y] = Pixel(lc)
        end
    end
    landscape[6,4] = Pixel(Ps.water, 0)
    landscape
end

@testset "Persefone Tests" begin
    @testset "Core model" begin
        include("io_tests.jl")
        include("landscape_tests.jl")
        include("simulation_tests.jl")
    end
    @testset "Nature model" begin
        include("nature_tests.jl")
    end
    @testset "Crop growth model" begin
        include("crop_tests.jl")
    end
    @testset "Farm model" begin
        include("farm_tests.jl")
    end
    rm(TESTSETTINGS["core.outdir"], force=true, recursive=true)
end

# NOTE: Due to an issue with Julia (https://github.com/JuliaLang/julia/issues/48456),
# whenever we are using `@test_logs` with a function that takes a model object, we have
# to wrap that model object in `Ps.withtestlogger()`. (For an example, see the "Model
# simulation" testset in simulation_tests.jl.)