|
| 1 | +# Copyright (C) 2025 ANSYS, Inc. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +"""Module for testing gRPC connection abstraction.""" |
| 24 | + |
| 25 | +from unittest.mock import MagicMock |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +from ansys.tools.common.abstractions.connection import AbstractGRPCConnection |
| 30 | + |
| 31 | + |
| 32 | +class MockGRPCConnection(AbstractGRPCConnection): |
| 33 | + """Mock implementation of AbstractGRPCConnection for testing.""" |
| 34 | + |
| 35 | + def __init__(self, host: str, port: str) -> None: |
| 36 | + """Initialize the mock gRPC connection.""" |
| 37 | + self._host = host |
| 38 | + self._port = port |
| 39 | + self._connected = False |
| 40 | + |
| 41 | + def connect(self) -> None: |
| 42 | + """Connect to the mock gRPC server.""" |
| 43 | + self._connected = True |
| 44 | + |
| 45 | + def close(self) -> None: |
| 46 | + """Close the mock gRPC connection.""" |
| 47 | + self._connected = False |
| 48 | + |
| 49 | + @property |
| 50 | + def service(self): |
| 51 | + """Service property that returns a mock gRPC stub.""" |
| 52 | + return MagicMock() |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def mock_connection(): |
| 57 | + """Fixture for creating a mock gRPC connection.""" |
| 58 | + return MockGRPCConnection(host="localhost", port="50051") |
| 59 | + |
| 60 | + |
| 61 | +def test_initialization(mock_connection): |
| 62 | + """Test initialization of the connection.""" |
| 63 | + assert mock_connection._host == "localhost" |
| 64 | + assert mock_connection._port == "50051" |
| 65 | + assert mock_connection.is_closed |
| 66 | + |
| 67 | + |
| 68 | +def test_connect(mock_connection): |
| 69 | + """Test connecting to the gRPC server.""" |
| 70 | + mock_connection.connect() |
| 71 | + |
| 72 | + |
| 73 | +def test_close(mock_connection): |
| 74 | + """Test disconnecting from the gRPC server.""" |
| 75 | + mock_connection.connect() |
| 76 | + mock_connection.close() |
| 77 | + assert mock_connection.is_closed |
| 78 | + |
| 79 | + |
| 80 | +def test_service_property(mock_connection): |
| 81 | + """Test the service property.""" |
| 82 | + service = mock_connection.service |
| 83 | + assert service is not None |
| 84 | + assert isinstance(service, MagicMock) |
| 85 | + |
| 86 | + |
| 87 | +def test_is_closed_property(mock_connection): |
| 88 | + """Test the is_closed property.""" |
| 89 | + assert mock_connection.is_closed |
0 commit comments