# tests/test_gates_service.py import unittest from unittest.mock import MagicMock from models import Credential, Gate, Status from repository import GatesRepository from services import AVConnectService, GatesService class TestGatesService(unittest.TestCase): def setUp(self): self.gates_repo = MagicMock(spec=GatesRepository) self.avconnect_service = MagicMock(spec=AVConnectService) self.service = GatesService(self.gates_repo, self.avconnect_service) def test_open_gate_fails_when_gate_is_disabled(self): gate_key = "gate1" credential = Credential(username="user", password="pass") gate = Gate(id="1", name="Test Gate", status=Status.DISABLED) self.gates_repo.get_by_key.return_value = gate result = self.service.open_gate(gate_key, credential) self.assertFalse(result) self.gates_repo.get_by_key.assert_called_once_with(gate_key) self.avconnect_service.open_gate_by_id.assert_not_called() def test_open_gate_succeeds_with_valid_enabled_gate(self): gate_key = "gate2" credential = Credential(username="user", password="pass") gate = Gate(id="2", name="Enabled Gate", status=Status.ENABLED) self.gates_repo.get_by_key.return_value = gate self.avconnect_service.open_gate_by_id.return_value = True result = self.service.open_gate(gate_key, credential) self.assertTrue(result) self.gates_repo.get_by_key.assert_called_once_with(gate_key) self.avconnect_service.open_gate_by_id.assert_called_once_with(gate.id, credential) def test_open_gate_handles_exception_gracefully(self): gate_key = "gate3" credential = Credential(username="user", password="pass") gate = Gate(id="3", name="Test Gate", status=Status.ENABLED) self.gates_repo.get_by_key.return_value = gate self.avconnect_service.open_gate_by_id.side_effect = Exception("Test Exception") result = self.service.open_gate(gate_key, credential) self.assertFalse(result) self.gates_repo.get_by_key.assert_called_once_with(gate_key) self.avconnect_service.open_gate_by_id.assert_called_once_with(gate.id, credential) def test_open_gate_returns_false_when_gate_does_not_exist(self): gate_key = "nonexistent_gate" credential = Credential(username="user", password="pass") self.gates_repo.get_by_key.return_value = None result = self.service.open_gate(gate_key, credential) self.assertFalse(result) self.gates_repo.get_by_key.assert_called_once_with(gate_key) self.avconnect_service.open_gate_by_id.assert_not_called() if __name__ == "__main__": unittest.main()