"""Filament-Bedarf aus Queue und Spulen.""" import unittest from unittest.mock import MagicMock from grotti.bedarf import compute class BedarfTests(unittest.TestCase): def _rows(self, *lists): mocks = [] for rows in lists: m = MagicMock() m.fetchall.return_value = rows mocks.append(m) return mocks def test_shortfall_when_queue_exceeds_stock(self): conn = MagicMock() conn.execute.side_effect = self._rows( [ { "id": 1, "spool_id": 10, "repeat_total": 2, "repeat_done": 0, "weight_g": 50.0, "filaments": [{"type": "PLA", "color": "Schwarz"}], "spool_material": "PLA", "spool_color": "Schwarz", }, ], [ {"material_type": "PLA", "color": "Schwarz", "remaining_g": 40.0}, ], ) out = compute(conn) self.assertEqual(len(out["needed"]), 1) self.assertAlmostEqual(out["needed"][0]["weight_g"], 100.0) self.assertEqual(len(out["shortfall"]), 1) self.assertAlmostEqual(out["shortfall"][0]["shortfall_g"], 60.0) def test_no_shortfall_when_enough_filament(self): conn = MagicMock() conn.execute.side_effect = self._rows( [ { "id": 1, "spool_id": None, "repeat_total": 1, "repeat_done": 0, "weight_g": 20.0, "filaments": [{"type": "PETG", "color": ""}], "spool_material": None, "spool_color": None, }, ], [ {"material_type": "PETG", "color": "", "remaining_g": 500.0}, ], ) out = compute(conn) self.assertEqual(out["shortfall"], []) if __name__ == "__main__": unittest.main()