|
| 1 | +from unittest import mock |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from pydantic import ValidationError |
3 | 5 |
|
4 | 6 | from unstructured_ingest.processes.connectors.confluence import ( |
5 | 7 | ConfluenceAccessConfig, |
6 | 8 | ConfluenceConnectionConfig, |
| 9 | + ConfluenceIndexer, |
| 10 | + ConfluenceIndexerConfig, |
7 | 11 | ) |
8 | 12 |
|
9 | 13 |
|
| 14 | +@pytest.fixture |
| 15 | +def connection_config(): |
| 16 | + """Provides a minimal ConfluenceConnectionConfig for testing.""" |
| 17 | + access_config = ConfluenceAccessConfig(api_token="token") |
| 18 | + return ConfluenceConnectionConfig( |
| 19 | + url="https://dummy", |
| 20 | + username="user", |
| 21 | + access_config=access_config, |
| 22 | + ) |
| 23 | + |
| 24 | + |
10 | 25 | def test_connection_config_multiple_auth(): |
11 | 26 | with pytest.raises(ValidationError): |
12 | 27 | ConfluenceConnectionConfig( |
@@ -69,3 +84,80 @@ def test_connection_config_pat_auth(): |
69 | 84 | access_config=ConfluenceAccessConfig(token="access_token"), |
70 | 85 | url="url", |
71 | 86 | ) |
| 87 | + |
| 88 | + |
| 89 | +def test_precheck_with_spaces_calls_get_space(monkeypatch, connection_config): |
| 90 | + """Test that precheck calls get_space for each space when spaces are set.""" |
| 91 | + spaces = ["A", "B", "C"] |
| 92 | + index_config = ConfluenceIndexerConfig( |
| 93 | + max_num_of_spaces=100, |
| 94 | + max_num_of_docs_from_each_space=100, |
| 95 | + spaces=spaces, |
| 96 | + ) |
| 97 | + indexer = ConfluenceIndexer(connection_config=connection_config, index_config=index_config) |
| 98 | + mock_client = mock.MagicMock() |
| 99 | + with mock.patch.object(type(connection_config), "get_client", mock.MagicMock()): |
| 100 | + type(connection_config).get_client.return_value.__enter__.return_value = mock_client |
| 101 | + |
| 102 | + result = indexer.precheck() |
| 103 | + calls = [mock.call(space) for space in spaces] |
| 104 | + mock_client.get_space.assert_has_calls(calls, any_order=False) |
| 105 | + assert mock_client.get_space.call_count == len(spaces) |
| 106 | + assert result is True |
| 107 | + |
| 108 | + |
| 109 | +def test_precheck_without_spaces_calls_get_all_spaces(monkeypatch, connection_config): |
| 110 | + """Test that precheck calls get_all_spaces when spaces is not set.""" |
| 111 | + index_config = ConfluenceIndexerConfig( |
| 112 | + max_num_of_spaces=100, |
| 113 | + max_num_of_docs_from_each_space=100, |
| 114 | + spaces=None, |
| 115 | + ) |
| 116 | + indexer = ConfluenceIndexer(connection_config=connection_config, index_config=index_config) |
| 117 | + mock_client = mock.MagicMock() |
| 118 | + with mock.patch.object(type(connection_config), "get_client", mock.MagicMock()): |
| 119 | + type(connection_config).get_client.return_value.__enter__.return_value = mock_client |
| 120 | + |
| 121 | + result = indexer.precheck() |
| 122 | + mock_client.get_all_spaces.assert_called_once_with(limit=1) |
| 123 | + mock_client.get_space.assert_not_called() |
| 124 | + assert result is True |
| 125 | + |
| 126 | + |
| 127 | +def test_precheck_with_spaces_raises(monkeypatch, connection_config): |
| 128 | + """Test that precheck raises UserError if get_space fails.""" |
| 129 | + spaces = ["A", "B"] |
| 130 | + index_config = ConfluenceIndexerConfig( |
| 131 | + max_num_of_spaces=100, |
| 132 | + max_num_of_docs_from_each_space=100, |
| 133 | + spaces=spaces, |
| 134 | + ) |
| 135 | + indexer = ConfluenceIndexer(connection_config=connection_config, index_config=index_config) |
| 136 | + mock_client = mock.MagicMock() |
| 137 | + mock_client.get_space.side_effect = Exception("fail") |
| 138 | + from unstructured_ingest.processes.connectors.confluence import UserError |
| 139 | + |
| 140 | + with mock.patch.object(type(connection_config), "get_client", mock.MagicMock()): |
| 141 | + type(connection_config).get_client.return_value.__enter__.return_value = mock_client |
| 142 | + |
| 143 | + with pytest.raises(UserError): |
| 144 | + indexer.precheck() |
| 145 | + |
| 146 | + |
| 147 | +def test_precheck_without_spaces_raises(monkeypatch, connection_config): |
| 148 | + """Test that precheck raises SourceConnectionError if get_all_spaces fails.""" |
| 149 | + index_config = ConfluenceIndexerConfig( |
| 150 | + max_num_of_spaces=100, |
| 151 | + max_num_of_docs_from_each_space=100, |
| 152 | + spaces=None, |
| 153 | + ) |
| 154 | + indexer = ConfluenceIndexer(connection_config=connection_config, index_config=index_config) |
| 155 | + mock_client = mock.MagicMock() |
| 156 | + mock_client.get_all_spaces.side_effect = Exception("fail") |
| 157 | + from unstructured_ingest.processes.connectors.confluence import UserError |
| 158 | + |
| 159 | + with mock.patch.object(type(connection_config), "get_client", mock.MagicMock()): |
| 160 | + type(connection_config).get_client.return_value.__enter__.return_value = mock_client |
| 161 | + |
| 162 | + with pytest.raises(UserError): |
| 163 | + indexer.precheck() |
0 commit comments