|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from hamilton import ad_hoc_utils, driver |
| 19 | +from hamilton.plugins.h_tqdm import ProgressBar |
| 20 | + |
| 21 | + |
| 22 | +def _build_driver(progress_bar: ProgressBar, module_name: str) -> driver.Driver: |
| 23 | + def normalized(raw_value: float) -> float: |
| 24 | + return raw_value / 100.0 |
| 25 | + |
| 26 | + def scaled(normalized: float) -> float: |
| 27 | + return normalized * 10.0 |
| 28 | + |
| 29 | + def total(scaled: float) -> float: |
| 30 | + return scaled + 5.0 |
| 31 | + |
| 32 | + module = ad_hoc_utils.create_temporary_module( |
| 33 | + normalized, scaled, total, module_name=module_name |
| 34 | + ) |
| 35 | + return driver.Builder().with_adapters(progress_bar).with_modules(module).build() |
| 36 | + |
| 37 | + |
| 38 | +def test_progress_bar_reaches_total_with_overrides(): |
| 39 | + """Regression test for #845: bar must hit 100% when overrides are passed.""" |
| 40 | + progress_bar = ProgressBar() |
| 41 | + dr = _build_driver(progress_bar, "tqdm_pipeline_overrides") |
| 42 | + |
| 43 | + dr.execute( |
| 44 | + ["total"], |
| 45 | + overrides={"scaled": 42.0}, |
| 46 | + inputs={"raw_value": 200.0}, |
| 47 | + ) |
| 48 | + |
| 49 | + assert progress_bar.progress_bar.n == progress_bar.progress_bar.total |
| 50 | + |
| 51 | + |
| 52 | +def test_progress_bar_reaches_total_without_overrides(): |
| 53 | + """Bar reaches 100% on a normal successful run.""" |
| 54 | + progress_bar = ProgressBar() |
| 55 | + dr = _build_driver(progress_bar, "tqdm_pipeline_no_overrides") |
| 56 | + |
| 57 | + dr.execute(["total"], inputs={"raw_value": 200.0}) |
| 58 | + |
| 59 | + assert progress_bar.progress_bar.n == progress_bar.progress_bar.total |
| 60 | + |
| 61 | + |
| 62 | +def test_progress_bar_does_not_force_completion_on_failure(): |
| 63 | + """Bar must not be forced to total when execution fails.""" |
| 64 | + |
| 65 | + def step_one(raw_value: float) -> float: |
| 66 | + return raw_value |
| 67 | + |
| 68 | + def step_two(step_one: float) -> float: |
| 69 | + raise RuntimeError("boom") |
| 70 | + |
| 71 | + def step_three(step_two: float) -> float: |
| 72 | + return step_two |
| 73 | + |
| 74 | + module = ad_hoc_utils.create_temporary_module( |
| 75 | + step_one, step_two, step_three, module_name="tqdm_pipeline_failure" |
| 76 | + ) |
| 77 | + progress_bar = ProgressBar() |
| 78 | + dr = driver.Builder().with_adapters(progress_bar).with_modules(module).build() |
| 79 | + |
| 80 | + try: |
| 81 | + dr.execute(["step_three"], inputs={"raw_value": 1.0}) |
| 82 | + except Exception: |
| 83 | + pass |
| 84 | + |
| 85 | + assert progress_bar.progress_bar.n < progress_bar.progress_bar.total |
0 commit comments