2022-02-22 05:59:07 +00:00
|
|
|
import datetime
|
2022-02-23 18:45:00 +00:00
|
|
|
from typing import Mapping
|
2022-02-22 05:59:07 +00:00
|
|
|
|
2022-02-23 18:45:00 +00:00
|
|
|
from fincal.core import AllFrequencies, Frequency, Series, TimeSeriesCore
|
2022-02-22 05:59:07 +00:00
|
|
|
from fincal.fincal import create_date_series
|
2022-02-23 18:45:00 +00:00
|
|
|
from numpy import isin
|
2022-02-22 05:59:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestFrequency:
|
|
|
|
def test_creation(self):
|
|
|
|
D = Frequency('daily', 'days', 1, 1, 'D')
|
|
|
|
assert D.days == 1
|
|
|
|
assert D.symbol == 'D'
|
|
|
|
assert D.name == 'daily'
|
|
|
|
assert D.value == 1
|
|
|
|
assert D.freq_type == 'days'
|
|
|
|
|
|
|
|
|
|
|
|
class TestAllFrequencies:
|
|
|
|
def test_attributes(self):
|
|
|
|
assert hasattr(AllFrequencies, 'D')
|
|
|
|
assert hasattr(AllFrequencies, 'M')
|
|
|
|
assert hasattr(AllFrequencies, 'Q')
|
|
|
|
|
|
|
|
def test_days(self):
|
|
|
|
assert AllFrequencies.D.days == 1
|
|
|
|
assert AllFrequencies.M.days == 30
|
|
|
|
assert AllFrequencies.Q.days == 91
|
|
|
|
|
|
|
|
def test_symbol(self):
|
|
|
|
assert AllFrequencies.H.symbol == 'H'
|
|
|
|
assert AllFrequencies.W.symbol == 'W'
|
|
|
|
|
|
|
|
def test_values(self):
|
|
|
|
assert AllFrequencies.H.value == 6
|
|
|
|
assert AllFrequencies.Y.value == 1
|
|
|
|
|
|
|
|
def test_type(self):
|
|
|
|
assert AllFrequencies.Q.freq_type == 'months'
|
|
|
|
assert AllFrequencies.W.freq_type == 'days'
|
|
|
|
|
|
|
|
|
|
|
|
class TestSeries:
|
|
|
|
def test_creation(self):
|
2022-02-22 07:35:54 +00:00
|
|
|
series = Series([1, 2, 3, 4, 5, 6, 7], data_type='number')
|
2022-02-22 05:59:07 +00:00
|
|
|
assert series.dtype == float
|
|
|
|
assert series[2] == 3
|
|
|
|
|
2022-02-22 07:35:54 +00:00
|
|
|
dates = create_date_series('2021-01-01', '2021-01-31', frequency='D')
|
|
|
|
series = Series(dates, data_type='date')
|
|
|
|
assert series.dtype == datetime.datetime
|
2022-02-23 18:45:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestTimeSeriesCore:
|
|
|
|
data = [('2021-01-01', 220), ('2021-02-01', 230), ('2021-03-01', 240)]
|
|
|
|
|
|
|
|
def test_creation(self):
|
|
|
|
ts = TimeSeriesCore(self.data, frequency='M')
|
|
|
|
assert isinstance(ts, TimeSeriesCore)
|
|
|
|
assert isinstance(ts, Mapping)
|
|
|
|
|
|
|
|
def test_getitem(self):
|
|
|
|
ts = TimeSeriesCore(self.data, frequency='M')
|
|
|
|
assert ts.dates[0] == datetime.datetime(2021, 1, 1, 0, 0)
|
|
|
|
assert ts.values[0] == 220
|
|
|
|
assert ts['2021-01-01'][1] == 220
|
|
|
|
assert len(ts[ts.dates > '2021-01-01']) == 2
|
|
|
|
assert ts[ts.dates == '2021-02-01'].iloc[0][1] == 230
|
|
|
|
assert ts.iloc[2][0] == datetime.datetime(2021, 3, 1)
|
|
|
|
assert len(ts.iloc[:2]) == 2
|