VannaSqlGenerationServiceTest.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package com.agent.management.rag.structured;
  2. import com.agent.management.model.entity.DataSource;
  3. import com.agent.management.rag.bridge.RagAiBridgeClient;
  4. import com.agent.management.rag.capability.*;
  5. import com.agent.management.rag.memory.RagExampleMemoryService;
  6. import com.agent.management.rag.model.RagQuery;
  7. import com.agent.management.rag.model.RagSourceType;
  8. import com.agent.management.service.DataSourceService;
  9. import com.agent.management.service.SchemaExplorerService;
  10. import org.junit.jupiter.api.Test;
  11. import org.mockito.ArgumentCaptor;
  12. import java.util.List;
  13. import java.util.Map;
  14. import static org.assertj.core.api.Assertions.assertThat;
  15. import static org.mockito.Mockito.*;
  16. class VannaSqlGenerationServiceTest {
  17. @Test
  18. void sendsOnlyWhitelistedTableDdlAndConfiguredContext() {
  19. RagAiBridgeClient bridge = mock(RagAiBridgeClient.class);
  20. DataSourceService dataSources = mock(DataSourceService.class);
  21. RagCapabilityProfileService profiles = mock(RagCapabilityProfileService.class);
  22. RagSchemaLinker linker = mock(RagSchemaLinker.class);
  23. RagExampleMemoryService examples = mock(RagExampleMemoryService.class);
  24. DataSource source = new DataSource();
  25. source.setType("MYSQL");
  26. when(dataSources.getDecrypted(1L)).thenReturn(source);
  27. SqlSchemaSnapshot snapshot = new SqlSchemaSnapshot(List.of(
  28. new SqlSchemaSnapshot.TableSchema("rescue_forces", "", List.of(
  29. new SqlSchemaSnapshot.ColumnSchema("name", "VARCHAR", "力量名称"),
  30. new SqlSchemaSnapshot.ColumnSchema("status", "VARCHAR", "状态"))),
  31. new SqlSchemaSnapshot.TableSchema("secret_table", "", List.of(
  32. new SqlSchemaSnapshot.ColumnSchema("secret", "VARCHAR", "")))
  33. ));
  34. when(profiles.get(RagSourceType.STRUCTURED_DATA, "1"))
  35. .thenReturn(new RagCapabilityProfile(RagSourceType.STRUCTURED_DATA,"1","v",java.time.Instant.now(),snapshot,null,List.of()));
  36. when(linker.selectSqlTables(eq("可用力量"), eq(snapshot), anyList(), anyMap())).thenReturn(List.of("rescue_forces"));
  37. when(examples.findSimilar(any(), anyString(), anyString(), anyInt())).thenReturn(List.of());
  38. when(bridge.textToSql(any())).thenReturn(Map.of("sql", "SELECT name FROM rescue_forces"));
  39. RagQuery query = new RagQuery();
  40. query.setQuery("可用力量");
  41. query.setFilters(Map.of(
  42. "allowTextToSql", true,
  43. "tableWhitelist", List.of("rescue_forces"),
  44. "documentation", "READY 表示可用",
  45. "examples", List.of(Map.of("question", "q", "sql", "SELECT 1")),
  46. "maxRows", 20
  47. ));
  48. assertThat(new VannaSqlGenerationService(bridge, dataSources, profiles, linker, mock(RagSemanticCatalogService.class), examples, new RagEntityMentionExtractor()).generateSql(query, 1L))
  49. .contains("SELECT name FROM rescue_forces");
  50. ArgumentCaptor<Map<String, Object>> request = ArgumentCaptor.forClass(Map.class);
  51. verify(bridge).textToSql(request.capture());
  52. assertThat(String.valueOf(request.getValue().get("ddl")))
  53. .contains("rescue_forces", "name VARCHAR", "status VARCHAR")
  54. .doesNotContain("secret_table");
  55. assertThat(request.getValue()).containsEntry("documentation", "READY 表示可用")
  56. .containsEntry("maxRows", 20);
  57. }
  58. }