From 958ba10dbf41a441df0bfb38701204ea392a184d Mon Sep 17 00:00:00 2001 From: Sebastian Schmidl Date: Sun, 16 Jul 2023 21:37:53 +0200 Subject: [PATCH] Additional custom input tests (#33) * fix: CI configuration 'build' * feat: improve custom input tests --- .github/workflows/build.yml | 4 +- .../custom_input_ts/data_types_timeseries.csv | 6 +++ .../test_custom_input.py | 51 ++++++++++++++----- 3 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 tests/custom_input_ts/data_types_timeseries.csv diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 33721f1..ee7d31c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -92,7 +92,7 @@ jobs: if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') needs: - test - - typecheck + - check runs-on: ubuntu-latest steps: @@ -117,7 +117,7 @@ jobs: if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') needs: - test - - typecheck + - check runs-on: ubuntu-latest steps: diff --git a/tests/custom_input_ts/data_types_timeseries.csv b/tests/custom_input_ts/data_types_timeseries.csv new file mode 100644 index 0000000..9e8d82a --- /dev/null +++ b/tests/custom_input_ts/data_types_timeseries.csv @@ -0,0 +1,6 @@ +ints,strings,bools,floats,nulls +1,a,true,0.187, +2,b,false,4.123, +3,c,false,-7896.1, +4,doc,false,325.1, +-1,ester,true,897.123, diff --git a/tests/test_base_oscillations/test_custom_input.py b/tests/test_base_oscillations/test_custom_input.py index b03f723..d714f8a 100644 --- a/tests/test_base_oscillations/test_custom_input.py +++ b/tests/test_base_oscillations/test_custom_input.py @@ -1,9 +1,9 @@ import unittest from pathlib import Path +import warnings import pandas as pd import numpy as np -import os from numpy.random import SeedSequence from numpy.testing import assert_array_equal @@ -16,6 +16,9 @@ def setUp(self) -> None: self.ctx = GenerationContext(SeedSequence(42)).to_bo() self.input_path1 = Path("tests/custom_input_ts/dummy_timeseries.csv") self.input_path2 = Path("tests/custom_input_ts/dummy_timeseries_2.csv") + self.input_path_datatypes = Path( + "tests/custom_input_ts/data_types_timeseries.csv" + ) self.column_idx = 1 self.length = 100 self.expected_test = ( @@ -140,19 +143,39 @@ def test_input_too_short(self): ) self.assertRegex(str(e.exception).lower(), "less than the desired length") - def test_integer_conversion(self): - df = pd.DataFrame({"data": [1, 2, 3, 4, 5]}) - df.to_csv("test_data.csv", index=False) - custom_input = CustomInput("test_data.csv") - # test if warning is raised - with self.assertWarns(UserWarning): - # Generate the time series data - timeseries = custom_input.generate_only_base( + def test_read_floats(self): + for tpe in ["floats", "nulls"]: + # a warning will raise an error! + with warnings.catch_warnings(): + timeseries = CustomInput().generate_only_base( + ctx=self.ctx, + length=5, + input_timeseries_path_test=self.input_path_datatypes, + use_column_test=tpe, + ) + # data is properly converted + self.assertEqual(timeseries.dtype, np.float_) + + def test_convert_to_float_with_warning(self): + for tpe in ["ints", "bools"]: + with self.assertWarns(UserWarning) as w: + timeseries = CustomInput().generate_only_base( + ctx=self.ctx, + length=5, + input_timeseries_path_test=self.input_path_datatypes, + use_column_test=tpe, + ) + # warning is raised + self.assertRegex(str(w.warning), "automatically converted to float") + # data is properly converted + self.assertEqual(timeseries.dtype, np.float_) + + def test_error_on_string_type(self): + with self.assertRaises(ValueError) as e: + CustomInput().generate_only_base( ctx=self.ctx, length=5, - input_timeseries_path_test="test_data.csv", - use_column_test="data", + input_timeseries_path_test=self.input_path_datatypes, + use_column_test="strings", ) - # test if data is properly converted - self.assertEqual(timeseries.dtype, np.float64) - os.remove("test_data.csv") + self.assertRegex(str(e.exception), "could not convert string to float")