From 2267cc1b0837978ddd112d109000e1023fd6f026 Mon Sep 17 00:00:00 2001 From: RTnhN <43109787+RTnhN@users.noreply.github.com> Date: Fri, 27 Sep 2024 20:43:58 -0500 Subject: [PATCH 1/6] Add tests for timestamp formats --- tests/test_timestamp_formats.py | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/test_timestamp_formats.py diff --git a/tests/test_timestamp_formats.py b/tests/test_timestamp_formats.py new file mode 100644 index 0000000..a60fcd7 --- /dev/null +++ b/tests/test_timestamp_formats.py @@ -0,0 +1,62 @@ +import pytest +from datetime import datetime, timezone, timedelta +from logmerger.timestamp_wrapper import TimestampedLineTransformer + +local_time = datetime.now().astimezone() +local_tz = local_time.tzinfo + + +@pytest.mark.parametrize( + "string_date, expected_datetime", + [ + ( + "2023-07-14 08:00:01,000Z Log", + datetime(2023, 7, 14, 8, 0, 1, tzinfo=timezone.utc), + ), + ( + "2023-07-14 08:00:01,123+0200 Log", + datetime(2023, 7, 14, 8, 0, 1, 123000, tzinfo=timezone(timedelta(hours=2))), + ), + ( + "2023-07-14 08:00:01.000Z Log", + datetime(2023, 7, 14, 8, 0, 1, tzinfo=timezone.utc), + ), + ( + "2023-07-14 08:00:01.123+0200 Log", + datetime(2023, 7, 14, 8, 0, 1, 123000, tzinfo=timezone(timedelta(hours=2))), + ), + ( + "2023-07-14 08:00:01 Log", + datetime(2023, 7, 14, 8, 0, 1, tzinfo=local_tz)), + ( + "2023-07-14T08:00:01,000Z Log", + datetime(2023, 7, 14, 8, 0, 1, tzinfo=timezone.utc), + ), + ( + "Jul 14 08:00:01 Log", + datetime(datetime.now().year, 7, 14, 8, 0, 1, tzinfo=local_tz), + ), + ( + "1694561169.550987 Log", + datetime.fromtimestamp(1694561169.550987, tz=timezone.utc), + ), + ( + "1694561169550 Log", + datetime.fromtimestamp(1694561169550 / 1000, tz=timezone.utc), + ), + ( + "1694561169 Log", + datetime.fromtimestamp(1694561169, tz=timezone.utc), + ), + ], +) +def test_make_transformer_from_sample_line(string_date, expected_datetime): + transformer = TimestampedLineTransformer.make_transformer_from_sample_line( + string_date + ) + + parsed_datetime, _ = transformer(string_date) + + parsed_datetime = parsed_datetime.astimezone(timezone.utc) + + assert parsed_datetime == expected_datetime From 8932d6ddadc2c381753845ba1d750533320a478c Mon Sep 17 00:00:00 2001 From: RTnhN <43109787+RTnhN@users.noreply.github.com> Date: Fri, 27 Sep 2024 21:02:24 -0500 Subject: [PATCH 2/6] Add CI workflow --- .github/workflows/Python Version Testing.yml | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/workflows/Python Version Testing.yml diff --git a/.github/workflows/Python Version Testing.yml b/.github/workflows/Python Version Testing.yml new file mode 100644 index 0000000..dcb412c --- /dev/null +++ b/.github/workflows/Python Version Testing.yml @@ -0,0 +1,29 @@ +name: Version Testing + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9", "3.10", "3.11", "3.12"] + + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install tox + python -m pip install -r requirements.txt + - name: Test with tox + run: tox -e py From c8e74a9acf9a0982cb4f1cd2371c267b5b2d447c Mon Sep 17 00:00:00 2001 From: RTnhN <43109787+RTnhN@users.noreply.github.com> Date: Fri, 27 Sep 2024 21:04:46 -0500 Subject: [PATCH 3/6] Add fail fast false option to CI --- .github/workflows/Python Version Testing.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/Python Version Testing.yml b/.github/workflows/Python Version Testing.yml index dcb412c..49f4dcb 100644 --- a/.github/workflows/Python Version Testing.yml +++ b/.github/workflows/Python Version Testing.yml @@ -11,6 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: + fail-fast: false matrix: python-version: ["3.9", "3.10", "3.11", "3.12"] From 673452259f9f069c1f459846056b91e9c94fbfc3 Mon Sep 17 00:00:00 2001 From: RTnhN <43109787+RTnhN@users.noreply.github.com> Date: Fri, 27 Sep 2024 21:13:45 -0500 Subject: [PATCH 4/6] Refactor timestamp format testing --- tests/test_timestamp_formats.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/test_timestamp_formats.py b/tests/test_timestamp_formats.py index a60fcd7..abc5cd6 100644 --- a/tests/test_timestamp_formats.py +++ b/tests/test_timestamp_formats.py @@ -1,10 +1,23 @@ +from pprint import pprint import pytest + from datetime import datetime, timezone, timedelta from logmerger.timestamp_wrapper import TimestampedLineTransformer local_time = datetime.now().astimezone() local_tz = local_time.tzinfo +def _test_timestamp_format_parsing(string_date, expected_datetime): + transformer = TimestampedLineTransformer.make_transformer_from_sample_line( + string_date + ) + + parsed_datetime, _ = transformer(string_date) + parsed_datetime = parsed_datetime.astimezone(timezone.utc) + pprint(parsed_datetime, width=200) + pprint(expected_datetime, width=200) + assert parsed_datetime == expected_datetime + @pytest.mark.parametrize( "string_date, expected_datetime", @@ -50,13 +63,5 @@ ), ], ) -def test_make_transformer_from_sample_line(string_date, expected_datetime): - transformer = TimestampedLineTransformer.make_transformer_from_sample_line( - string_date - ) - - parsed_datetime, _ = transformer(string_date) - - parsed_datetime = parsed_datetime.astimezone(timezone.utc) - - assert parsed_datetime == expected_datetime +def test_timestamp_format_parsing(string_date, expected_datetime): + _test_timestamp_format_parsing(string_date, expected_datetime) From 10343c86570aba56171638086654874d0e6c319d Mon Sep 17 00:00:00 2001 From: RTnhN <43109787+RTnhN@users.noreply.github.com> Date: Sat, 28 Sep 2024 00:44:01 -0500 Subject: [PATCH 5/6] Add details to print statements --- tests/test_timestamp_formats.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_timestamp_formats.py b/tests/test_timestamp_formats.py index abc5cd6..74def2c 100644 --- a/tests/test_timestamp_formats.py +++ b/tests/test_timestamp_formats.py @@ -1,4 +1,3 @@ -from pprint import pprint import pytest from datetime import datetime, timezone, timedelta @@ -14,8 +13,9 @@ def _test_timestamp_format_parsing(string_date, expected_datetime): parsed_datetime, _ = transformer(string_date) parsed_datetime = parsed_datetime.astimezone(timezone.utc) - pprint(parsed_datetime, width=200) - pprint(expected_datetime, width=200) + print(repr(string_date)) + print("Parsed time :", parsed_datetime) + print("Expected time:", expected_datetime) assert parsed_datetime == expected_datetime From 143a4d05206e9c2ae7645d8b46bdaba7e6e8d17f Mon Sep 17 00:00:00 2001 From: RTnhN <43109787+RTnhN@users.noreply.github.com> Date: Sat, 28 Sep 2024 00:51:04 -0500 Subject: [PATCH 6/6] Add annotations to method signatures --- tests/test_timestamp_formats.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_timestamp_formats.py b/tests/test_timestamp_formats.py index 74def2c..bbd537c 100644 --- a/tests/test_timestamp_formats.py +++ b/tests/test_timestamp_formats.py @@ -6,7 +6,8 @@ local_time = datetime.now().astimezone() local_tz = local_time.tzinfo -def _test_timestamp_format_parsing(string_date, expected_datetime): + +def _test_timestamp_format_parsing(string_date: str, expected_datetime: datetime): transformer = TimestampedLineTransformer.make_transformer_from_sample_line( string_date ) @@ -63,5 +64,5 @@ def _test_timestamp_format_parsing(string_date, expected_datetime): ), ], ) -def test_timestamp_format_parsing(string_date, expected_datetime): +def test_timestamp_format_parsing(string_date: str, expected_datetime: datetime): _test_timestamp_format_parsing(string_date, expected_datetime)