Skip to content
Snippets Groups Projects
Select Git revision
  • bf5024876b440bbd1eecccc4179c10e23cec2bd4
  • master default protected
  • taxon-oriented
  • marco/vectorise
  • development
  • phase-oriented
  • atkis
  • marco/atkis-skylark-speed-opt
  • marco/master-skylark-speed-opt
  • marco/atkis-fix-unit-tests
  • marco/aquacrop-fix-api-use
  • new-data
  • fix-almass-crop-model
  • precompile-statements
  • precompile-tools
  • tmp-faster-loading
  • skylark
  • testsuite
  • code-review
  • v0.8
  • v0.7.0
  • v0.6.1
  • v0.6.0
  • v0.5.5
  • v0.5.4
  • v0.5.3
  • v0.5.2
  • v0.2
  • v0.3.0
  • v0.4.1
  • v0.5
31 results

io_tests.jl

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    io_tests.jl 4.28 KiB
    ### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe.
    ###
    ### This tests the core input and output functions.
    ###
    
    @testset "Model configuration" begin
        model = inittestmodel()
        @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 = inittestmodel()
        # 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)]]
        Ps.newdataoutput!(model, "never", ["Date"], testoutput, "never")
        Ps.newdataoutput!(model, "daily", ["Date"], testoutput, "daily")
        Ps.newdataoutput!(model, "monthly", ["Date"], testoutput, "monthly")
        Ps.newdataoutput!(model, "yearly", ["Date"], testoutput, "yearly")
        Ps.newdataoutput!(model, "end", ["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 size(model.datatables["daily"]) == (366, 1)
        @test isfile(joinpath(outdir, "monthly.csv"))
        @test countlines(joinpath(outdir, "monthly.csv")) == 14
        @test size(model.datatables["monthly"]) == (13, 1)
        @test isfile(joinpath(outdir, "yearly.csv"))
        @test countlines(joinpath(outdir, "yearly.csv")) == 3
        @test size(model.datatables["yearly"]) == (2, 1)
        @test isfile(joinpath(outdir, "end.csv"))
        @test countlines(joinpath(outdir, "end.csv")) == 2
        @test size(model.datatables["end"]) == (1, 1)
        rm(outdir, force=true, recursive=true)
    end
    
    @testset "Model object serialization" begin
        model = inittestmodel()
        Ps.createdatadir(@param(core.outdir), true)
        originalenddate = @param(core.enddate)
        @param(core.enddate) = Date(year(model.date), 2, 5)
        @test_logs((:debug, "Saved model object to results_testsuite/test.dat."),
                   min_level=Logging.Debug, match_mode=:any,
                   savemodelobject(model, "test"))
        @test isfile(joinpath(@param(core.outdir), "test.dat"))
        model2 = loadmodelobject(joinpath(@param(core.outdir), "test.dat"))
        @test model.date == model2.date
        @test model.settings == model2.settings
        @test Ps.nagents(model) == Ps.nagents(model2)
        simulate!(Ps.withtestlogger(model))
        simulate!(Ps.withtestlogger(model2))
        @test model.date == model2.date
        @test Ps.nagents(model) == Ps.nagents(model2)
        rm(@param(core.outdir), force=true, recursive=true)
        @param(core.enddate) = originalenddate
    end
    
    @testset "Output visualisation" begin
        model = initialise(TESTPARAMETERS)
        stepsimulation!(model)
        @test_logs((:debug, "Visualising output."),
                   min_level=Logging.Debug, match_mode=:any,
                   visualiseoutput(model))
        @test isfile(joinpath(@param(core.outdir), "individuals.pdf"))
        @test isfile(joinpath(@param(core.outdir), "populations.pdf"))
        rm(@param(core.outdir), force=true, recursive=true)
    end