Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
io_tests.jl 2.94 KiB
### Persefone - a socio-economic-ecological model of European agricultural landscapes.
###
### This tests the core input and output functions.
###

@testset "Model configuration" begin
    settings = Ps.getsettings(TESTPARAMETERS)
    properties = Dict{Symbol,Any}(:settings=>settings)
    space = GridSpace((10,10), periodic=false)
    model = AgentBasedModel(Animal, space, properties=properties, warn=false)

    @test @param(core.configfile) == basename(TESTPARAMETERS)
    @test @param(core.startdate) == Date(2022, 2, 1)
    @test @param(core.loglevel) == "warn"
    @test @param(nature.targetspecies) == ["Wolpertinger", "Wyvern"]
    @param(core.enddate) = Date(2022,1,3)
    @test @param(core.enddate) == Date(2022,1,3)
end

@testset "Output functions" begin
    model = smalltestlandscape()
    # test that the output directory is created with all files
    outdir = @param(core.outdir)
    Ps.createdatadir(outdir, @param(core.overwrite))
    @test isdir(outdir)
    logger = Ps.modellogger(@param(core.loglevel), outdir)
    @test_logs((:debug, "Setting up output directory results_testsuite."),
               min_level=Logging.Debug, match_mode=:any,
               Ps.saveinputfiles(model))
    @test isfile(joinpath(outdir, @param(world.landcovermap)))
    @test isfile(joinpath(outdir, @param(world.farmfieldsmap)))
    @test isfile(joinpath(outdir, @param(core.configfile)))
    @test isfile(joinpath(outdir, Ps.LOGFILE))
    # check whether the overwrite warning/protection works
    @test_logs((:warn, "Overwriting existing output directory $(outdir)."),
               match_mode=:any,
               Ps.createdatadir(outdir, @param(core.overwrite)))
    @test_throws ErrorException Ps.createdatadir(outdir, false)
    # test that creating a DataOutput works, and outputs data with the required frequency
    testoutput(model) = string(model.date)*"\n"
    Ps.newdataoutput!(model, "never.csv", "Date", testoutput, "never")
    Ps.newdataoutput!(model, "daily.csv", "Date", testoutput, "daily")
    Ps.newdataoutput!(model, "monthly.csv", "Date", testoutput, "monthly")
    Ps.newdataoutput!(model, "yearly.csv", "Date", testoutput, "yearly")
    Ps.newdataoutput!(model, "end.csv", "Date", testoutput, "end")
    @param(core.enddate) = Date(2023,2,1)
    @test_logs((:info, "Simulated 366 days."),
               match_mode=:any,
               simulate!(Ps.withtestlogger(model)))
    @param(core.enddate) = Date(2022,3,31) # change back to original to prevent errors
    @test !isfile(joinpath(outdir, "never.csv"))
    @test isfile(joinpath(outdir, "daily.csv"))
    @test countlines(joinpath(outdir, "daily.csv")) == 367
    @test isfile(joinpath(outdir, "monthly.csv"))
    @test countlines(joinpath(outdir, "monthly.csv")) == 14
    @test isfile(joinpath(outdir, "yearly.csv"))
    @test countlines(joinpath(outdir, "yearly.csv")) == 3
    @test isfile(joinpath(outdir, "end.csv"))
    @test countlines(joinpath(outdir, "end.csv")) == 2
    rm(outdir, force=true, recursive=true)
end