|
| 1 | +@testset "Test add_vintage_flow_sum_constraints!" begin |
| 2 | + # Setup a temporary DuckDB connection and model |
| 3 | + connection = DBInterface.connect(DuckDB.DB) |
| 4 | + model = JuMP.Model() |
| 5 | + |
| 6 | + # Create mock tables for testing using register_data_frame |
| 7 | + # This first table is only necessary because we have a left join of var_flow with the asset table |
| 8 | + table_rows = [("input_1", "semi-compact"), ("input_2", "compact"), ("death_star", "simple")] |
| 9 | + asset = DataFrame(table_rows, [:asset, :investment_method]) |
| 10 | + DuckDB.register_data_frame(connection, asset, "asset") |
| 11 | + |
| 12 | + table_rows = [ |
| 13 | + ("input_1", "death_star", false), |
| 14 | + ("input_2", "death_star", false), |
| 15 | + ("death_star", "input_1", false), |
| 16 | + ("death_star", "input_2", false), |
| 17 | + ] |
| 18 | + flow = DataFrame(table_rows, [:from_asset, :to_asset, :is_transport]) |
| 19 | + DuckDB.register_data_frame(connection, flow, "flow") |
| 20 | + |
| 21 | + table_rows = [ |
| 22 | + (1, "input_1", "death_star", 2025, 1, 1, 1), |
| 23 | + (2, "input_2", "death_star", 2025, 1, 1, 1), |
| 24 | + (3, "death_star", "input_1", 2025, 1, 1, 1), |
| 25 | + (4, "death_star", "input_2", 2025, 1, 1, 1), |
| 26 | + ] |
| 27 | + var_flow = DataFrame( |
| 28 | + table_rows, |
| 29 | + [:id, :from_asset, :to_asset, :year, :rep_period, :time_block_start, :time_block_end], |
| 30 | + ) |
| 31 | + DuckDB.register_data_frame(connection, var_flow, "var_flow") |
| 32 | + |
| 33 | + table_rows = [ |
| 34 | + (1, "input_1", "death_star", 2025, 2025, 1, 1, 1), |
| 35 | + (2, "input_1", "death_star", 2025, 2020, 1, 1, 1), |
| 36 | + ] |
| 37 | + var_vintage_flow = DataFrame( |
| 38 | + table_rows, |
| 39 | + [ |
| 40 | + :id, |
| 41 | + :from_asset, |
| 42 | + :to_asset, |
| 43 | + :milestone_year, |
| 44 | + :commission_year, |
| 45 | + :rep_period, |
| 46 | + :time_block_start, |
| 47 | + :time_block_end, |
| 48 | + ], |
| 49 | + ) |
| 50 | + DuckDB.register_data_frame(connection, var_vintage_flow, "var_vintage_flow") |
| 51 | + |
| 52 | + variables = Dict{Symbol,TulipaEnergyModel.TulipaVariable}( |
| 53 | + key => TulipaEnergyModel.TulipaVariable(connection, "var_$key") for |
| 54 | + key in (:flow, :vintage_flow) |
| 55 | + ) |
| 56 | + TulipaEnergyModel.add_flow_variables!(connection, model, variables) |
| 57 | + TulipaEnergyModel.add_vintage_flow_variables!(connection, model, variables) |
| 58 | + |
| 59 | + table_rows = [(1, "input_1", "death_star", 2025, 1, 1, 1)] |
| 60 | + |
| 61 | + cons_vintage_flow_sum_semi_compact_method = DataFrame( |
| 62 | + table_rows, |
| 63 | + [:id, :from_asset, :to_asset, :year, :rep_period, :time_block_start, :time_block_end], |
| 64 | + ) |
| 65 | + DuckDB.register_data_frame( |
| 66 | + connection, |
| 67 | + cons_vintage_flow_sum_semi_compact_method, |
| 68 | + "cons_vintage_flow_sum_semi_compact_method", |
| 69 | + ) |
| 70 | + |
| 71 | + constraints = let key = :vintage_flow_sum_semi_compact_method |
| 72 | + Dict{Symbol,TulipaEnergyModel.TulipaConstraint}( |
| 73 | + key => TulipaEnergyModel.TulipaConstraint(connection, "cons_$key"), |
| 74 | + ) |
| 75 | + end |
| 76 | + |
| 77 | + TulipaEnergyModel.add_vintage_flow_sum_constraints!(connection, model, variables, constraints) |
| 78 | + |
| 79 | + # Test the constraints |
| 80 | + var_flow = variables[:flow].container |
| 81 | + var_vintage_flow = variables[:vintage_flow].container |
| 82 | + |
| 83 | + expected_con = |
| 84 | + [JuMP.@build_constraint(var_vintage_flow[1] + var_vintage_flow[2] == var_flow[1])] |
| 85 | + |
| 86 | + observed_con = |
| 87 | + [JuMP.constraint_object(con) for con in model[:vintage_flow_sum_semi_compact_method]] |
| 88 | + |
| 89 | + for (expected, observed) in zip(expected_con, observed_con) |
| 90 | + @test _is_constraint_equal(expected, observed) |
| 91 | + end |
| 92 | + |
| 93 | + @test length(expected_con) == length(observed_con) |
| 94 | +end |
0 commit comments