|
| 1 | +import logging |
| 2 | + |
| 3 | +import pytest |
| 4 | +from fastmcp import Context |
| 5 | + |
| 6 | +from integtests.conftest import BucketDef, ConfigDef, TableDef |
| 7 | +from keboola_mcp_server.client import KeboolaClient, SuggestedComponent |
| 8 | +from keboola_mcp_server.tools.search import GlobalSearchOutput, find_component_id, find_ids_by_name |
| 9 | + |
| 10 | +LOG = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.asyncio |
| 14 | +async def test_global_search_end_to_end( |
| 15 | + keboola_client: KeboolaClient, |
| 16 | + mcp_context: Context, |
| 17 | + buckets: list[BucketDef], |
| 18 | + tables: list[TableDef], |
| 19 | + configs: list[ConfigDef], |
| 20 | +) -> None: |
| 21 | + """ |
| 22 | + Test the global_search tool end-to-end by searching for items that exist in the test project. |
| 23 | + This verifies that the search returns expected results for buckets, tables, and configurations. |
| 24 | + """ |
| 25 | + |
| 26 | + # skip this test if the global search is not available |
| 27 | + if not await keboola_client.storage_client.is_enabled('global-search'): |
| 28 | + LOG.warning('Global search is not available. Please enable it in the project settings.') |
| 29 | + pytest.skip('Global search is not available. Please enable it in the project settings.') |
| 30 | + |
| 31 | + # Search for test items by name prefix 'test' which should match our test data |
| 32 | + result = await find_ids_by_name( |
| 33 | + ctx=mcp_context, name_prefixes=['test'], item_types=tuple(), limit=50, offset=0 # Search all types |
| 34 | + ) |
| 35 | + |
| 36 | + # Verify the result structure |
| 37 | + assert isinstance(result, GlobalSearchOutput) |
| 38 | + assert isinstance(result.counts, dict) |
| 39 | + assert isinstance(result.groups, dict) |
| 40 | + assert 'total' in result.counts |
| 41 | + |
| 42 | + # Verify we found some results |
| 43 | + assert result.counts['total'] > 0, 'Should find at least some test items' |
| 44 | + |
| 45 | + # Create sets of expected IDs for verification |
| 46 | + expected_bucket_ids = {bucket.bucket_id for bucket in buckets} |
| 47 | + expected_table_ids = {table.table_id for table in tables} |
| 48 | + expected_config_ids = {config.configuration_id for config in configs if config.configuration_id} |
| 49 | + |
| 50 | + # Check that we can find test buckets |
| 51 | + bucket_groups = [group for group in result.groups.values() if group.type == 'bucket'] |
| 52 | + assert len(bucket_groups) == 1 |
| 53 | + bucket_group = bucket_groups[0] |
| 54 | + found_bucket_ids = {item.id for item in bucket_group.items} |
| 55 | + # At least some test buckets should be found |
| 56 | + assert found_bucket_ids.intersection(expected_bucket_ids), 'Should find at least one test bucket' |
| 57 | + |
| 58 | + # Check that we can find test tables |
| 59 | + table_groups = [group for group in result.groups.values() if group.type == 'table'] |
| 60 | + assert len(table_groups) == 1 |
| 61 | + table_group = table_groups[0] |
| 62 | + found_table_ids = {item.id for item in table_group.items} |
| 63 | + # At least some test tables should be found |
| 64 | + assert found_table_ids.intersection(expected_table_ids), 'Should find at least one test table' |
| 65 | + |
| 66 | + # Check that we can find test configurations |
| 67 | + config_groups = [group for group in result.groups.values() if group.type == 'configuration'] |
| 68 | + assert len(config_groups) == 1 |
| 69 | + config_group = config_groups[0] |
| 70 | + found_config_ids = {item.id for item in config_group.items} |
| 71 | + # At least some test configurations should be found |
| 72 | + assert found_config_ids.intersection(expected_config_ids), 'Should find at least one test configuration' |
| 73 | + |
| 74 | + config_groups = [group for group in result.groups.values() if group.type == 'configuration'] |
| 75 | + assert len(config_groups) == 1 |
| 76 | + config_group = config_groups[0] |
| 77 | + found_config_ids = {item.id for item in config_group.items} |
| 78 | + # At least some test configurations should be found |
| 79 | + assert found_config_ids.intersection(expected_config_ids), 'Should find at least one test configuration' |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_find_component_id(mcp_context: Context): |
| 84 | + """Tests that `find_component_id` returns relevant component IDs for a query.""" |
| 85 | + query = 'generic extractor' |
| 86 | + generic_extractor_id = 'ex-generic-v2' |
| 87 | + |
| 88 | + result = await find_component_id(query=query, ctx=mcp_context) |
| 89 | + |
| 90 | + assert isinstance(result, list) |
| 91 | + assert len(result) > 0 |
| 92 | + assert all(isinstance(component, SuggestedComponent) for component in result) |
| 93 | + assert generic_extractor_id in [component.component_id for component in result] |
0 commit comments