From 2c99e4450b9326625aaa464a27b746af51da4301 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Wed, 7 Aug 2019 14:26:01 +0200 Subject: [PATCH] Add test for DataModelUtil --- .../test_data_model_util.py | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tracetools_analysis/test/tracetools_analysis/test_data_model_util.py diff --git a/tracetools_analysis/test/tracetools_analysis/test_data_model_util.py b/tracetools_analysis/test/tracetools_analysis/test_data_model_util.py new file mode 100644 index 0000000..96e4348 --- /dev/null +++ b/tracetools_analysis/test/tracetools_analysis/test_data_model_util.py @@ -0,0 +1,75 @@ +# Copyright 2019 Robert Bosch GmbH +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from datetime import datetime +from datetime import timezone +import unittest + +from pandas import DataFrame +from pandas.util.testing import assert_frame_equal + +from tracetools_analysis.utils import DataModelUtil + + +class TestDataModelUtil(unittest.TestCase): + + def __init__(self, *args) -> None: + super().__init__( + *args, + ) + + def test_convert_time_columns(self) -> None: + input_df = DataFrame( + data=[ + { + 'timestamp': 1565177400000*1000000, + 'random_thingy': 'abc', + 'some_duration': 3000000, + 'const_number': 123456, + }, + { + 'timestamp': 946684800000*1000000, + 'random_thingy': 'def', + 'some_duration': 10000000, + 'const_number': 789101112, + }, + ], + ) + expected_df = DataFrame( + data=[ + { + 'timestamp': datetime(2019, 8, 7, 11, 30, 0, tzinfo=timezone.utc), + 'random_thingy': 'abc', + 'some_duration': 3, + 'const_number': 123456, + }, + { + 'timestamp': datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc), + 'random_thingy': 'def', + 'some_duration': 10, + 'const_number': 789101112, + }, + ], + ) + result_df = DataModelUtil.convert_time_columns( + input_df, + ['some_duration'], + ['timestamp'], + inplace=True, + ) + assert_frame_equal(result_df, expected_df, check_dtype=False) + + +if __name__ == '__main__': + unittest.main()