Skip to content

Commit

Permalink
WIP 2
Browse files Browse the repository at this point in the history
  • Loading branch information
filipkotoucek committed Aug 14, 2024
1 parent 746b4b2 commit f3a512a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
1 change: 1 addition & 0 deletions prusaerrors/sl1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PRINTER_MODEL_PATH = "/run/model"
7 changes: 4 additions & 3 deletions prusaerrors/sl1/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import yaml

from prusaerrors.shared.codes import Category, Code, Printer, unique_codes, Codes

PRINTER_MODEL_PATH = Path("/run/model")
from prusaerrors.sl1 import PRINTER_MODEL_PATH

if "_" not in vars(builtins):

def _(value):
return value


def yaml_codes(src_path: Path):
"""
Add code definitions from YAML source
Expand All @@ -34,7 +34,8 @@ def decor(cls):
assert "Errors" in data

printer = Printer.SL1
model = [x.name for x in PRINTER_MODEL_PATH.iterdir() if x.is_file()]
print(PRINTER_MODEL_PATH)
model = [x.name for x in Path(str(PRINTER_MODEL_PATH)).iterdir() if x.is_file()]
if len(model) == 1:
a = Printer.M1.name.lower()
if a in model:
Expand Down
51 changes: 32 additions & 19 deletions tests/test_sl1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,41 @@
import unittest
from unittest.mock import MagicMock, PropertyMock, patch

from prusaerrors.sl1.codes import Sl1Codes
#from prusaerrors.sl1.codes import Sl1Codes


class MyMagicMock(MagicMock):
def __repr__(self):
return str(self.return_value)


class TestErrors(unittest.TestCase):
def test_str_conversion(self):
self.assertEqual("#10500", str(Sl1Codes.NONE))


def test_code_lookup(self):
with patch("prusaerrors.sl1.codes.PRINTER_MODEL_PATH") as mock:
with TemporaryDirectory() as dir:
mock.return_value = dir

with open(str(dir) + "/sl1", "w+", encoding="utf-8") as f:
self.assertEqual(Sl1Codes.NONE, Sl1Codes.get("#10500"))
with open(str(dir) + "/sl1s", "w+", encoding="utf-8") as f:
self.assertEqual(Sl1Codes.NONE, Sl1Codes.get("#10500"))
with open(str(dir) + "/m1", "w+", encoding="utf-8") as f:
self.assertEqual(Sl1Codes.NONE, Sl1Codes.get("#29500"))

def test_unknown_code_lookup(self):
self.assertEqual(Sl1Codes.UNKNOWN, Sl1Codes.get("random string"))
#def test_str_conversion(self):
# self.assertEqual("#10500", str(Sl1Codes.NONE))

def _test_code_lookup(self, mock: MyMagicMock, printer_model: str, code: str):
with TemporaryDirectory() as temp:
mock.return_value = temp
with open(temp + "/" + printer_model, "w+", encoding="utf-8") as _:
from prusaerrors.sl1.codes import Sl1Codes
a = Sl1Codes.NONE
b = Sl1Codes.get(code)
self.assertEqual(a, b)

@patch("prusaerrors.sl1.PRINTER_MODEL_PATH", new_callable=MyMagicMock)
def test_code_lookup_sl1(self, mock):
self._test_code_lookup(mock, "sl1", "#10500")

@patch("prusaerrors.sl1.PRINTER_MODEL_PATH", new_callable=MyMagicMock)
def test_code_lookup_sl1s(self, mock):
self._test_code_lookup(mock, "sl1s", "#10500")

@patch("prusaerrors.sl1.PRINTER_MODEL_PATH", new_callable=MyMagicMock)
def test_code_lookup_m1(self, mock):
self._test_code_lookup(mock, "m1", "#29500")

# def test_unknown_code_lookup(self):
# self.assertEqual(Sl1Codes.UNKNOWN, Sl1Codes.get("random string"))


if __name__ == "__main__":
Expand Down

0 comments on commit f3a512a

Please sign in to comment.