"""Real Tk widget checks with synthetic report data; no database connection.""" import tkinter as tk import unittest import warnings from datetime import date from decimal import Decimal from unittest.mock import Mock, patch import customtkinter as ctk from inventario import InventarioWindow, display_number from inventory_service import build_report from test_inventory import stock, HISTORY class InventoryGuiTests(unittest.TestCase): def setUp(self): self.root = ctk.CTk() self.root.withdraw() self.win = InventarioWindow(self.root, Mock()) self.win.withdraw() self.root.update_idletasks() def tearDown(self): for token in self.root.tk.call("after", "info"): try: # Cancel the timer without deleting another widget's Tcl command. self.root.tk.call("after", "cancel", token) except tk.TclError: pass self.win.destroy() self.root.destroy() def sample(self): return build_report(date(2025, 12, 31), [ HISTORY, stock(Product="A", Quantity=Decimal(10), SamQuantity=Decimal(10)), stock(Product="B", ContainerId=2, Quantity=Decimal(2), SamQuantity=Decimal(2)), ]) def test_no_query_at_open_and_render_zeros(self): self.win.db_client.query_json.assert_not_called() self.win._loaded(self.sample()) values = self.win.tree.item("0", "values") self.assertEqual(values[3:9], ("1", "10", "0", "10", "0", "0")) self.assertEqual(self.win.btn_export.cget("state"), "normal") self.assertIn("31/12/2025", self.win.var_status.get()) def test_selection_populates_lot_and_container_details(self): self.win._loaded(self.sample()) self.win.tree.selection_set("0") self.win._select() self.assertEqual(len(self.win.lot_tree.get_children()), 1) self.assertEqual(len(self.win.detail_tree.get_children()), 1) def test_sort_uses_numbers_not_formatted_strings(self): self.win._loaded(self.sample()) self.win._sort("SAM") self.assertEqual([r["Product"] for r in self.win._rows], ["B", "A"]) self.win._sort("SAM") self.assertEqual([r["Product"] for r in self.win._rows], ["A", "B"]) def test_load_disables_export_and_repeated_requests(self): self.win._loaded(self.sample()) with patch.object(self.win._async, "run") as run: self.win._load() self.assertEqual(self.win.btn_export.cget("state"), "disabled") self.win._load() self.assertEqual(run.call_count, 1) run.call_args.args[0].close() def test_export_uses_result_date_not_edited_entry(self): self.win._loaded(self.sample()) self.win.var_date.set("01/01/2026") with patch("inventario.filedialog.asksaveasfilename", return_value="") as save: self.win._export() self.assertEqual(save.call_args.kwargs["initialfile"], "inventario_20251231.xlsx") def test_decimal_formatting(self): self.assertEqual(display_number(Decimal("0.000000")), "0") self.assertEqual(display_number(Decimal("1200.000000")), "1200") self.assertEqual(display_number(None), "?") def test_failed_load_restores_controls_without_old_export(self): self.win._loaded(self.sample()) with patch.object(self.win._async, "run") as run: self.win._load() run.call_args.args[0].close() with patch("inventario.messagebox.showerror"), patch("inventario.log_exception"): self.win._error(RuntimeError("Synthetic failure")) self.assertEqual(self.win.btn_load.cget("state"), "normal") self.assertEqual(self.win.btn_export.cget("state"), "disabled") self.assertEqual(self.win.tree.get_children(), ()) class InventoryLauncherTests(unittest.TestCase): def test_menu_opens_single_inventory_child(self): from main import Launcher from user_session import UserSession session = UserSession(1, "TEST", "Test operator") with warnings.catch_warnings(), patch.object(Launcher, "_apply_dynamic_geometry"), \ patch.object(Launcher, "lift"), patch.object(Launcher, "focus_force"): warnings.simplefilter("error", UserWarning) root = Launcher(session, Mock()) root.withdraw() try: self.assertIsInstance(root._exit_icon, ctk.CTkImage) self.assertEqual(root._exit_icon.cget("size"), (14, 14)) for mode in ("light", "dark"): for scale in (1, 2): photo = root._exit_icon.create_scaled_photo_image(scale, mode) self.assertEqual((photo.width(), photo.height()), (14 * scale, 14 * scale)) def widgets(widget): for child in widget.winfo_children(): yield child yield from widgets(child) buttons = [w for w in widgets(root) if isinstance(w, ctk.CTkButton) and w.cget("text") == "Inventario"] self.assertEqual(len(buttons), 1) def create(*args, **kwargs): win = InventarioWindow(*args, **kwargs) win.withdraw() return win with patch("main.open_inventario_window", side_effect=create) as factory, \ patch("main.place_window_below_parent_later"), \ patch.object(InventarioWindow, "lift"), patch.object(InventarioWindow, "focus_force"): buttons[0].invoke() first = root._child_windows_by_key["inventario"] buttons[0].invoke() self.assertIs(root._child_windows_by_key["inventario"], first) self.assertEqual(factory.call_count, 1) root.db_client.query_json.assert_not_called() finally: for token in root.tk.call("after", "info"): root.tk.call("after", "cancel", token) root.destroy() if __name__ == "__main__": unittest.main()