Skip to content

Commit

Permalink
add tests, can't run them yet (borgbackup#8338)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-gn committed Aug 26, 2024
1 parent 454ff75 commit b5c7df6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/borg/helpers/progress.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import json
import time
import typing

from ..logger import create_logger

Expand All @@ -24,9 +25,15 @@ def __init__(self, msgid=None):
self.id = self.operation_id()
self.msgid = msgid

def make_json(self, *, finished=False, **kwargs):
def make_json(self, *, finished=False, override_time: typing.Optional[float] = None, **kwargs):
kwargs.update(
dict(operation=self.id, msgid=self.msgid, type=self.JSON_TYPE, finished=finished, time=time.time())
dict(
operation=self.id,
msgid=self.msgid,
type=self.JSON_TYPE,
finished=finished,
time=override_time or time.time(),
)
)
return json.dumps(kwargs)

Expand Down
2 changes: 1 addition & 1 deletion src/borg/public/cli_api/v1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Pydantic models that can parse borg 1.x's CLI output.
"""Pydantic models that can parse borg 1.x's JSON output.
The two top-level models are:
Expand Down
20 changes: 20 additions & 0 deletions src/borg/testsuite/public/cli_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import borg.public.cli_api.v1 as v1
from borg.helpers.progress import ProgressIndicatorBase


def test_parse_progress_percent_unfinished():
percent = ProgressIndicatorBase()
override_time = 4567.23
json_output = percent.make_json(finished=False, current=10, override_time=override_time)
assert v1.ProgressPercent.model_validate_json(json_output) == v1.ProgressPercent(
operation=1, msgid=None, finished=False, message=None, current=10, info=None, total=None, time=4567.23
)


def test_parse_progress_percent_finished():
percent = ProgressIndicatorBase()
override_time = 4567.23
json_output = percent.make_json(finished=True, override_time=override_time)
assert v1.ProgressPercent.model_validate_json(json_output) == v1.ProgressPercent(
operation=1, msgid=None, finished=True, message=None, current=None, info=None, total=None, time=override_time
)

0 comments on commit b5c7df6

Please sign in to comment.