Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting timestamp object to string to #780

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion iib/workers/tasks/fbc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import shutil
import json
from datetime import datetime
from pathlib import Path
from typing import Tuple, List

Expand Down Expand Up @@ -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.
Expand All @@ -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)
19 changes: 18 additions & 1 deletion tests/test_workers/test_tasks/test_fbc_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import datetime
import json
import os
import tempfile
Expand All @@ -15,6 +16,7 @@
merge_catalogs_dirs,
enforce_json_config_dir,
extract_fbc_fragment,
_serialize_datetime,
)


Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 <class 'int'> is not serializable."):
_serialize_datetime(2025)
Loading