|
| 1 | +import sys |
| 2 | +from unittest.mock import AsyncMock |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.asyncio |
| 9 | +@pytest.mark.parametrize("transport", ["stdio", "sse", "streamable-http"]) |
| 10 | +async def test_transport_argument_parsing(transport): |
| 11 | + """Test that all transport options are parsed correctly.""" |
| 12 | + from postgres_mcp.server import main |
| 13 | + |
| 14 | + original_argv = sys.argv |
| 15 | + try: |
| 16 | + sys.argv = [ |
| 17 | + "postgres_mcp", |
| 18 | + "postgresql://user:password@localhost/db", |
| 19 | + f"--transport={transport}", |
| 20 | + ] |
| 21 | + |
| 22 | + with ( |
| 23 | + patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), |
| 24 | + patch("postgres_mcp.server.mcp.run_stdio_async", AsyncMock()) as mock_stdio, |
| 25 | + patch("postgres_mcp.server.mcp.run_sse_async", AsyncMock()) as mock_sse, |
| 26 | + patch("postgres_mcp.server.mcp.run_streamable_http_async", AsyncMock()) as mock_http, |
| 27 | + ): |
| 28 | + await main() |
| 29 | + |
| 30 | + # Verify the correct transport method was called |
| 31 | + if transport == "stdio": |
| 32 | + mock_stdio.assert_called_once() |
| 33 | + mock_sse.assert_not_called() |
| 34 | + mock_http.assert_not_called() |
| 35 | + elif transport == "sse": |
| 36 | + mock_stdio.assert_not_called() |
| 37 | + mock_sse.assert_called_once() |
| 38 | + mock_http.assert_not_called() |
| 39 | + elif transport == "streamable-http": |
| 40 | + mock_stdio.assert_not_called() |
| 41 | + mock_sse.assert_not_called() |
| 42 | + mock_http.assert_called_once() |
| 43 | + finally: |
| 44 | + sys.argv = original_argv |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_streamable_http_host_port_arguments(): |
| 49 | + """Test that streamable-http host and port arguments are applied correctly.""" |
| 50 | + from postgres_mcp.server import main |
| 51 | + from postgres_mcp.server import mcp |
| 52 | + |
| 53 | + original_argv = sys.argv |
| 54 | + try: |
| 55 | + sys.argv = [ |
| 56 | + "postgres_mcp", |
| 57 | + "postgresql://user:password@localhost/db", |
| 58 | + "--transport=streamable-http", |
| 59 | + "--streamable-http-host=0.0.0.0", |
| 60 | + "--streamable-http-port=9000", |
| 61 | + ] |
| 62 | + |
| 63 | + with ( |
| 64 | + patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), |
| 65 | + patch("postgres_mcp.server.mcp.run_streamable_http_async", AsyncMock()), |
| 66 | + ): |
| 67 | + await main() |
| 68 | + |
| 69 | + # Verify the host and port were set correctly |
| 70 | + assert mcp.settings.host == "0.0.0.0" |
| 71 | + assert mcp.settings.port == 9000 |
| 72 | + finally: |
| 73 | + sys.argv = original_argv |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_sse_host_port_arguments(): |
| 78 | + """Test that SSE host and port arguments are applied correctly.""" |
| 79 | + from postgres_mcp.server import main |
| 80 | + from postgres_mcp.server import mcp |
| 81 | + |
| 82 | + original_argv = sys.argv |
| 83 | + try: |
| 84 | + sys.argv = [ |
| 85 | + "postgres_mcp", |
| 86 | + "postgresql://user:password@localhost/db", |
| 87 | + "--transport=sse", |
| 88 | + "--sse-host=0.0.0.0", |
| 89 | + "--sse-port=8080", |
| 90 | + ] |
| 91 | + |
| 92 | + with ( |
| 93 | + patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), |
| 94 | + patch("postgres_mcp.server.mcp.run_sse_async", AsyncMock()), |
| 95 | + ): |
| 96 | + await main() |
| 97 | + |
| 98 | + # Verify the host and port were set correctly |
| 99 | + assert mcp.settings.host == "0.0.0.0" |
| 100 | + assert mcp.settings.port == 8080 |
| 101 | + finally: |
| 102 | + sys.argv = original_argv |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.asyncio |
| 106 | +async def test_default_transport_is_stdio(): |
| 107 | + """Test that the default transport is stdio when not specified.""" |
| 108 | + from postgres_mcp.server import main |
| 109 | + |
| 110 | + original_argv = sys.argv |
| 111 | + try: |
| 112 | + sys.argv = [ |
| 113 | + "postgres_mcp", |
| 114 | + "postgresql://user:password@localhost/db", |
| 115 | + ] |
| 116 | + |
| 117 | + with ( |
| 118 | + patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), |
| 119 | + patch("postgres_mcp.server.mcp.run_stdio_async", AsyncMock()) as mock_stdio, |
| 120 | + patch("postgres_mcp.server.mcp.run_sse_async", AsyncMock()) as mock_sse, |
| 121 | + patch("postgres_mcp.server.mcp.run_streamable_http_async", AsyncMock()) as mock_http, |
| 122 | + ): |
| 123 | + await main() |
| 124 | + |
| 125 | + mock_stdio.assert_called_once() |
| 126 | + mock_sse.assert_not_called() |
| 127 | + mock_http.assert_not_called() |
| 128 | + finally: |
| 129 | + sys.argv = original_argv |
0 commit comments