Skip to content

Commit

Permalink
add a mock progress implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mdekstrand committed Apr 26, 2024
1 parent b765d4f commit c2e2409
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions progress_api/backends/mock.py
Original file line number Diff line number Diff line change
@@ -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}")

0 comments on commit c2e2409

Please sign in to comment.