Skip to content

Commit 957855f

Browse files
gnawinabelsiqueira
andauthored
Add validation for flow_both (#1281)
* Add validation flow_both * Fix existing flow_both data in a few places * Fix minimum data in the docs * Add a comment * Update docs/src/10-tutorials/17-workflow.md Co-authored-by: Abel Soares Siqueira <nepper271@gmail.com> --------- Co-authored-by: Abel Soares Siqueira <nepper271@gmail.com>
1 parent ac1a879 commit 957855f

6 files changed

Lines changed: 90 additions & 549 deletions

File tree

benchmark/EU/flow-both.csv

Lines changed: 0 additions & 534 deletions
Large diffs are not rendered by default.

docs/src/10-tutorials/17-workflow.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,18 @@ DuckDB.query(
417417
connection,
418418
"CREATE TABLE flow_both AS
419419
SELECT
420-
from_asset,
421-
to_asset,
422-
year AS milestone_year,
423-
year AS commission_year,
424-
initial_export_units,
425-
initial_import_units,
420+
t_flow_yearly.from_asset,
421+
t_flow_yearly.to_asset,
422+
t_flow_yearly.year AS milestone_year,
423+
t_flow_yearly.year AS commission_year,
424+
t_flow_yearly.initial_export_units,
425+
t_flow_yearly.initial_import_units,
426426
FROM t_flow_yearly
427-
ORDER by from_asset, to_asset
427+
LEFT JOIN flow
428+
ON flow.from_asset = t_flow_yearly.from_asset
429+
AND flow.to_asset = t_flow_yearly.to_asset
430+
WHERE flow.is_transport = TRUE -- flow_both must only contain transport flows
431+
ORDER by t_flow_yearly.from_asset, t_flow_yearly.to_asset
428432
"
429433
)
430434
```

docs/src/20-user-guide/50-schemas.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ data = Dict(
8787
# Basic flow data
8888
"flow" => DataFrame(:from_asset => ["some_producer"], :to_asset => ["some_consumer"]),
8989
"flow_both" => DataFrame(
90-
:from_asset => ["some_producer"],
91-
:to_asset => ["some_consumer"],
92-
:commission_year => [2030],
93-
:milestone_year => [2030],
90+
:from_asset => String[],
91+
:to_asset => String[],
92+
:commission_year => Int[],
93+
:milestone_year => Int[],
9494
),
9595
"flow_commission" => DataFrame(
9696
:from_asset => ["some_producer"],

src/data-validation.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ function validate_data!(connection)
3636
_validate_only_transport_flows_are_investable!,
3737
false,
3838
),
39+
(
40+
"flow_both only contain transport flows",
41+
_validate_flow_both_table_does_not_contain_non_transport_flows!,
42+
false,
43+
),
3944
("group consistency between tables", _validate_group_consistency!, false),
4045
(
4146
"data consistency for simple investment",
@@ -190,6 +195,30 @@ function _validate_only_transport_flows_are_investable!(connection)
190195
return error_messages
191196
end
192197

198+
function _validate_flow_both_table_does_not_contain_non_transport_flows!(connection)
199+
# In principle, we should also check all transport flows are covered.
200+
# But that is tested elsewhere, i.e., in _validate_simple_method_data_consistency!()
201+
error_messages = String[]
202+
203+
for row in DuckDB.query(
204+
connection,
205+
"SELECT flow_both.from_asset, flow_both.to_asset, flow_both.milestone_year, flow_both.commission_year
206+
FROM flow_both
207+
LEFT JOIN flow
208+
ON flow.from_asset = flow_both.from_asset
209+
AND flow.to_asset = flow_both.to_asset
210+
WHERE flow.is_transport = FALSE
211+
",
212+
)
213+
push!(
214+
error_messages,
215+
"Unexpected (flow=('$(row.from_asset)', '$(row.to_asset)'), milestone_year=$(row.milestone_year), commission_year=$(row.commission_year)) in 'flow_both' because 'flow_both' should only contain transport flows.",
216+
)
217+
end
218+
219+
return error_messages
220+
end
221+
193222
function _validate_foreign_key!(
194223
connection,
195224
table_name,

test/data-simplest.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ const simplest_data = Dict(
4141
# Basic flow data
4242
"flow" => DataFrame(:from_asset => ["some_producer"], :to_asset => ["some_consumer"]),
4343
"flow_both" => DataFrame(
44-
:from_asset => ["some_producer"],
45-
:to_asset => ["some_consumer"],
46-
:commission_year => [2030],
47-
:milestone_year => [2030],
44+
:from_asset => String[],
45+
:to_asset => String[],
46+
:commission_year => Int[],
47+
:milestone_year => Int[],
4848
),
4949
"flow_commission" => DataFrame(
5050
:from_asset => ["some_producer"],

test/test-data-validation.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,48 @@ end
146146
end
147147
end
148148

149+
@testset "Check flow_both does not only contain non-transport flows" begin
150+
@testset "Using fake data" begin
151+
flow = DataFrame(
152+
:from_asset => ["A1", "A2"],
153+
:to_asset => ["B", "B"],
154+
:is_transport => [false, true],
155+
)
156+
flow_both = DataFrame(
157+
:from_asset => ["A1", "A2"],
158+
:to_asset => ["B", "B"],
159+
:milestone_year => [1, 2],
160+
:commission_year => [1, 2],
161+
)
162+
connection = DBInterface.connect(DuckDB.DB)
163+
DuckDB.register_data_frame(connection, flow, "flow")
164+
DuckDB.register_data_frame(connection, flow_both, "flow_both")
165+
166+
error_messages =
167+
TEM._validate_flow_both_table_does_not_contain_non_transport_flows!(connection)
168+
@test error_messages == [
169+
"Unexpected (flow=('A1', 'B'), milestone_year=1, commission_year=1) in 'flow_both' because 'flow_both' should only contain transport flows.",
170+
]
171+
end
172+
173+
@testset "Using Multi-year data" begin
174+
connection = _multi_year_fixture()
175+
DuckDB.query(
176+
connection,
177+
"""
178+
INSERT INTO flow_both (from_asset, to_asset, milestone_year, commission_year)
179+
VALUES ('wind', 'demand', 2030, 2030);
180+
""",
181+
)
182+
183+
error_messages =
184+
TEM._validate_flow_both_table_does_not_contain_non_transport_flows!(connection)
185+
@test error_messages == [
186+
"Unexpected (flow=('wind', 'demand'), milestone_year=2030, commission_year=2030) in 'flow_both' because 'flow_both' should only contain transport flows.",
187+
]
188+
end
189+
end
190+
149191
@testset "Check that foreign keys are valid" begin
150192
@testset "Using fake data" begin
151193
# Main table

0 commit comments

Comments
 (0)