|
| 1 | +# Copyright 2025 CVS Health and/or one of its affiliates |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import time |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +import langfair.utils.display as display_module |
| 20 | +from langfair.utils.display import ( |
| 21 | + ConditionalBarColumn, |
| 22 | + ConditionalSpinnerColumn, |
| 23 | + ConditionalTextColumn, |
| 24 | + ConditionalTextPercentageColumn, |
| 25 | + ConditionalTimeElapsedColumn, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(autouse=True) |
| 30 | +def fast_sleep(monkeypatch): |
| 31 | + monkeypatch.setattr(time, "sleep", lambda x: None) |
| 32 | + |
| 33 | + |
| 34 | +def test_start_progress_bar_without_existing(): |
| 35 | + progress = display_module.start_progress_bar() |
| 36 | + assert progress.live.is_started |
| 37 | + task_id = progress.add_task("[Task]Test", total=10) |
| 38 | + progress.update(task_id, completed=5) |
| 39 | + task = progress.tasks[task_id] |
| 40 | + assert task.completed == 5 |
| 41 | + display_module.stop_progress_bar(progress) |
| 42 | + assert not progress.live.is_started |
| 43 | + |
| 44 | + |
| 45 | +def test_start_progress_bar_with_existing(): |
| 46 | + existing = display_module.start_progress_bar() |
| 47 | + progress = display_module.start_progress_bar(existing) |
| 48 | + assert progress is existing |
| 49 | + assert progress.live.is_started |
| 50 | + display_module.stop_progress_bar(progress) |
| 51 | + assert not progress.live.is_started |
| 52 | + |
| 53 | + |
| 54 | +def test_stop_progress_bar_stops(): |
| 55 | + progress = display_module.start_progress_bar() |
| 56 | + display_module.stop_progress_bar(progress) |
| 57 | + assert not progress.live.is_started |
| 58 | + |
| 59 | + |
| 60 | +def test_task_creation_and_update(): |
| 61 | + progress = display_module.start_progress_bar() |
| 62 | + task_id = progress.add_task("[Task]Downloading", total=100) |
| 63 | + progress.update(task_id, completed=40) |
| 64 | + task = progress.tasks[task_id] |
| 65 | + assert task.description == "[Task]Downloading" |
| 66 | + assert task.completed == 40 |
| 67 | + assert task.total == 100 |
| 68 | + display_module.stop_progress_bar(progress) |
| 69 | + |
| 70 | + |
| 71 | +def test_conditional_columns_render_normal_task(): |
| 72 | + progress = display_module.start_progress_bar() |
| 73 | + task_id = progress.add_task("[Task]Processing", total=80) |
| 74 | + progress.update(task_id, completed=20) |
| 75 | + task = progress.tasks[task_id] |
| 76 | + |
| 77 | + # Validate Conditional* behavior driven by description prefixes |
| 78 | + assert "[progress.description]Processing" in ConditionalTextColumn( |
| 79 | + "[progress.description]{task.description}" |
| 80 | + ).render(task) |
| 81 | + |
| 82 | + assert "[progress.percentage]20/80" in ConditionalTextPercentageColumn( |
| 83 | + "[progress.percentage]{task.completed}/{task.total}" |
| 84 | + ).render(task) |
| 85 | + |
| 86 | + display_module.stop_progress_bar(progress) |
| 87 | + |
| 88 | + |
| 89 | +def test_conditional_columns_render_no_progress_bar(): |
| 90 | + progress = display_module.start_progress_bar() |
| 91 | + task_id = progress.add_task("[No Progress Bar]Hidden", total=50) |
| 92 | + progress.update(task_id, completed=10) |
| 93 | + task = progress.tasks[task_id] |
| 94 | + |
| 95 | + assert ConditionalBarColumn().render(task) == "" |
| 96 | + assert ConditionalTimeElapsedColumn().render(task) == "" |
| 97 | + assert ( |
| 98 | + ConditionalTextColumn("[progress.description]{task.description}").render(task) |
| 99 | + == "[progress.description]Hidden" |
| 100 | + ) |
| 101 | + assert ( |
| 102 | + ConditionalTextPercentageColumn( |
| 103 | + "[progress.percentage]{task.completed}/{task.total}" |
| 104 | + ).render(task) |
| 105 | + == "" |
| 106 | + ) |
| 107 | + assert ConditionalSpinnerColumn().render(task) == "" |
0 commit comments