Refactor gates creating service, repository and tests

This commit is contained in:
Alessandro Franchini
2025-08-22 01:34:25 +02:00
parent 16cf408725
commit 58c0916deb
24 changed files with 251 additions and 89 deletions

View File

@@ -0,0 +1,51 @@
import unittest
from models import Gate, Status
from repository import GatesRepository
class TestGatesRepository(unittest.TestCase):
def setUp(self):
self.mock_json_path = "../resources/mock_gates.json"
self.repo = GatesRepository(self.mock_json_path)
def test_get_by_key_returns_gate_when_key_exists(self):
gate_key = "gateA"
expected_gate = Gate("1", "Gate A", Status.ENABLED)
result = self.repo.get_by_key(gate_key)
self.assertEqual(expected_gate.id, result.id)
self.assertEqual(expected_gate.name, result.name)
self.assertEqual(expected_gate.status, result.status)
def test_get_by_key_returns_none_when_key_does_not_exist(self):
gate_key = "invalid_key"
result = self.repo.get_by_key(gate_key)
self.assertIsNone(result)
def test_get_all_enabled_returns_only_enabled_gates(self):
enabled_gate_ids = ["1", "3", "5"]
result = self.repo.get_all_enabled()
self.assertEqual(len(enabled_gate_ids), len(result))
self.assertEqual(
sorted(enabled_gate_ids), sorted([gate.id for gate in result])
)
for gate in result:
self.assertTrue(gate.status == Status.ENABLED)
print(gate.status)
# New Test
def test_load_gates_handles_invalid_json_file_gracefully(self):
repo = GatesRepository("./tests/invalid_mock_gates.json")
self.assertEqual(repo._gates, {})
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,24 @@
{
"gateA": {
"name": "Gate A",
"id": "1"
},
"gateB": {
"name": "Gate B",
"id": "2",
"status": 0
},
"gateC": {
"name": "Gate C",
"id": "3"
},
"gateD": {
"name": "Gate D",
"id": "4",
"status": 0
},
"gateE": {
"name": "Gate E",
"id": "5"
}
}

View File

@@ -0,0 +1,67 @@
# 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()