Skip to content
Snippets Groups Projects
  • xo30xoqa's avatar
    4499a7fc
    Fixed broken tests · 4499a7fc
    xo30xoqa authored
    Added tests for dataframe data collection (#64), refactored
    `smalltestlandscape` into `inittestmodel`.
    4499a7fc
    History
    Fixed broken tests
    xo30xoqa authored
    Added tests for dataframe data collection (#64), refactored
    `smalltestlandscape` into `inittestmodel`.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
runtests.jl 3.22 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 Agents
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)

"""
Initialise an AgentBasedModel 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.landcovermap"],
                                     TESTSETTINGS["world.farmfieldsmap"])
    end
    space = GridSpace(size(landscape), periodic=false)
    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}(),
                                  :datatables=>Dict{String,DataFrame}(),
                                  :settings=>TESTSETTINGS)
    return AgentBasedModel(Union{Farmer,Animal,FarmPlot}, space, properties=properties,
                           rng=StableRNG(TESTSETTINGS["core.seed"]), warn=false)
end    

"""
    smalltestlandscape()

Create a 6x6 landscape with three land cover types for testing:

    F F F F F F
    F F F F F F
    F F F F F F
    F F F F F W
    F F G G G G
    F F G G G G
"""
function smalltestlandscape()
    landscape = Matrix{Pixel}(undef, 6, 6)
    for x in 1:6
        for y in 1:6
            (x in (1:2) || y in (1:4)) ? lc = Ps.forest : lc = Ps.grass
            landscape[x,y] = Pixel(lc, missing, [])
        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.)