Skip to content

Commit

Permalink
Refactor test with zulu format datetime utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jason810496 committed Nov 16, 2024
1 parent ddd5ecf commit e23c805
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
5 changes: 3 additions & 2 deletions tests/api_fastapi/core_api/routes/public/test_event_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from airflow.utils.session import provide_session

from tests_common.test_utils.db import clear_db_logs, clear_db_runs
from tests_common.test_utils.format_datetime import datetime_zulu_format

pytestmark = pytest.mark.db_test

Expand Down Expand Up @@ -160,14 +161,14 @@ def test_get_event_log(self, test_client, setup, event_log_key, expected_status_

expected_json = {
"event_log_id": event_log_id,
"when": event_log.dttm.isoformat().replace("+00:00", "Z") if event_log.dttm else None,
"when": datetime_zulu_format(event_log.dttm) if event_log.dttm else None,
"dag_id": expected_body.get("dag_id"),
"task_id": expected_body.get("task_id"),
"run_id": expected_body.get("run_id"),
"map_index": event_log.map_index,
"try_number": event_log.try_number,
"event": expected_body.get("event"),
"logical_date": event_log.logical_date.isoformat().replace("+00:00", "Z")
"logical_date": datetime_zulu_format(event_log.logical_date)
if event_log.logical_date
else None,
"owner": expected_body.get("owner"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from airflow.utils.session import provide_session

from tests_common.test_utils.db import clear_db_import_errors
from tests_common.test_utils.format_datetime import datetime_zulu_format_without_ms

pytestmark = pytest.mark.db_test

Expand Down Expand Up @@ -115,7 +116,7 @@ def test_get_import_error(
return
expected_json = {
"import_error_id": import_error_id,
"timestamp": expected_body["timestamp"].isoformat().replace("+00:00", "Z"),
"timestamp": datetime_zulu_format_without_ms(expected_body["timestamp"]),
"filename": expected_body["filename"],
"stack_trace": expected_body["stack_trace"],
}
Expand Down
7 changes: 3 additions & 4 deletions tests/api_fastapi/core_api/routes/public/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from airflow.utils.state import JobState, State

from tests_common.test_utils.db import clear_db_jobs
from tests_common.test_utils.format_datetime import datetime_zulu_format

pytestmark = pytest.mark.db_test

Expand Down Expand Up @@ -154,11 +155,9 @@ def test_get_jobs(
"dag_id": None,
"state": "running",
"job_type": "SchedulerJob",
"start_date": self.scheduler_jobs[idx].start_date.isoformat().replace("+00:00", "Z"),
"start_date": datetime_zulu_format(self.scheduler_jobs[idx].start_date),
"end_date": None,
"latest_heartbeat": self.scheduler_jobs[idx]
.latest_heartbeat.isoformat()
.replace("+00:00", "Z"),
"latest_heartbeat": datetime_zulu_format(self.scheduler_jobs[idx].latest_heartbeat),
"executor_class": None,
"hostname": self.scheduler_jobs[idx].hostname,
"unixname": self.scheduler_jobs[idx].unixname,
Expand Down
29 changes: 29 additions & 0 deletions tests_common/test_utils/format_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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 __future__ import annotations

from datetime import datetime


def datetime_zulu_format(dt: datetime) -> str:
"""Format a datetime object to a string in Zulu time."""
return dt.strftime("%Y-%m-%dT%H:%M:%S.%fZ")


def datetime_zulu_format_without_ms(dt: datetime) -> str:
"""Format a datetime object to a string in Zulu time without milliseconds."""
return dt.strftime("%Y-%m-%dT%H:%M:%SZ")

0 comments on commit e23c805

Please sign in to comment.