From b4d52915720a8954bb7bc040af0e233c2644c4a8 Mon Sep 17 00:00:00 2001 From: gouravkr Date: Tue, 22 Feb 2022 08:25:57 +0530 Subject: [PATCH] added more tests --- testing.ipynb | 28 +++++++++---------- tests/test_fincal.py | 64 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 24 deletions(-) diff --git a/testing.ipynb b/testing.ipynb index 67fb39e..3842ef0 100644 --- a/testing.ipynb +++ b/testing.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "id": "3f7938c0-98e3-43b8-86e8-4f000cda7ce5", "metadata": {}, "outputs": [], @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "4b8ccd5f-dfff-4202-82c4-f66a30c122b6", "metadata": {}, "outputs": [], @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 12, "id": "c52b0c2c-dd01-48dd-9ffa-3147ec9571ef", "metadata": {}, "outputs": [ @@ -50,7 +50,7 @@ "\t (datetime.datetime(2022, 2, 14, 0, 0), 82.53299999999999)], frequency='D')" ] }, - "execution_count": 3, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -62,7 +62,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "id": "9e8ff6c6-3a36-435a-ba87-5b9844c18779", "metadata": {}, "outputs": [ @@ -73,7 +73,7 @@ " (datetime.datetime(2021, 5, 31, 0, 0), 74.85)]" ] }, - "execution_count": 4, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -84,7 +84,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 8, "id": "086d4377-d1b1-4e51-84c0-39dee28ef75e", "metadata": {}, "outputs": [ @@ -100,7 +100,7 @@ "\t (datetime.datetime(2022, 2, 14, 0, 0), 82.53299999999999)], frequency='D')" ] }, - "execution_count": 16, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -111,7 +111,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "id": "6f1226a3-2327-435b-88e7-fd0fdcc8cc1c", "metadata": {}, "outputs": [ @@ -127,7 +127,7 @@ "\t (datetime.datetime(2022, 2, 14, 0, 0), 82.53299999999999)], frequency='D')" ] }, - "execution_count": 5, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -139,7 +139,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "e815edc9-3746-4192-814e-bd27b2771a0c", "metadata": {}, "outputs": [ @@ -158,7 +158,7 @@ " (datetime.datetime(2013, 1, 15, 0, 0), 19.272000000000002)]" ] }, - "execution_count": 9, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -169,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 11, "id": "dc469722-c816-4b57-8d91-7a3b865f86be", "metadata": { "tags": [] @@ -180,7 +180,7 @@ "output_type": "stream", "text": [ "CPU times: total: 15.6 ms\n", - "Wall time: 14 ms\n" + "Wall time: 10 ms\n" ] } ], diff --git a/tests/test_fincal.py b/tests/test_fincal.py index 2424667..0926280 100644 --- a/tests/test_fincal.py +++ b/tests/test_fincal.py @@ -4,7 +4,7 @@ import random from typing import Literal, Sequence import pytest -from fincal.core import Frequency +from fincal.core import AllFrequencies, Frequency, Series from fincal.fincal import TimeSeries, create_date_series THIS_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -53,6 +53,40 @@ def create_test_data( return data +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 TestDateSeries: def test_daily(self): start_date = datetime.datetime(2020, 1, 1) @@ -134,18 +168,28 @@ class TestFincal: data = create_test_data(frequency='D', eomonth=False, n=500, gaps=0.1, month_position='start', date_as_str=True) time_series = TimeSeries(data, frequency="D") ffill_data = time_series.ffill() - assert len(ffill_data) > 498 + assert len(ffill_data) >= 498 ffill_data = time_series.ffill(inplace=True) assert ffill_data is None - assert len(time_series) > 498 + assert len(time_series) >= 498 - def test_slicing(self): + def test_iloc_slicing(self): data = create_test_data(frequency='D', eomonth=False, n=50, gaps=0, month_position='start', date_as_str=True) time_series = TimeSeries(data, frequency="D") - assert time_series[0] is not None - assert time_series[:3] is not None - assert time_series[5:7] is not None - assert isinstance(time_series[0], tuple) - assert isinstance(time_series[10:20], list) - assert len(time_series[10:20]) == 10 + assert time_series.iloc[0] is not None + assert time_series.iloc[:3] is not None + assert time_series.iloc[5:7] is not None + assert isinstance(time_series.iloc[0], tuple) + assert isinstance(time_series.iloc[10:20], list) + assert len(time_series.iloc[10:20]) == 10 + + def test_key_slicing(self): + data = create_test_data(frequency='D', eomonth=False, n=50, gaps=0, month_position='start', date_as_str=True) + time_series = TimeSeries(data, frequency="D") + available_date = time_series.iloc[5][0] + assert time_series[available_date] is not None + assert isinstance(time_series['dates'], Series) + assert isinstance(time_series['values'], Series) + assert len(time_series.dates) == 50 + assert len(time_series.values) == 50