Skip to content

Commit

Permalink
add df checking in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjoonjung-PNNL committed Jun 21, 2023
1 parent 52c83cf commit 95ec7c2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/library/trim_respond_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def TrimRespondLogic(
SPres_max: Union[float, int],
tol: Union[float, int],
controller_type: str,
) -> pd.DataFrame:
) -> Union[None, pd.DataFrame]:
"""Trim and respond logic verification logic.
Args:
Expand All @@ -34,6 +34,17 @@ def TrimRespondLogic(
"""

# check the given arguments type
if not isinstance(df, pd.DataFrame):
logging.error(
f"The type of the `df` arg must be a dataframe. It cannot be {type(df)}."
)
return None

for col in ["Date/Time", "setpoint", "number_of_requests"]:
if col not in df.columns:
logging.error(f"{col} column doesn't exist in the `df`.")
return None

if not isinstance(Td, (float, int)):
logging.error(
f"The type of the `Td` arg must be a float or int. It cannot be {type(Td)}."
Expand Down
38 changes: 38 additions & 0 deletions tests/test_trim_respond_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,44 @@ class TestTrimRespond(unittest.TestCase):
def test_check_args_type(self):
"""Test whether arguments' type is correct."""

# check `data` (type)
with self.assertLogs() as logobs:
TrimRespondLogic(
dict(data),
Td="0",
ignored_requests=2,
SPtrim=-0.04,
SPres=0.06,
SPmin=0.15,
SPmax=1.5,
SPres_max=0.15,
tol=0.01,
controller_type="direct_acting",
)
self.assertEqual(
"ERROR:root:The type of the `df` arg must be a dataframe. It cannot be <class 'dict'>.",
logobs.output[0],
)

# check `data` (missing column)
with self.assertLogs() as logobs:
TrimRespondLogic(
data.drop("setpoint", axis=1),
Td="0",
ignored_requests=2,
SPtrim=-0.04,
SPres=0.06,
SPmin=0.15,
SPmax=1.5,
SPres_max=0.15,
tol=0.01,
controller_type="direct_acting",
)
self.assertEqual(
"ERROR:root:setpoint column doesn't exist in the `df`.",
logobs.output[0],
)

# check `Td`
with self.assertLogs() as logobs:
TrimRespondLogic(
Expand Down

0 comments on commit 95ec7c2

Please sign in to comment.