1
0
Эх сурвалжийг харах

tests: add tests for the status record with different job status changes

trgiangdo 8 сар өмнө
parent
commit
d48ada7db8

+ 2 - 2
taipy/core/job/job.py

@@ -212,7 +212,7 @@ class Job(_Entity, _Labeled):
 
 
     @property
     @property
     @_self_reload(_MANAGER_NAME)
     @_self_reload(_MANAGER_NAME)
-    def pending_time(self) -> Optional[float]:
+    def pending_duration(self) -> Optional[float]:
         """Get the duration of the job in the pending state in seconds.
         """Get the duration of the job in the pending state in seconds.
 
 
         Returns:
         Returns:
@@ -231,7 +231,7 @@ class Job(_Entity, _Labeled):
 
 
     @property
     @property
     @_self_reload(_MANAGER_NAME)
     @_self_reload(_MANAGER_NAME)
-    def blocked_time(self) -> Optional[float]:
+    def blocked_duration(self) -> Optional[float]:
         """Get the duration of the job in the blocked state in seconds.
         """Get the duration of the job in the blocked state in seconds.
 
 
         Returns:
         Returns:

+ 62 - 1
tests/core/job/test_job.py

@@ -9,12 +9,13 @@
 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 # 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.
 # specific language governing permissions and limitations under the License.
 
 
-from datetime import timedelta
+from datetime import datetime, timedelta
 from time import sleep
 from time import sleep
 from typing import Union, cast
 from typing import Union, cast
 from unittest import mock
 from unittest import mock
 from unittest.mock import MagicMock
 from unittest.mock import MagicMock
 
 
+import freezegun
 import pytest
 import pytest
 
 
 from taipy.config.common.scope import Scope
 from taipy.config.common.scope import Scope
@@ -323,6 +324,66 @@ def test_auto_set_and_reload(current_datetime, job_id):
     assert not job_1._is_in_context
     assert not job_1._is_in_context
 
 
 
 
+def test_status_records(job_id):
+    task_1 = Task(config_id="name_1", properties={}, function=_foo, id=TaskId("task_1"))
+    submission = _SubmissionManagerFactory._build_manager()._create(task_1.id, task_1._ID_PREFIX, task_1.config_id)
+    with freezegun.freeze_time("2024-09-25 13:30:30"):
+        job_1 = Job(job_id, task_1, submission.id, "scenario_entity_id")
+    submission.jobs = [job_1]
+
+    _TaskManager._set(task_1)
+    _JobManager._set(job_1)
+
+    assert job_1._status_change_records == {"SUBMITTED": datetime(2024, 9, 25, 13, 30, 30)}
+    assert job_1.submitted_time == datetime(2024, 9, 25, 13, 30, 30)
+    assert job_1.execution_duration is None
+
+    with freezegun.freeze_time("2024-09-25 13:35:30"):
+        job_1.pending()
+    assert job_1._status_change_records == {
+        "SUBMITTED": datetime(2024, 9, 25, 13, 30, 30),
+        "PENDING": datetime(2024, 9, 25, 13, 35, 30),
+    }
+    assert job_1.execution_duration is None
+    with freezegun.freeze_time("2024-09-25 13:36:00"):
+        assert job_1.pending_duration == 30
+
+    with freezegun.freeze_time("2024-09-25 13:40:30"):
+        job_1.blocked()
+    assert job_1._status_change_records == {
+        "SUBMITTED": datetime(2024, 9, 25, 13, 30, 30),
+        "PENDING": datetime(2024, 9, 25, 13, 35, 30),
+        "BLOCKED": datetime(2024, 9, 25, 13, 40, 30),
+    }
+    assert job_1.execution_duration is None
+    with freezegun.freeze_time("2024-09-25 13:41:00"):
+        assert job_1.blocked_duration == 30
+
+    with freezegun.freeze_time("2024-09-25 13:50:30"):
+        job_1.running()
+    assert job_1._status_change_records == {
+        "SUBMITTED": datetime(2024, 9, 25, 13, 30, 30),
+        "PENDING": datetime(2024, 9, 25, 13, 35, 30),
+        "BLOCKED": datetime(2024, 9, 25, 13, 40, 30),
+        "RUNNING": datetime(2024, 9, 25, 13, 50, 30),
+    }
+    assert job_1.run_time == datetime(2024, 9, 25, 13, 50, 30)
+    assert job_1.pending_duration == 900
+    assert job_1.blocked_duration == 600
+    assert job_1.execution_duration > 0
+
+    with freezegun.freeze_time("2024-09-25 13:56:35"):
+        job_1.completed()
+    assert job_1._status_change_records == {
+        "SUBMITTED": datetime(2024, 9, 25, 13, 30, 30),
+        "PENDING": datetime(2024, 9, 25, 13, 35, 30),
+        "BLOCKED": datetime(2024, 9, 25, 13, 40, 30),
+        "RUNNING": datetime(2024, 9, 25, 13, 50, 30),
+        "COMPLETED": datetime(2024, 9, 25, 13, 56, 35),
+    }
+    assert job_1.execution_duration == 365
+
+
 def test_is_deletable():
 def test_is_deletable():
     with mock.patch("taipy.core.job._job_manager._JobManager._is_deletable") as mock_submit:
     with mock.patch("taipy.core.job._job_manager._JobManager._is_deletable") as mock_submit:
         task = Task(config_id="name_1", properties={}, function=_foo, id=TaskId("task_1"))
         task = Task(config_id="name_1", properties={}, function=_foo, id=TaskId("task_1"))