From c2e2409772e0e80971847a8e767a68fc87f9a8e8 Mon Sep 17 00:00:00 2001 From: Michael Ekstrand Date: Fri, 26 Apr 2024 14:19:49 -0400 Subject: [PATCH] add a mock progress implementation --- progress_api/backends/mock.py | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 progress_api/backends/mock.py diff --git a/progress_api/backends/mock.py b/progress_api/backends/mock.py new file mode 100644 index 0000000..941ed51 --- /dev/null +++ b/progress_api/backends/mock.py @@ -0,0 +1,56 @@ +# This file is part of Progress API. +# Copyright (C) 2023 - 2024 Drexel University and contributors +# Licensed under the MIT license, see LICENSE.md for details. +# SPDX-License-Identifier: MIT + +""" +Mock backend that records progress updates for testing. +""" +from __future__ import annotations + +from typing import Optional + +from .. import api +from . import ProgressBackend, ProgressBarSpec + + +class MockProgressBackend(ProgressBackend): + """ + Progress bar backend that records progress updates in a list for testing. + """ + + record: list[str] + + def __init__(self) -> None: + super().__init__() + self.record = [] + + def create_bar(self, spec: ProgressBarSpec) -> api.Progress: + self.record.append(f"start {spec.label} {spec.total}") + return MockProgress(self, spec) + + +class MockProgress(api.Progress): + backend: MockProgressBackend + + def __init__(self, backend: MockProgressBackend, spec: ProgressBarSpec): + self.backend = backend + self.spec = spec + + def set_label(self, label: Optional[str]): + pass + + def set_total(self, total: int): + pass + + def update( + self, + n: int = 1, + state: Optional[str] = None, + src_state: Optional[str] = None, + metric: int | str | float | None = None, + ): + self.backend.record.append(f"update {self.spec.label} {n} ({src_state} -> {state})") + + def finish(self): + self.backend.record.append(f"finish {self.spec.label}")