Make time series measurements with a Sartorius scale.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.6 KiB

5 years ago
""" tests for the sartorius_logger.parsers module """
import pytest
from collections import namedtuple
@pytest.mark.parametrize(
"text,value,unit,seconds",
[
("1", 1, "h", 60 * 60),
("12", 12, "h", 12 * 60 * 60),
("3s", 3, "s", 3),
("4sec", 4, "s", 4),
("5M", 5, "m", 5 * 60),
("6mins", 6, "m", 6 * 60),
("7h", 7, "h", 7 * 60 * 60),
("8hours", 8, "h", 8 * 60 * 60),
("9x", 9, "h", 9 * 60 * 60),
("10s", 10, "s", 10),
("11.5 m", 11, "m", 11 * 60),
],
)
def test_parse_duration(text, value, unit, seconds):
from sartorius_logger.parsers import parse_duration, ParsedDuration
result = parse_duration(text, default_unit="h")
assert isinstance(result, ParsedDuration)
assert result == (value, unit, seconds)
@pytest.mark.parametrize("value", ["m", "", "x1"])
def test_parse_duration_none_if_not_number_on_start(value):
from sartorius_logger.parsers import parse_duration
result = parse_duration(value)
assert result is None
@pytest.mark.parametrize(
"value,expected",
[
("s", "s"),
("m", "m"),
("h", "h"),
("sec", "s"),
("min", "m"),
("hours", "h"),
("seconds", "s"),
("minutes", "m"),
("hours", "h"),
("", "<deafault>"),
("unknown", "<deafault>"),
],
)
def test_normalize_time_unit(value, expected):
from sartorius_logger.parsers import _normalize_time_unit
result = _normalize_time_unit(value, default_unit="<deafault>")
assert result == expected
def test_parse_cli_arguments(mocker):
from sartorius_logger.parsers import parse_cli_arguments
mocked_argparse = mocker.patch(
"sartorius_logger.parsers.argparse.ArgumentParser"
)
mocked_normalize = mocker.patch(
"sartorius_logger.parsers._normalize_cli_arguments"
)
parse_cli_arguments()
assert mocked_argparse.call_count == 1
assert mocked_normalize.call_count == 1
def test_normalize_cli_arguments(mocker):
from sartorius_logger.parsers import (
_normalize_cli_arguments,
Settings,
)
import pathlib
Dummy = namedtuple("Dummy", ["duration", "interval", "output", "port"])
arguments = Dummy("2h", "30min", "New Folder", "usb")
result = _normalize_cli_arguments(arguments)
assert isinstance(result, Settings)
assert result.duration == (2, "h", 2 * 60 * 60)
assert result.interval == (30, "m", 30 * 60)
assert result.port == "usb"
assert isinstance(result.directory, pathlib.Path)
assert result.directory.name == "New Folder"
assert result.directory.parent.name == "Desktop"
@pytest.mark.parametrize(
"value,expected",
[
("", ("example", "Desktop")),
("Some Folder", ("example", "Desktop", "Some Folder")),
("/Some/Folder", ("Some", "Folder",)),
],
)
def test_check_output_directory_path(mocker, value, expected):
from sartorius_logger.parsers import _check_output_directory_path, Path
mocker.patch.object(Path, "home", return_value=Path("/example"))
mocker.patch.object(Path, "exists", return_value=False)
result = _check_output_directory_path(value)
assert result == Path("/").joinpath(*expected)
def test_check_output_directory_path_exist_not_dir(mocker):
from sartorius_logger.parsers import _check_output_directory_path, Path
mocker.patch.object(Path, "home", return_value=Path("/example"))
mocker.patch.object(Path, "exists", return_value=True)
mocker.patch.object(Path, "is_dir", return_value=False)
result = _check_output_directory_path("/some/existing/folder")
assert result == Path("/") / "example" / "Desktop"