From 4b0fc7327cf5ea5afc5e2ca2c15df93c313c4309 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Wed, 9 Nov 2022 10:34:18 -0600 Subject: [PATCH] Accelerate tests with Revise.jl --- Project.toml | 1 + instances/solutions/s1.log | 11 -- .../fixtures/instances}/s1.json | 0 .../fixtures/instances}/s2.json | 0 .../fixtures/instances}/solutions/s1.json | 0 test/graph/build_test.jl | 59 +++--- test/instance/compress_test.jl | 95 +++++----- test/instance/geodb_test.jl | 36 ++-- test/instance/parse_test.jl | 175 +++++++++--------- test/model/build_test.jl | 65 +++---- test/model/solve_test.jl | 100 +++++----- test/reports_test.jl | 26 +-- test/runtests.jl | 51 +++-- 13 files changed, 322 insertions(+), 297 deletions(-) delete mode 100644 instances/solutions/s1.log rename {instances => test/fixtures/instances}/s1.json (100%) rename {instances => test/fixtures/instances}/s2.json (100%) rename {instances => test/fixtures/instances}/solutions/s1.json (100%) diff --git a/Project.toml b/Project.toml index b03521d..ff67434 100644 --- a/Project.toml +++ b/Project.toml @@ -21,6 +21,7 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" PackageCompiler = "9b87118b-4619-50d2-8e1e-99f35a4d4d9d" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" ProgressBars = "49802e3a-d2f1-5c88-81d8-b72133a6f568" +Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" Shapefile = "8e980c4a-a4fe-5da2-b3a7-4b4b0353a2f4" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" StochasticPrograms = "8b8459f2-c380-502b-8633-9aed2d6c2b35" diff --git a/instances/solutions/s1.log b/instances/solutions/s1.log deleted file mode 100644 index 230d5fc..0000000 --- a/instances/solutions/s1.log +++ /dev/null @@ -1,11 +0,0 @@ -[ Info: Reading s1.json... -[ Info: Building graph... -[ Info: 2 time periods -[ Info: 6 process nodes -[ Info: 8 shipping nodes (plant) -[ Info: 10 shipping nodes (collection) -[ Info: 38 arcs -[ Info: Building optimization model... -[ Info: Optimizing MILP... -[ Info: Re-optimizing with integer variables fixed... -[ Info: Extracting solution... diff --git a/instances/s1.json b/test/fixtures/instances/s1.json similarity index 100% rename from instances/s1.json rename to test/fixtures/instances/s1.json diff --git a/instances/s2.json b/test/fixtures/instances/s2.json similarity index 100% rename from instances/s2.json rename to test/fixtures/instances/s2.json diff --git a/instances/solutions/s1.json b/test/fixtures/instances/solutions/s1.json similarity index 100% rename from instances/solutions/s1.json rename to test/fixtures/instances/solutions/s1.json diff --git a/test/graph/build_test.jl b/test/graph/build_test.jl index 1b52f58..b2523fd 100644 --- a/test/graph/build_test.jl +++ b/test/graph/build_test.jl @@ -3,37 +3,38 @@ using RELOG -@testset "build_graph" begin - basedir = dirname(@__FILE__) - instance = RELOG.parsefile("$basedir/../../instances/s1.json") - graph = RELOG.build_graph(instance) - process_node_by_location_name = - Dict(n.location.location_name => n for n in graph.process_nodes) +function graph_build_test() + @testset "build_graph" begin + instance = RELOG.parsefile(fixture("instances/s1.json")) + graph = RELOG.build_graph(instance) + process_node_by_location_name = + Dict(n.location.location_name => n for n in graph.process_nodes) - @test length(graph.plant_shipping_nodes) == 8 - @test length(graph.collection_shipping_nodes) == 10 - @test length(graph.process_nodes) == 6 + @test length(graph.plant_shipping_nodes) == 8 + @test length(graph.collection_shipping_nodes) == 10 + @test length(graph.process_nodes) == 6 - node = graph.collection_shipping_nodes[1] - @test node.location.name == "C1" - @test length(node.incoming_arcs) == 0 - @test length(node.outgoing_arcs) == 2 - @test node.outgoing_arcs[1].source.location.name == "C1" - @test node.outgoing_arcs[1].dest.location.plant_name == "F1" - @test node.outgoing_arcs[1].dest.location.location_name == "L1" - @test node.outgoing_arcs[1].values["distance"] == 1095.62 + node = graph.collection_shipping_nodes[1] + @test node.location.name == "C1" + @test length(node.incoming_arcs) == 0 + @test length(node.outgoing_arcs) == 2 + @test node.outgoing_arcs[1].source.location.name == "C1" + @test node.outgoing_arcs[1].dest.location.plant_name == "F1" + @test node.outgoing_arcs[1].dest.location.location_name == "L1" + @test node.outgoing_arcs[1].values["distance"] == 1095.62 - node = process_node_by_location_name["L1"] - @test node.location.plant_name == "F1" - @test node.location.location_name == "L1" - @test length(node.incoming_arcs) == 10 - @test length(node.outgoing_arcs) == 2 + node = process_node_by_location_name["L1"] + @test node.location.plant_name == "F1" + @test node.location.location_name == "L1" + @test length(node.incoming_arcs) == 10 + @test length(node.outgoing_arcs) == 2 - node = process_node_by_location_name["L3"] - @test node.location.plant_name == "F2" - @test node.location.location_name == "L3" - @test length(node.incoming_arcs) == 2 - @test length(node.outgoing_arcs) == 2 + node = process_node_by_location_name["L3"] + @test node.location.plant_name == "F2" + @test node.location.location_name == "L3" + @test length(node.incoming_arcs) == 2 + @test length(node.outgoing_arcs) == 2 - @test length(graph.arcs) == 38 -end + @test length(graph.arcs) == 38 + end +end \ No newline at end of file diff --git a/test/instance/compress_test.jl b/test/instance/compress_test.jl index 87d2a98..4b7a5a6 100644 --- a/test/instance/compress_test.jl +++ b/test/instance/compress_test.jl @@ -3,51 +3,52 @@ using RELOG -@testset "compress" begin - basedir = dirname(@__FILE__) - instance = RELOG.parsefile("$basedir/../../instances/s1.json") - compressed = RELOG._compress(instance) - - product_name_to_product = Dict(p.name => p for p in compressed.products) - location_name_to_facility = Dict() - for p in compressed.plants - location_name_to_facility[p.location_name] = p +function compress_test() + @testset "compress" begin + instance = RELOG.parsefile(fixture("instances/s1.json")) + compressed = RELOG._compress(instance) + + product_name_to_product = Dict(p.name => p for p in compressed.products) + location_name_to_facility = Dict() + for p in compressed.plants + location_name_to_facility[p.location_name] = p + end + for c in compressed.collection_centers + location_name_to_facility[c.name] = c + end + + p1 = product_name_to_product["P1"] + p2 = product_name_to_product["P2"] + p3 = product_name_to_product["P3"] + c1 = location_name_to_facility["C1"] + l1 = location_name_to_facility["L1"] + + @test compressed.time == 1 + @test compressed.building_period == [1] + + @test p1.name == "P1" + @test p1.transportation_cost ≈ [0.015] + @test p1.transportation_energy ≈ [0.115] + @test p1.transportation_emissions["CO2"] ≈ [0.051] + @test p1.transportation_emissions["CH4"] ≈ [0.0025] + + @test c1.name == "C1" + @test c1.amount ≈ [1869.12] + + @test l1.plant_name == "F1" + @test l1.location_name == "L1" + @test l1.energy ≈ [0.115] + @test l1.emissions["CO2"] ≈ [0.051] + @test l1.emissions["CH4"] ≈ [0.0025] + @test l1.sizes[1].opening_cost ≈ [500] + @test l1.sizes[2].opening_cost ≈ [1250] + @test l1.sizes[1].fixed_operating_cost ≈ [60] + @test l1.sizes[2].fixed_operating_cost ≈ [60] + @test l1.sizes[1].variable_operating_cost ≈ [30] + @test l1.sizes[2].variable_operating_cost ≈ [30] + @test l1.disposal_limit[p2] ≈ [2.0] + @test l1.disposal_limit[p3] ≈ [2.0] + @test l1.disposal_cost[p2] ≈ [-10.0] + @test l1.disposal_cost[p3] ≈ [-10.0] end - for c in compressed.collection_centers - location_name_to_facility[c.name] = c - end - - p1 = product_name_to_product["P1"] - p2 = product_name_to_product["P2"] - p3 = product_name_to_product["P3"] - c1 = location_name_to_facility["C1"] - l1 = location_name_to_facility["L1"] - - @test compressed.time == 1 - @test compressed.building_period == [1] - - @test p1.name == "P1" - @test p1.transportation_cost ≈ [0.015] - @test p1.transportation_energy ≈ [0.115] - @test p1.transportation_emissions["CO2"] ≈ [0.051] - @test p1.transportation_emissions["CH4"] ≈ [0.0025] - - @test c1.name == "C1" - @test c1.amount ≈ [1869.12] - - @test l1.plant_name == "F1" - @test l1.location_name == "L1" - @test l1.energy ≈ [0.115] - @test l1.emissions["CO2"] ≈ [0.051] - @test l1.emissions["CH4"] ≈ [0.0025] - @test l1.sizes[1].opening_cost ≈ [500] - @test l1.sizes[2].opening_cost ≈ [1250] - @test l1.sizes[1].fixed_operating_cost ≈ [60] - @test l1.sizes[2].fixed_operating_cost ≈ [60] - @test l1.sizes[1].variable_operating_cost ≈ [30] - @test l1.sizes[2].variable_operating_cost ≈ [30] - @test l1.disposal_limit[p2] ≈ [2.0] - @test l1.disposal_limit[p3] ≈ [2.0] - @test l1.disposal_cost[p2] ≈ [-10.0] - @test l1.disposal_cost[p3] ≈ [-10.0] -end +end \ No newline at end of file diff --git a/test/instance/geodb_test.jl b/test/instance/geodb_test.jl index e0009f0..443cc28 100644 --- a/test/instance/geodb_test.jl +++ b/test/instance/geodb_test.jl @@ -4,22 +4,24 @@ using RELOG -@testset "geodb_query (2018-us-county)" begin - region = RELOG.geodb_query("2018-us-county:17043") - @test region.centroid.lat == 41.83956 - @test region.centroid.lon == -88.08857 - @test region.population == 922_921 -end +function geodb_test() + @testset "geodb_query (2018-us-county)" begin + region = RELOG.geodb_query("2018-us-county:17043") + @test region.centroid.lat == 41.83956 + @test region.centroid.lon == -88.08857 + @test region.population == 922_921 + end -# @testset "geodb_query (2018-us-zcta)" begin -# region = RELOG.geodb_query("2018-us-zcta:60439") -# @test region.centroid.lat == 41.68241 -# @test region.centroid.lon == -87.98954 -# end + # @testset "geodb_query (2018-us-zcta)" begin + # region = RELOG.geodb_query("2018-us-zcta:60439") + # @test region.centroid.lat == 41.68241 + # @test region.centroid.lon == -87.98954 + # end -@testset "geodb_query (us-state)" begin - region = RELOG.geodb_query("us-state:IL") - @test region.centroid.lat == 39.73939 - @test region.centroid.lon == -89.50414 - @test region.population == 12_671_821 -end + @testset "geodb_query (us-state)" begin + region = RELOG.geodb_query("us-state:IL") + @test region.centroid.lat == 39.73939 + @test region.centroid.lon == -89.50414 + @test region.population == 12_671_821 + end +end \ No newline at end of file diff --git a/test/instance/parse_test.jl b/test/instance/parse_test.jl index f4d0527..b5ba710 100644 --- a/test/instance/parse_test.jl +++ b/test/instance/parse_test.jl @@ -3,91 +3,90 @@ using RELOG -@testset "parse" begin - basedir = dirname(@__FILE__) - instance = RELOG.parsefile("$basedir/../../instances/s1.json") - - centers = instance.collection_centers - plants = instance.plants - products = instance.products - location_name_to_plant = Dict(p.location_name => p for p in plants) - product_name_to_product = Dict(p.name => p for p in products) - - @test length(centers) == 10 - @test centers[1].name == "C1" - @test centers[1].latitude == 7 - @test centers[1].latitude == 7 - @test centers[1].longitude == 7 - @test centers[1].amount == [934.56, 934.56] - @test centers[1].product.name == "P1" - - @test length(plants) == 6 - - plant = location_name_to_plant["L1"] - @test plant.plant_name == "F1" - @test plant.location_name == "L1" - @test plant.input.name == "P1" - @test plant.latitude == 0 - @test plant.longitude == 0 - - @test length(plant.sizes) == 2 - @test plant.sizes[1].capacity == 250 - @test plant.sizes[1].opening_cost == [500, 500] - @test plant.sizes[1].fixed_operating_cost == [30, 30] - @test plant.sizes[1].variable_operating_cost == [30, 30] - @test plant.sizes[2].capacity == 1000 - @test plant.sizes[2].opening_cost == [1250, 1250] - @test plant.sizes[2].fixed_operating_cost == [30, 30] - @test plant.sizes[2].variable_operating_cost == [30, 30] - - p1 = product_name_to_product["P1"] - @test p1.disposal_limit == [1.0, 1.0] - @test p1.disposal_cost == [-1000.0, -1000.0] - - p2 = product_name_to_product["P2"] - @test p2.disposal_limit == [0.0, 0.0] - @test p2.disposal_cost == [0.0, 0.0] - - p3 = product_name_to_product["P3"] - @test length(plant.output) == 2 - @test plant.output[p2] == 0.2 - @test plant.output[p3] == 0.5 - @test plant.disposal_limit[p2] == [1, 1] - @test plant.disposal_limit[p3] == [1, 1] - @test plant.disposal_cost[p2] == [-10, -10] - @test plant.disposal_cost[p3] == [-10, -10] - - plant = location_name_to_plant["L3"] - @test plant.location_name == "L3" - @test plant.input.name == "P2" - @test plant.latitude == 25 - @test plant.longitude == 65 - - @test length(plant.sizes) == 2 - @test plant.sizes[1].capacity == 1000.0 - @test plant.sizes[1].opening_cost == [3000, 3000] - @test plant.sizes[1].fixed_operating_cost == [50, 50] - @test plant.sizes[1].variable_operating_cost == [50, 50] - @test plant.sizes[1] == plant.sizes[2] - - p4 = product_name_to_product["P4"] - @test plant.output[p3] == 0.05 - @test plant.output[p4] == 0.8 - @test plant.disposal_limit[p3] == [1e8, 1e8] - @test plant.disposal_limit[p4] == [0, 0] -end - -@testset "parse (geodb)" begin - basedir = dirname(@__FILE__) - instance = RELOG.parsefile("$basedir/../../instances/s2.json") - - centers = instance.collection_centers - @test centers[1].name == "C1" - @test centers[1].latitude == 41.83956 - @test centers[1].longitude == -88.08857 -end - -# @testset "parse (invalid)" begin -# basedir = dirname(@__FILE__) -# @test_throws ErrorException RELOG.parsefile("$basedir/../fixtures/s1-wrong-length.json") -# end +function parse_test() + @testset "parse" begin + instance = RELOG.parsefile(fixture("instances/s1.json")) + + centers = instance.collection_centers + plants = instance.plants + products = instance.products + location_name_to_plant = Dict(p.location_name => p for p in plants) + product_name_to_product = Dict(p.name => p for p in products) + + @test length(centers) == 10 + @test centers[1].name == "C1" + @test centers[1].latitude == 7 + @test centers[1].latitude == 7 + @test centers[1].longitude == 7 + @test centers[1].amount == [934.56, 934.56] + @test centers[1].product.name == "P1" + + @test length(plants) == 6 + + plant = location_name_to_plant["L1"] + @test plant.plant_name == "F1" + @test plant.location_name == "L1" + @test plant.input.name == "P1" + @test plant.latitude == 0 + @test plant.longitude == 0 + + @test length(plant.sizes) == 2 + @test plant.sizes[1].capacity == 250 + @test plant.sizes[1].opening_cost == [500, 500] + @test plant.sizes[1].fixed_operating_cost == [30, 30] + @test plant.sizes[1].variable_operating_cost == [30, 30] + @test plant.sizes[2].capacity == 1000 + @test plant.sizes[2].opening_cost == [1250, 1250] + @test plant.sizes[2].fixed_operating_cost == [30, 30] + @test plant.sizes[2].variable_operating_cost == [30, 30] + + p1 = product_name_to_product["P1"] + @test p1.disposal_limit == [1.0, 1.0] + @test p1.disposal_cost == [-1000.0, -1000.0] + + p2 = product_name_to_product["P2"] + @test p2.disposal_limit == [0.0, 0.0] + @test p2.disposal_cost == [0.0, 0.0] + + p3 = product_name_to_product["P3"] + @test length(plant.output) == 2 + @test plant.output[p2] == 0.2 + @test plant.output[p3] == 0.5 + @test plant.disposal_limit[p2] == [1, 1] + @test plant.disposal_limit[p3] == [1, 1] + @test plant.disposal_cost[p2] == [-10, -10] + @test plant.disposal_cost[p3] == [-10, -10] + + plant = location_name_to_plant["L3"] + @test plant.location_name == "L3" + @test plant.input.name == "P2" + @test plant.latitude == 25 + @test plant.longitude == 65 + + @test length(plant.sizes) == 2 + @test plant.sizes[1].capacity == 1000.0 + @test plant.sizes[1].opening_cost == [3000, 3000] + @test plant.sizes[1].fixed_operating_cost == [50, 50] + @test plant.sizes[1].variable_operating_cost == [50, 50] + @test plant.sizes[1] == plant.sizes[2] + + p4 = product_name_to_product["P4"] + @test plant.output[p3] == 0.05 + @test plant.output[p4] == 0.8 + @test plant.disposal_limit[p3] == [1e8, 1e8] + @test plant.disposal_limit[p4] == [0, 0] + end + + @testset "parse (geodb)" begin + instance = RELOG.parsefile(fixture("instances/s2.json")) + + centers = instance.collection_centers + @test centers[1].name == "C1" + @test centers[1].latitude == 41.83956 + @test centers[1].longitude == -88.08857 + end + + # @testset "parse (invalid)" begin + # @test_throws ErrorException RELOG.parsefile(fixture("s1-wrong-length.json")) + # end +end \ No newline at end of file diff --git a/test/model/build_test.jl b/test/model/build_test.jl index e85f6a2..efd191a 100644 --- a/test/model/build_test.jl +++ b/test/model/build_test.jl @@ -3,35 +3,36 @@ using RELOG, HiGHS, JuMP, Printf, JSON, MathOptInterface.FileFormats -@testset "build" begin - basedir = dirname(@__FILE__) - instance = RELOG.parsefile("$basedir/../../instances/s1.json") - graph = RELOG.build_graph(instance) - model = RELOG.build_model(instance, graph, HiGHS.Optimizer) - - process_node_by_location_name = - Dict(n.location.location_name => n for n in graph.process_nodes) - - shipping_node_by_loc_and_prod_names = Dict( - (n.location.location_name, n.product.name) => n for n in graph.plant_shipping_nodes - ) - - @test length(model[1, :open_plant]) == 12 - @test length(model[2, :flow]) == 76 - @test length(model[2, :plant_dispose]) == 16 - @test length(model[2, :capacity]) == 12 - @test length(model[2, :expansion]) == 12 - - # l1 = process_node_by_location_name["L1"] - # v = model[2, :capacity][l1.index, 1] - # @test lower_bound(v) == 0.0 - # @test upper_bound(v) == 1000.0 - - # v = model[2, :expansion][l1.index, 1] - # @test lower_bound(v) == 0.0 - # @test upper_bound(v) == 750.0 - - # v = model[2, :plant_dispose][shipping_node_by_loc_and_prod_names["L1", "P2"].index, 1] - # @test lower_bound(v) == 0.0 - # @test upper_bound(v) == 1.0 -end +function model_build_test() + @testset "build" begin + instance = RELOG.parsefile(fixture("instances/s1.json")) + graph = RELOG.build_graph(instance) + model = RELOG.build_model(instance, graph, HiGHS.Optimizer) + + process_node_by_location_name = + Dict(n.location.location_name => n for n in graph.process_nodes) + + shipping_node_by_loc_and_prod_names = Dict( + (n.location.location_name, n.product.name) => n for n in graph.plant_shipping_nodes + ) + + @test length(model[1, :open_plant]) == 12 + @test length(model[2, :flow]) == 76 + @test length(model[2, :plant_dispose]) == 16 + @test length(model[2, :capacity]) == 12 + @test length(model[2, :expansion]) == 12 + + # l1 = process_node_by_location_name["L1"] + # v = model[2, :capacity][l1.index, 1] + # @test lower_bound(v) == 0.0 + # @test upper_bound(v) == 1000.0 + + # v = model[2, :expansion][l1.index, 1] + # @test lower_bound(v) == 0.0 + # @test upper_bound(v) == 750.0 + + # v = model[2, :plant_dispose][shipping_node_by_loc_and_prod_names["L1", "P2"].index, 1] + # @test lower_bound(v) == 0.0 + # @test upper_bound(v) == 1.0 + end +end \ No newline at end of file diff --git a/test/model/solve_test.jl b/test/model/solve_test.jl index 0d63874..096fd98 100644 --- a/test/model/solve_test.jl +++ b/test/model/solve_test.jl @@ -5,63 +5,65 @@ using RELOG, JuMP, Printf, JSON, MathOptInterface.FileFormats basedir = dirname(@__FILE__) -@testset "solve (exact)" begin - solution = RELOG.solve("$basedir/../../instances/s1.json") +function model_solve_test() + @testset "solve (exact)" begin + solution = RELOG.solve(fixture("instances/s1.json")) - solution_filename = tempname() - RELOG.write(solution, solution_filename) - @test isfile(solution_filename) + solution_filename = tempname() + RELOG.write(solution, solution_filename) + @test isfile(solution_filename) - @test "Costs" in keys(solution) - @test "Fixed operating (\$)" in keys(solution["Costs"]) - @test "Transportation (\$)" in keys(solution["Costs"]) - @test "Variable operating (\$)" in keys(solution["Costs"]) - @test "Total (\$)" in keys(solution["Costs"]) + @test "Costs" in keys(solution) + @test "Fixed operating (\$)" in keys(solution["Costs"]) + @test "Transportation (\$)" in keys(solution["Costs"]) + @test "Variable operating (\$)" in keys(solution["Costs"]) + @test "Total (\$)" in keys(solution["Costs"]) - @test "Plants" in keys(solution) - @test "F1" in keys(solution["Plants"]) - @test "F2" in keys(solution["Plants"]) - @test "F3" in keys(solution["Plants"]) - @test "F4" in keys(solution["Plants"]) + @test "Plants" in keys(solution) + @test "F1" in keys(solution["Plants"]) + @test "F2" in keys(solution["Plants"]) + @test "F3" in keys(solution["Plants"]) + @test "F4" in keys(solution["Plants"]) - @test "Products" in keys(solution) - @test "P1" in keys(solution["Products"]) - @test "C1" in keys(solution["Products"]["P1"]) - @test "Dispose (tonne)" in keys(solution["Products"]["P1"]["C1"]) + @test "Products" in keys(solution) + @test "P1" in keys(solution["Products"]) + @test "C1" in keys(solution["Products"]["P1"]) + @test "Dispose (tonne)" in keys(solution["Products"]["P1"]["C1"]) - total_disposal = - sum([loc["Dispose (tonne)"] for loc in values(solution["Products"]["P1"])]) - @test total_disposal == [1.0, 1.0] -end + total_disposal = + sum([loc["Dispose (tonne)"] for loc in values(solution["Products"]["P1"])]) + @test total_disposal == [1.0, 1.0] + end -@testset "solve (heuristic)" begin - # Should not crash - solution = RELOG.solve("$basedir/../../instances/s1.json", heuristic = true) -end + @testset "solve (heuristic)" begin + # Should not crash + solution = RELOG.solve(fixture("instances/s1.json"), heuristic = true) + end -@testset "solve (infeasible)" begin - json = JSON.parsefile("$basedir/../../instances/s1.json") - for (location_name, location_dict) in json["products"]["P1"]["initial amounts"] - location_dict["amount (tonne)"] *= 1000 + @testset "solve (infeasible)" begin + json = JSON.parsefile(fixture("instances/s1.json")) + for (location_name, location_dict) in json["products"]["P1"]["initial amounts"] + location_dict["amount (tonne)"] *= 1000 + end + @test_throws ErrorException("No solution available") RELOG.solve(RELOG.parse(json)) end - @test_throws ErrorException("No solution available") RELOG.solve(RELOG.parse(json)) -end -@testset "solve (with storage)" begin - basedir = dirname(@__FILE__) - filename = "$basedir/../fixtures/storage.json" - instance = RELOG.parsefile(filename) - @test instance.plants[1].storage_limit == 50.0 - @test instance.plants[1].storage_cost == [2.0, 1.5, 1.0] + @testset "solve (with storage)" begin + basedir = dirname(@__FILE__) + filename = "$basedir/../fixtures/storage.json" + instance = RELOG.parsefile(filename) + @test instance.plants[1].storage_limit == 50.0 + @test instance.plants[1].storage_cost == [2.0, 1.5, 1.0] - solution = RELOG.solve(filename) - plant_dict = solution["Plants"]["mega plant"]["Chicago"] - @test plant_dict["Variable operating cost (\$)"] == [500.0, 0.0, 100.0] - @test plant_dict["Process (tonne)"] == [50.0, 0.0, 50.0] - @test plant_dict["Storage (tonne)"] == [50.0, 50.0, 0.0] - @test plant_dict["Storage cost (\$)"] == [100.0, 75.0, 0.0] + solution = RELOG.solve(filename) + plant_dict = solution["Plants"]["mega plant"]["Chicago"] + @test plant_dict["Variable operating cost (\$)"] == [500.0, 0.0, 100.0] + @test plant_dict["Process (tonne)"] == [50.0, 0.0, 50.0] + @test plant_dict["Storage (tonne)"] == [50.0, 50.0, 0.0] + @test plant_dict["Storage cost (\$)"] == [100.0, 75.0, 0.0] - @test solution["Costs"]["Variable operating (\$)"] == [500.0, 0.0, 100.0] - @test solution["Costs"]["Storage (\$)"] == [100.0, 75.0, 0.0] - @test solution["Costs"]["Total (\$)"] == [600.0, 75.0, 100.0] -end + @test solution["Costs"]["Variable operating (\$)"] == [500.0, 0.0, 100.0] + @test solution["Costs"]["Storage (\$)"] == [100.0, 75.0, 0.0] + @test solution["Costs"]["Total (\$)"] == [600.0, 75.0, 100.0] + end +end \ No newline at end of file diff --git a/test/reports_test.jl b/test/reports_test.jl index 9193977..4d1de33 100644 --- a/test/reports_test.jl +++ b/test/reports_test.jl @@ -6,16 +6,18 @@ using RELOG, JSON, GZip basedir = @__DIR__ -@testset "Reports" begin - @testset "from solve" begin - solution = RELOG.solve("$basedir/../instances/s1.json") - tmp_filename = tempname() - # The following should not crash - RELOG.write_plant_emissions_report(solution, tmp_filename) - RELOG.write_plant_outputs_report(solution, tmp_filename) - RELOG.write_plants_report(solution, tmp_filename) - RELOG.write_products_report(solution, tmp_filename) - RELOG.write_transportation_emissions_report(solution, tmp_filename) - RELOG.write_transportation_report(solution, tmp_filename) +function reports_test() + @testset "Reports" begin + @testset "from solve" begin + solution = RELOG.solve(fixture("instances/s1.json")) + tmp_filename = tempname() + # The following should not crash + RELOG.write_plant_emissions_report(solution, tmp_filename) + RELOG.write_plant_outputs_report(solution, tmp_filename) + RELOG.write_plants_report(solution, tmp_filename) + RELOG.write_products_report(solution, tmp_filename) + RELOG.write_transportation_emissions_report(solution, tmp_filename) + RELOG.write_transportation_report(solution, tmp_filename) + end end -end +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 9012e6b..55fda38 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,19 +2,46 @@ # Written by Alinson Santos Xavier using Test +using RELOG +using Revise -@testset "RELOG" begin - @testset "Instance" begin - include("instance/compress_test.jl") - include("instance/geodb_test.jl") - include("instance/parse_test.jl") - end - @testset "Graph" begin - include("graph/build_test.jl") +includet("instance/compress_test.jl") +includet("instance/geodb_test.jl") +includet("instance/parse_test.jl") +includet("graph/build_test.jl") +includet("model/build_test.jl") +includet("model/solve_test.jl") +includet("reports_test.jl") + +function fixture(path) + for candidate in [ + "fixtures/$path", + "test/fixtures/$path" + ] + if isfile(candidate) + return candidate + end end - @testset "Model" begin - include("model/build_test.jl") - include("model/solve_test.jl") + error("Fixture not found: $path") +end + +function runtests() + @testset "RELOG" begin + @testset "Instance" begin + compress_test() + geodb_test() + parse_test() + end + @testset "Graph" begin + graph_build_test() + end + @testset "Model" begin + model_build_test() + model_solve_test() + end + reports_test() end - include("reports_test.jl") + return end + +runtests()