Skip to content

Commit

Permalink
update some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorb1 committed Mar 29, 2024
1 parent 29c8c74 commit 0ad7a07
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/otoole/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
>>> converter.convert('my_datafile.txt', 'folder_of_csv_files')
"""

from __future__ import annotations

import logging
Expand Down Expand Up @@ -615,6 +616,7 @@ def _expand_dataframe(
return df

default_df = self._get_default_dataframe(name, input_data, default_values)
# default_df = self._check_index_dtypes(name, self.user_config[name], default_df)

df = pd.concat([df, default_df])
df = df[~df.index.duplicated(keep="first")]
Expand Down
9 changes: 5 additions & 4 deletions src/otoole/read_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,17 @@ def read(
input_data, config_type=config_type
)

input_data = self._check_index(input_data)

if self.write_defaults:
for name in [
names = [
x for x in self.user_config if self.user_config[x]["type"] == "param"
]:
]
for name in names:
input_data[name] = self._expand_dataframe(
name, input_data, default_values
)

input_data = self._check_index(input_data)

return input_data, default_values


Expand Down
102 changes: 84 additions & 18 deletions tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def simple_user_config():
"default": 0,
"short_name": "CAPEX",
},
"DiscountRate": {
"indices": ["REGION"],
"type": "param",
"dtype": "float",
"default": 0.25,
},
"REGION": {
"dtype": "str",
"type": "set",
Expand Down Expand Up @@ -276,14 +282,15 @@ def input_data_single_index_empty(region):
def test_expand_parameters_defaults(
self, user_config, simple_default_values, input_data, parameter, expected
):
write_strategy = DummyWriteStrategy(
user_config=user_config, default_values=simple_default_values
)
write_strategy.input_data = input_data
actual = write_strategy._expand_defaults(
input_data, write_strategy.default_values
read_strategy = DummyReadStrategy(user_config=user_config)
actual = read_strategy._expand_dataframe(
parameter, input_data, simple_default_values
)
assert_frame_equal(actual[parameter], expected)
print("\n")
print(actual.index.dtypes)
print("\n")
print(expected.index.dtypes)
assert_frame_equal(actual, expected)

def test_expand_result_defaults(
self,
Expand All @@ -292,11 +299,11 @@ def test_expand_result_defaults(
simple_input_data,
simple_result_data,
):
write_strategy = DummyWriteStrategy(
user_config=simple_user_config, default_values=simple_default_values
read_strategy = DummyReadStrategy(
user_config=simple_user_config, write_defaults=True
)
actual = write_strategy._expand_defaults(
simple_result_data, write_strategy.default_values, simple_input_data
actual = read_strategy._expand_dataframe(
"NewCapacity", simple_input_data, simple_default_values
)

expected = pd.DataFrame(
Expand All @@ -317,17 +324,76 @@ def test_expand_results_key_error(
self, simple_user_config, simple_result_data, simple_default_values
):
"""When input data is just the result data"""
write_strategy = DummyWriteStrategy(
user_config=simple_user_config,
default_values=simple_default_values,
write_defaults=True,
read_strategy = DummyReadStrategy(
user_config=simple_user_config, write_defaults=True
)

with raises(KeyError, match="REGION"):
write_strategy._expand_defaults(
simple_result_data, write_strategy.default_values
with raises(KeyError, match="SpecifiedAnnualDemand"):
read_strategy._expand_dataframe(
"SpecifiedAnnualDemand", simple_result_data, simple_default_values
)

def defaults_dataframe_single_index(region):
discount_rate_out = pd.DataFrame(
[["SIMPLICITY", 0.25]], columns=["REGION", "VALUE"]
).set_index(["REGION"])
discount_rate_out["VALUE"] = discount_rate_out["VALUE"].astype(float)

data = {
"REGION": region,
}
return data, "DiscountRate", discount_rate_out

def defaults_dataframe_multi_index(region, technology, year):
capex_out = pd.DataFrame(
[
["SIMPLICITY", "NGCC", 2014, -1],
["SIMPLICITY", "NGCC", 2015, -1],
["SIMPLICITY", "NGCC", 2016, -1],
["SIMPLICITY", "HYD1", 2014, -1],
["SIMPLICITY", "HYD1", 2015, -1],
["SIMPLICITY", "HYD1", 2016, -1],
],
columns=["REGION", "TECHNOLOGY", "YEAR", "VALUE"],
).set_index(["REGION", "TECHNOLOGY", "YEAR"])
capex_out["VALUE"] = capex_out["VALUE"].astype(float)

data = {
"TECHNOLOGY": technology,
"YEAR": year,
"REGION": region,
}
return data, "CapitalCost", capex_out

parameter_test_data = [
defaults_dataframe_single_index(region),
defaults_dataframe_multi_index(region, technology, year),
]
parameter_test_data_ids = [
"single_index",
"multi_index",
]

@mark.parametrize(
"input_data,parameter,expected",
parameter_test_data,
ids=parameter_test_data_ids,
)
def test_get_default_dataframe(
self,
simple_user_config,
simple_default_values,
simple_input_data,
input_data,
parameter,
expected,
):
read_strategy = DummyReadStrategy(user_config=simple_user_config)
actual = read_strategy._get_default_dataframe(
parameter, input_data, simple_default_values
)
assert_frame_equal(actual, expected)


class TestReadStrategy:

Expand Down

0 comments on commit 0ad7a07

Please sign in to comment.