wangzhigang1999 commented on code in PR #7400:
URL: https://github.com/apache/kyuubi/pull/7400#discussion_r3109261750


##########
externals/kyuubi-data-agent-engine/src/test/java/org/apache/kyuubi/engine/dataagent/mysql/WithMySQLContainer.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.engine.dataagent.mysql;
+
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
+import java.sql.Connection;
+import java.sql.Statement;
+import org.apache.kyuubi.engine.dataagent.tool.ToolRegistry;
+import org.apache.kyuubi.engine.dataagent.tool.sql.RunMutationQueryTool;
+import org.apache.kyuubi.engine.dataagent.tool.sql.RunSelectQueryTool;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.testcontainers.mysql.MySQLContainer;
+import org.testcontainers.utility.DockerImageName;
+
+/**
+ * Shared base class for MySQL integration tests. Starts a MySQL 8.0.32 
container once per test
+ * class and provides a pooled DataSource.
+ */
+public abstract class WithMySQLContainer {
+
+  private static final String MYSQL_IMAGE =
+      System.getProperty("kyuubi.test.mysql.image", "mysql:8.0.32");
+
+  private static final long TOOL_CALL_TIMEOUT_SECONDS = 30;
+  private static final int QUERY_TIMEOUT_SECONDS = 10;
+
+  protected static MySQLContainer mysql;
+  protected static HikariDataSource dataSource;
+  protected static ToolRegistry registry;
+
+  @BeforeClass
+  public static void startContainer() {
+    DockerImageName imageName =
+        DockerImageName.parse(MYSQL_IMAGE).asCompatibleSubstituteFor("mysql");
+    mysql =
+        new MySQLContainer(imageName)
+            .withUsername("root")
+            .withPassword("kyuubi")
+            .withDatabaseName("test_db")
+            .withEnv("MYSQL_ROOT_PASSWORD", "kyuubi");
+    mysql.start();
+
+    HikariConfig config = new HikariConfig();
+    config.setJdbcUrl(mysql.getJdbcUrl());
+    config.setUsername(mysql.getUsername());
+    config.setPassword(mysql.getPassword());
+    config.setMaximumPoolSize(5);
+    config.setMinimumIdle(1);
+    config.setPoolName("kyuubi-mysql-it");
+    dataSource = new HikariDataSource(config);
+
+    registry = new ToolRegistry(TOOL_CALL_TIMEOUT_SECONDS);
+    registry.register(new RunSelectQueryTool(dataSource, 
QUERY_TIMEOUT_SECONDS));
+    registry.register(new RunMutationQueryTool(dataSource, 
QUERY_TIMEOUT_SECONDS));
+  }
+
+  @AfterClass
+  public static void stopContainer() {
+    if (dataSource != null) {
+      dataSource.close();
+    }
+    if (mysql != null) {
+      mysql.stop();
+    }
+  }
+
+  /** Execute a SQL statement that does not return a result set. */
+  protected static void exec(String sql) {
+    try (Connection conn = dataSource.getConnection();
+        Statement stmt = conn.createStatement()) {
+      stmt.execute(sql);
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  /**
+   * Invoke a tool through the ToolRegistry, same as the LLM runtime does. 
Builds the JSON args
+   * envelope automatically.
+   */
+  protected static String callTool(String toolName, String sql) {
+    return registry.executeTool(toolName, "{\"sql\": \"" + sql.replace("\"", 
"\\\"") + "\"}");
+  }

Review Comment:
   accept



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to