Skip to content

Commit

Permalink
Simplify _estimate_duration in snapshot.py
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Jul 8, 2024
1 parent 6ad5642 commit 94d2485
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
14 changes: 5 additions & 9 deletions src/ert/gui/model/snapshot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import datetime
import logging
from collections import defaultdict
from contextlib import ExitStack
from datetime import datetime, timedelta
from typing import Any, Dict, Final, List, Optional, Sequence, Union, overload

from dateutil import tz
from qtpy.QtCore import QAbstractItemModel, QModelIndex, QObject, QSize, Qt, QVariant
from qtpy.QtGui import QColor, QFont
from typing_extensions import override
Expand Down Expand Up @@ -68,13 +67,10 @@


def _estimate_duration(
start_time: datetime.datetime, end_time: Optional[datetime.datetime] = None
) -> datetime.timedelta:
timezone = None
if start_time.tzname() is not None:
timezone = tz.gettz(start_time.tzname())
start_time: datetime, end_time: Optional[datetime] = None
) -> timedelta:
if not end_time or end_time < start_time:
end_time = datetime.datetime.now(timezone)
end_time = datetime.now(start_time.tzinfo)
return end_time - start_time


Expand Down Expand Up @@ -441,7 +437,7 @@ def _job_data(
start_time, end_time=node.data.get(ids.END_TIME)
)
# There is no method for truncating microseconds, so we remove them
delta -= datetime.timedelta(microseconds=delta.microseconds)
delta -= timedelta(microseconds=delta.microseconds)
return str(delta)

return node.data.get(data_name)
Expand Down
16 changes: 7 additions & 9 deletions tests/unit_tests/gui/model/test_job_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
from datetime import datetime
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -60,8 +60,8 @@ def test_changes(full_snapshot):
assert model.index(0, _id_to_col(ids.STATUS)).data() == FORWARD_MODEL_STATE_START

partial = PartialSnapshot(full_snapshot)
start_time = datetime.datetime(year=2020, month=10, day=27, hour=12)
end_time = datetime.datetime(year=2020, month=10, day=28, hour=13)
start_time = datetime(year=2020, month=10, day=27, hour=12)
end_time = datetime(year=2020, month=10, day=28, hour=13)
partial.update_forward_model(
"0",
"0",
Expand Down Expand Up @@ -102,11 +102,9 @@ def test_duration(mock_datetime, timezone, full_snapshot):
)

partial = PartialSnapshot(full_snapshot)
start_time = datetime.datetime(
year=2020, month=10, day=27, hour=12, tzinfo=timezone
)
# mock only datetime.datetime.now()
mock_datetime.datetime.now.return_value = datetime.datetime(
start_time = datetime(year=2020, month=10, day=27, hour=12, tzinfo=timezone)
# mock only datetime.now()
mock_datetime.now.return_value = datetime(
year=2020,
month=10,
day=28,
Expand All @@ -128,7 +126,7 @@ def test_duration(mock_datetime, timezone, full_snapshot):
assert (
model.index(2, _id_to_col(DURATION), QModelIndex()).data() == "1 day, 1:12:11"
)
mock_datetime.datetime.now.assert_called_once_with(timezone)
mock_datetime.now.assert_called_once_with(timezone)


@pytest.mark.requires_window_manager
Expand Down

0 comments on commit 94d2485

Please sign in to comment.