tests: fix test_parse_line

This commit is contained in:
dettlaff 2023-11-29 13:02:59 +04:00
parent 03e6d45279
commit 372eadd306
2 changed files with 42 additions and 40 deletions

View file

@ -8,10 +8,11 @@ from selfprivacy_api.jobs import JobStatus, Jobs, Job
COMPLETED_WITH_ERROR = ( COMPLETED_WITH_ERROR = (
"We are sorry, сompleted with an error, report it to support chat." "Error occured, please report this to the support chat."
) )
RESULT_WAS_NOT_FOUND_ERROR = "We are sorry, garbage collection result was not found, something went wrong, report it to support chat." RESULT_WAS_NOT_FOUND_ERROR = "We are sorry, garbage collection result was not found. " \
CLEAR_COMPLETED = "Cleaning completed." "Something went wrong, please report this to the support chat."
CLEAR_COMPLETED = "Garbage collection completed."
def run_nix_store_print_dead() -> str: def run_nix_store_print_dead() -> str:
@ -32,7 +33,7 @@ def run_nix_collect_garbage() -> Iterable[bytes]:
return process.stdout if process.stdout else iter([]) return process.stdout if process.stdout else iter([])
def parse_line(line: str): def parse_line(job: Job, line: str) -> Job:
""" """
We parse the string for the presence of a final line, We parse the string for the presence of a final line,
with the final amount of space cleared. with the final amount of space cleared.
@ -43,18 +44,21 @@ def parse_line(line: str):
match = re.search(pattern, line) match = re.search(pattern, line)
if match is None: if match is None:
return ( Jobs.update(
JobStatus.ERROR, job=job,
COMPLETED_WITH_ERROR, status=JobStatus.ERROR,
RESULT_WAS_NOT_FOUND_ERROR, status_text=COMPLETED_WITH_ERROR,
error=RESULT_WAS_NOT_FOUND_ERROR,
) )
else: else:
return ( Jobs.update(
JobStatus.FINISHED, job=job,
CLEAR_COMPLETED, status=JobStatus.FINISHED,
f"{match.group(0)} have been cleared", status_text=CLEAR_COMPLETED,
result=f"{match.group(0)} have been cleared",
) )
return job
def process_stream(job: Job, stream: Iterable[bytes], total_dead_packages: int) -> None: def process_stream(job: Job, stream: Iterable[bytes], total_dead_packages: int) -> None:
@ -78,13 +82,7 @@ def process_stream(job: Job, stream: Iterable[bytes], total_dead_packages: int)
prev_progress = percent prev_progress = percent
elif "store paths deleted," in line: elif "store paths deleted," in line:
status = parse_line(line) parse_line(job, line)
Jobs.update(
job=job,
status=status[0],
status_text=status[1],
result=status[2],
)
def get_dead_packages(output) -> Tuple[int, float]: def get_dead_packages(output) -> Tuple[int, float]:

View file

@ -4,7 +4,7 @@
import pytest import pytest
from selfprivacy_api.jobs import JobStatus, Jobs from selfprivacy_api.jobs import JobStatus, Jobs, Job
from selfprivacy_api.jobs.nix_collect_garbage import ( from selfprivacy_api.jobs.nix_collect_garbage import (
get_dead_packages, get_dead_packages,
@ -57,39 +57,43 @@ determining live/dead paths...
log_event = [] log_event = []
@pytest.fixture
def job_reset():
Jobs.reset()
# --- # ---
def test_parse_line(job_reset): def test_parse_line():
txt = "190 store paths deleted, 425.51 MiB freed" txt = "note: currently hard linking saves -0.00 MiB 190 store paths deleted, 425.51 MiB freed"
output = (
JobStatus.FINISHED, job = Jobs.add(
CLEAR_COMPLETED, name="name",
"425.51 MiB have been cleared", type_id="parse_line",
description="description",
) )
assert parse_line(txt) == output
output = parse_line(job, txt)
assert output.result == '425.51 MiB have been cleared'
assert output.status == JobStatus.FINISHED
assert output.error is None
def test_parse_line_with_blank_line(job_reset): def test_parse_line_with_blank_line():
txt = "" txt = ""
output = ( job = Jobs.add(
JobStatus.ERROR, name="name",
COMPLETED_WITH_ERROR, type_id="parse_line",
RESULT_WAS_NOT_FOUND_ERROR, description="description",
) )
assert parse_line(txt) == output
output = parse_line(job, txt)
assert output.error == RESULT_WAS_NOT_FOUND_ERROR
assert output.status_text == COMPLETED_WITH_ERROR
assert output.status == JobStatus.ERROR
def test_get_dead_packages(job_reset): def test_get_dead_packages():
assert get_dead_packages(OUTPUT_PRINT_DEAD) == (5, 20.0) assert get_dead_packages(OUTPUT_PRINT_DEAD) == (5, 20.0)
def test_get_dead_packages_zero(job_reset): def test_get_dead_packages_zero():
assert get_dead_packages("") == (0, 0) assert get_dead_packages("") == (0, 0)