Still refactoring and unit tests

This commit is contained in:
2025-08-30 22:37:17 +00:00
parent 1d423c5ea2
commit c76a77cb0c
16 changed files with 78 additions and 73 deletions

View File

@@ -2,21 +2,24 @@
import unittest
from unittest.mock import MagicMock
from models import Credential, Gate, Status
from repository import GatesRepository
from services import AVConnectService, GatesService
from src.models import Credential, Gate, Status
from src.repository import GatesRepository, UsersRepository
from src.services import AVConnectService, GatesService, UsersService
class TestGatesService(unittest.TestCase):
def setUp(self):
self.mock_users_json_path = "../resources/mock_users.json"
self.gates_repo = MagicMock(spec=GatesRepository)
self.users_repo = UsersRepository(self.mock_users_json_path)
self.avconnect_service = MagicMock(spec=AVConnectService)
self.service = GatesService(self.gates_repo, self.avconnect_service)
self.users_service = UsersService(self.users_repo)
self.service = GatesService(self.gates_repo, self.avconnect_service, self.users_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)
gate = Gate(gid="1", name="Test Gate", status=Status.DISABLED)
self.gates_repo.get_by_key.return_value = gate
result = self.service.open_gate(gate_key, credential)
@@ -28,7 +31,7 @@ class TestGatesService(unittest.TestCase):
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)
gate = Gate(gid="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
@@ -36,12 +39,12 @@ class TestGatesService(unittest.TestCase):
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)
self.avconnect_service.open_gate_by_id.assert_called_once_with(gate.gid, 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)
gate = Gate(gid="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")
@@ -49,7 +52,7 @@ class TestGatesService(unittest.TestCase):
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)
self.avconnect_service.open_gate_by_id.assert_called_once_with(gate.gid, credential)
def test_open_gate_returns_false_when_gate_does_not_exist(self):
gate_key = "nonexistent_gate"