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

SorrTask-891 Unit tests for compute epoch function #901

Merged
merged 8 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
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
138 changes: 138 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,141 @@ 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()
# Convert series data to dataframe
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
df = srs.to_frame()
# Testing input with default unit - minute.
aish-nidhi marked this conversation as resolved.
Show resolved Hide resolved
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,
)
Loading