Skip to content

Commit

Permalink
SorrTask-891 Unit tests for compute epoch function (causify-ai#901)
Browse files Browse the repository at this point in the history
* Issue 891 - Unit test code skeleton

* Issue-891 3 unit tests with each available unit value for Series input.

* Issue-891 Unit test of compute epoch function for dataframe input with default unit

* Adding missed full stop for comment

* PR comment fixes

* Removing todo from function

* PR comment fixes

---------

Co-authored-by: Aishwarya Nidhi <aishwaryanidhi@Aishwaryas-Air.cgocable.net>
Co-authored-by: Samarth KaPatel <samarth.kapatel5@gmail.com>
  • Loading branch information
3 people authored and Dev Karan Suresh committed May 8, 2024
1 parent 690aee4 commit 8628788
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
1 change: 0 additions & 1 deletion core/finance/prediction_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def compute_bar_start_timestamps(
return srs


# TODO(Paul): Add unit tests.
def compute_epoch(
data: Union[pd.Series, pd.DataFrame], *, unit: Optional[str] = None
) -> Union[pd.Series, pd.DataFrame]:
Expand Down
136 changes: 136 additions & 0 deletions core/finance/test/test_prediction_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,139 @@ def _get_data() -> pd.DataFrame:
)
df.index.freq = "T"
return df


# #############################################################################


class TestComputeEpoch(hunitest.TestCase):
"""
Test the computation of epoch time series with different units.
"""

def helper(self) -> pd.Series:
"""
Fetch input data for test.
"""
timestamp_index = pd.date_range("2024-01-01", periods=10, freq="T")
close = list(range(200, 210))
data = {"close": close}
srs = pd.Series(data=data, index=timestamp_index)
return srs

def test1(self) -> None:
"""
Check that epoch is computed correctly for minute unit.
"""
unit = "minute"
srs = self.helper()
result = cfiprpro.compute_epoch(srs, unit=unit)
# Define expected values.
expected_length = 10
expected_column_value = None
expected_signature = r"""
minute
2024-01-01 00:00:00 28401120
2024-01-01 00:01:00 28401121
2024-01-01 00:02:00 28401122
2024-01-01 00:03:00 28401123
2024-01-01 00:04:00 28401124
2024-01-01 00:05:00 28401125
2024-01-01 00:06:00 28401126
2024-01-01 00:07:00 28401127
2024-01-01 00:08:00 28401128
2024-01-01 00:09:00 28401129
"""
# Check signature.
self.check_srs_output(
result, expected_length, expected_column_value, expected_signature
)

def test2(self) -> None:
"""
Check that epoch is computed correctly for second unit.
"""
unit = "second"
srs = self.helper()
result = cfiprpro.compute_epoch(srs, unit=unit)
# Define expected values.
expected_length = 10
expected_column_value = None
expected_signature = r"""
second
2024-01-01 00:00:00 1704067200
2024-01-01 00:01:00 1704067260
2024-01-01 00:02:00 1704067320
2024-01-01 00:03:00 1704067380
2024-01-01 00:04:00 1704067440
2024-01-01 00:05:00 1704067500
2024-01-01 00:06:00 1704067560
2024-01-01 00:07:00 1704067620
2024-01-01 00:08:00 1704067680
2024-01-01 00:09:00 1704067740
"""
# Check signature.
self.check_srs_output(
result, expected_length, expected_column_value, expected_signature
)

def test3(self) -> None:
"""
Check that epoch is computed correctly for nanosecond unit.
"""
unit = "nanosecond"
srs = self.helper()
result = cfiprpro.compute_epoch(srs, unit=unit)
# Define expected values.
expected_length = 10
expected_column_value = None
expected_signature = r"""
nanosecond
2024-01-01 00:00:00 1704067200000000000
2024-01-01 00:01:00 1704067260000000000
2024-01-01 00:02:00 1704067320000000000
2024-01-01 00:03:00 1704067380000000000
2024-01-01 00:04:00 1704067440000000000
2024-01-01 00:05:00 1704067500000000000
2024-01-01 00:06:00 1704067560000000000
2024-01-01 00:07:00 1704067620000000000
2024-01-01 00:08:00 1704067680000000000
2024-01-01 00:09:00 1704067740000000000
"""
# Check signature.
self.check_srs_output(
result, expected_length, expected_column_value, expected_signature
)

def test4(self) -> None:
"""
Check that epoch is computed correctly for dataframe input.
"""
srs = self.helper()
df = srs.to_frame()
result = cfiprpro.compute_epoch(df)
# Define expected values.
expected_length = 10
expected_column_value = None
expected_signature = r"""
# df=
index=[2024-01-01 00:00:00, 2024-01-01 00:09:00]
columns=minute
shape=(10, 1)
minute
2024-01-01 00:00:00 28401120
2024-01-01 00:01:00 28401121
2024-01-01 00:02:00 28401122
...
2024-01-01 00:07:00 28401127
2024-01-01 00:08:00 28401128
2024-01-01 00:09:00 28401129
"""
# Check signature.
self.check_df_output(
result,
expected_length,
expected_column_value,
expected_column_value,
expected_signature,
)

0 comments on commit 8628788

Please sign in to comment.