-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest-data-validation.jl
40 lines (37 loc) · 1.92 KB
/
test-data-validation.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const TEM = TulipaEnergyModel
@testset "Test DataValidationException print" begin
# Mostly to appease codecov
error_msg = "DataValidationException: The following issues were found in the data:\n- example"
@test_throws error_msg throw(TEM.DataValidationException(["example"]))
end
@testset "Test duplicate rows" begin
@testset "Using fake data" begin
bad_data = DataFrame(
:asset => ["ccgt", "demand", "wind", "ccgt", "demand"],
:year => [2030, 2030, 2030, 2050, 2050],
:value => [5.0, 10.0, 15.0, 7.0, 12.0],
)
connection = DBInterface.connect(DuckDB.DB)
DuckDB.register_data_frame(connection, bad_data, "bad_data")
@test TEM._validate_no_duplicate_rows!(connection, "bad_data", [:asset, :year]) == []
@test TEM._validate_no_duplicate_rows!(connection, "bad_data", [:asset]) == [
"Table bad_data has duplicate entries for (asset=ccgt)",
"Table bad_data has duplicate entries for (asset=demand)",
]
end
@testset "Duplicating rows of Tiny data" begin
connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, joinpath(@__DIR__, "inputs", "Tiny"))
# Duplicating rows in these specific tables
for table in ("asset", "asset_both", "flow_both")
DuckDB.query(connection, "INSERT INTO $table (FROM $table ORDER BY RANDOM() LIMIT 1)")
end
@test_throws TEM.DataValidationException TEM.create_internal_tables!(connection)
error_messages = TEM._validate_no_duplicate_rows!(connection)
@test length(error_messages) == 3
# These tests assume an order in the validation of the tables
@test occursin("Table asset has duplicate entries", error_messages[1])
@test occursin("Table asset_both has duplicate entries", error_messages[2])
@test occursin("Table flow_both has duplicate entries", error_messages[3])
end
end