test_slicer.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import pytest
  2. from unittest.mock import patch, MagicMock
  3. import subprocess
  4. from slicer_utils import slice_model
  5. def test_slice_model_success():
  6. # Mock output from PrusaSlicer --info
  7. mock_stdout = """
  8. volume = 12345.678
  9. size_x = 50.0
  10. size_y = 50.0
  11. size_z = 10.0
  12. """
  13. with patch("subprocess.run") as mock_run, \
  14. patch("shutil.which", return_value="/usr/bin/prusa-slicer"):
  15. mock_run.return_value = MagicMock(stdout=mock_stdout, check=True)
  16. result = slice_model("dummy.stl")
  17. assert result is not None
  18. assert result["success"] is True
  19. assert result["filament_g"] == round((12345.678 / 1000.0) * 1.25, 2)
  20. # 12345.678 / 10 = 1234 seconds = 20m 34s -> 20m
  21. assert result["print_time_str"] == "20m"
  22. def test_slice_model_no_slicer():
  23. with patch("shutil.which", return_value=None):
  24. result = slice_model("dummy.stl")
  25. assert result is None
  26. def test_slice_model_parsing_error():
  27. mock_stdout = "no volume here"
  28. with patch("subprocess.run") as mock_run, \
  29. patch("shutil.which", return_value="/usr/bin/prusa-slicer"):
  30. mock_run.return_value = MagicMock(stdout=mock_stdout, check=True)
  31. result = slice_model("dummy.stl")
  32. assert result is None
  33. def test_slice_model_process_error():
  34. with patch("subprocess.run", side_effect=subprocess.CalledProcessError(1, "cmd")):
  35. result = slice_model("dummy.stl")
  36. assert result is None