From f76494e98aa9d79f2d8f8daed27295082999bc38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lipovsk=C3=BD?= Date: Wed, 22 Jan 2025 17:54:43 +0100 Subject: [PATCH] Converting timestamp object to string to be able to serialize data and export it to JSON. CLOUDDST-25642 --- iib/workers/tasks/fbc_utils.py | 16 +++++++++++++++- .../test_workers/test_tasks/test_fbc_utils.py | 19 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/iib/workers/tasks/fbc_utils.py b/iib/workers/tasks/fbc_utils.py index 21c70f3fc..b536e4394 100644 --- a/iib/workers/tasks/fbc_utils.py +++ b/iib/workers/tasks/fbc_utils.py @@ -5,6 +5,7 @@ import logging import shutil import json +from datetime import datetime from pathlib import Path from typing import Tuple, List @@ -128,6 +129,19 @@ def extract_fbc_fragment(temp_dir: str, fbc_fragment: str) -> Tuple[str, List[st return fbc_fragment_path, operator_packages +def _serialize_datetime(obj: datetime) -> str: + """ + Serialize datetime objects. + + :param obj: datetime object to serialize + :return: JSON serializable object as string. + :rtype: str + """ + if isinstance(obj, datetime): + return obj.isoformat() + raise TypeError(f"Type {type(obj)} is not serializable.") + + def enforce_json_config_dir(config_dir: str) -> None: """ Ensure the files from config dir are in JSON format. @@ -150,5 +164,5 @@ def enforce_json_config_dir(config_dir: str) -> None: with open(in_file, 'r') as yaml_in, open(out_file, 'a') as json_out: data = yaml.load_all(yaml_in) for chunk in data: - json.dump(chunk, json_out) + json.dump(chunk, json_out, default=_serialize_datetime) os.remove(in_file) diff --git a/tests/test_workers/test_tasks/test_fbc_utils.py b/tests/test_workers/test_tasks/test_fbc_utils.py index 24645b754..f46d2d05c 100644 --- a/tests/test_workers/test_tasks/test_fbc_utils.py +++ b/tests/test_workers/test_tasks/test_fbc_utils.py @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-3.0-or-later +import datetime import json import os import tempfile @@ -15,6 +16,7 @@ merge_catalogs_dirs, enforce_json_config_dir, extract_fbc_fragment, + _serialize_datetime, ) @@ -186,9 +188,13 @@ def test_enforce_json_config_dir_multiple_chunks_input(tmpdir): another: data --- one_more: chunk + createdAt: 2025-01-21T07:15:29 """ - expected_result = """{"foo": "bar", "bar": "foo"}{"another": "data"}{"one_more": "chunk"}""" + expected_result = ( + '{"foo": "bar", "bar": "foo"}{"another": "data"}' + '{"one_more": "chunk", "createdAt": "2025-01-21T07:15:29"}' + ) input = os.path.join(tmpdir, f"test_file.yaml") output = os.path.join(tmpdir, f"test_file.json") @@ -216,3 +222,14 @@ def test_extract_fbc_fragment(mock_cffi, mock_osldr, ldr_output, tmpdir): extract_fbc_fragment(tmpdir, test_fbc_fragment) mock_cffi.assert_has_calls([mock.call(test_fbc_fragment, '/configs', fbc_fragment_path)]) mock_osldr.assert_has_calls([mock.call(fbc_fragment_path)]) + + +def test__serialize_datetime(): + assert ( + _serialize_datetime(datetime.datetime.fromisoformat("2025-01-22")) == "2025-01-22T00:00:00" + ) + + +def test__serialize_datetime_raise(): + with pytest.raises(TypeError, match=f"Type is not serializable."): + _serialize_datetime(2025)