Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding testing and validation #188

Merged
merged 8 commits into from
Apr 21, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions tests/test_runScenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,25 @@ def original_df():

def test_add_config_parameter_column__int(original_df):
new_df = add_config_parameter_column(original_df, "new_column", 10)
assert new_df.shape == (5, 2)
assert "new_column" in new_df.columns
assert set(new_df["new_column"]) == set([10])

correct_df = pd.DataFrame({
'sample_number': [1, 2, 3, 4, 5],
'new_column': [10]*5})
pd.testing.assert_frame_equal(new_df, correct_df)


def test_add_config_parameter_column__matrix(original_df):
f = {'matrix': [[9, 8], [7, 6]]}
new_df = add_config_parameter_column(original_df, "new_column", f)
assert new_df.shape == (5, 5)
assert all(new_cols in new_df.columns
for new_cols in ["new_column1_1", "new_column1_2", "new_column2_1", "new_column2_2"])
assert set(new_df["new_column1_1"]) == set([9])
assert set(new_df["new_column1_2"]) == set([8])
assert set(new_df["new_column2_1"]) == set([7])
assert set(new_df["new_column2_2"]) == set([6])
correct_df = pd.DataFrame({
'sample_number': [1, 2, 3, 4, 5],
'new_column1_1': [9]*5,
'new_column1_2': [8]*5,
'new_column2_1': [7]*5,
'new_column2_2': [6]*5,
})
pd.testing.assert_frame_equal(new_df, correct_df)


def test_add_config_parameter_column__random_uniform(original_df):
Expand All @@ -44,10 +48,11 @@ def test_add_config_parameter_column__datetotimestep():
f = {'custom_function': 'DateToTimestep',
'function_kwargs': {'dates': datetime(2020, 3, 1), 'startdate_col': 'startdate'}}
new_df = add_config_parameter_column(df, "new_column", f)
assert new_df.shape == (5, 3)
assert "new_column" in new_df.columns
print(new_df)
assert set(new_df["new_column"]) == set([10])
correct_df = pd.DataFrame({
'sample_number': [1, 2, 3, 4, 5],
'startdate': [datetime(2020, 2, 20)]*5,
'new_column': [10]*5})
pd.testing.assert_frame_equal(new_df, correct_df)


def test_add_config_parameter_column__subtract():
Expand All @@ -57,13 +62,15 @@ def test_add_config_parameter_column__subtract():
f = {'custom_function': 'subtract',
'function_kwargs': {'x1': 'col1', 'x2': 'col2'}}
new_df = add_config_parameter_column(df, "new_column", f)
assert new_df.shape == (5, 4)
assert "new_column" in new_df.columns
assert set(new_df["new_column"]) == set([1])
correct_df = pd.DataFrame({
'sample_number': [1, 2, 3, 4, 5],
'col1': [2, 4, 6, 8, 10],
'col2': [1, 3, 5, 7, 9],
'new_column': [1]*5})
pd.testing.assert_frame_equal(new_df, correct_df)


def test_add_config_parameter_column__error():
f = {'weird_function': {}}
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError, match="Unknown type of parameter"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! I didn't know about the match kwarg. That's useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Josh told me about this one today -- it's so nice!

add_config_parameter_column(pd.DataFrame, "new_column", f)
assert "Unknown type of parameter" in str(e.value)