Skip to content

Commit

Permalink
add first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FriedrichFroebel committed Sep 23, 2023
1 parent a729483 commit 03ac265
Show file tree
Hide file tree
Showing 10 changed files with 767 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
- name: apt update
run:
sudo apt-get update
- name: install APT dependencies
run:
sudo apt-get install fonts-roboto
- name: update PIP
run:
python -m pip install --upgrade pip
Expand Down
2 changes: 1 addition & 1 deletion brother_ql_web/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def main():

cli_parameters = cli.get_parameters()
configuration = Configuration.from_json(cli_parameters.configuration)
configuration = cli.update_configuration_from_parameters(
cli.update_configuration_from_parameters(
configuration=configuration, parameters=cli_parameters
)
logging.basicConfig(level=configuration.server.log_level)
Expand Down
9 changes: 4 additions & 5 deletions brother_ql_web/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ def _choose_default_font(fonts: dict, configuration: Configuration) -> None:

def update_configuration_from_parameters(
parameters: Namespace, configuration: Configuration
) -> Configuration:
):
# Server configuration.
if parameters.port:
configuration.server.port = parameters.port
if parameters.log_level:
# `log_level` will be numeric if parsed from argv, so we enforce the name here.
configuration.server.log_level = logging.getLevelName(parameters.log_level)
level = parameters.log_level
configuration.server.log_level = logging.getLevelName(level) if isinstance(level, int) else level
if parameters.font_folder:
configuration.server.additional_font_folder = parameters.font_folder

Expand All @@ -121,7 +122,7 @@ def update_configuration_from_parameters(
# Configuration issues.
if configuration.label.default_size not in label_sizes:
raise InvalidLabelSize(
"Invalid default label size. Please choose one of the following:\n:"
"Invalid default label size. Please choose one of the following:\n"
+ " ".join(label_sizes)
)

Expand All @@ -135,5 +136,3 @@ def update_configuration_from_parameters(

# Set font data.
_choose_default_font(fonts=fonts, configuration=configuration)

return configuration
11 changes: 8 additions & 3 deletions brother_ql_web/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ def from_json(cls, json_file: str):
name: str = field.name
field_type: str = cast(str, field.type)
field_class = global_variables[field_type]
kwargs_inner = parsed.pop(name)
instance = field_class(**kwargs_inner)
kwargs_inner = parsed.pop(name, None)
if name == "printer" and not kwargs_inner:
raise ValueError("Printer configuration missing")
if not kwargs_inner:
instance = field_class()
else:
instance = field_class(**kwargs_inner)
kwargs[name] = instance
if parsed:
raise ValueError(f"Unknown configuration values: {parsed}")
Expand Down Expand Up @@ -51,7 +56,7 @@ class PrinterConfiguration:
printer: str


@dataclass
@dataclass(frozen=True)
class Font:
family: str
style: str
Expand Down
16 changes: 15 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import logging
from unittest import TestCase # noqa: F401
from functools import cached_property
from pathlib import Path
from unittest import TestCase as _TestCase

from brother_ql_web.configuration import Configuration


def patch_deprecation_warning():
Expand All @@ -22,3 +26,13 @@ def warn(message, *args, **kwargs):


patch_deprecation_warning()


class TestCase(_TestCase):
@cached_property
def example_configuration_path(self) -> str:
return str(Path(__file__).parent.parent / "config.example.json")

@property
def example_configuration(self) -> Configuration:
return Configuration.from_json(self.example_configuration_path)
Loading

0 comments on commit 03ac265

Please sign in to comment.