package com.superbiz.agent.config; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.test.context.TestPropertySource; import javax.sql.DataSource; import java.sql.Connection; import static org.junit.jupiter.api.Assertions.*; /** * 单独测试 MySQL 连接(不启动完整 Spring Context) */ @DataJpaTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) @TestPropertySource(properties = { "spring.flyway.enabled=true", "spring.jpa.hibernate.ddl-auto=validate" }) class MySQLConnectionTest { @Autowired private DataSource dataSource; @Test void testMySQLConnection() { assertNotNull(dataSource, "DataSource should be configured"); try (Connection connection = dataSource.getConnection()) { assertNotNull(connection, "Connection should not be null"); assertFalse(connection.isClosed(), "Connection should be open"); String catalog = connection.getCatalog(); System.out.println("✓ MySQL 连接成功!"); System.out.println(" 数据库: " + catalog); System.out.println(" URL: " + connection.getMetaData().getURL()); assertEquals("superbiz_agent", catalog, "Database name should be superbiz_agent"); } catch (Exception e) { fail("MySQL 连接失败: " + e.getMessage()); } } @Test void testFlywayMigration() { // 如果能到这里,说明 Flyway 迁移成功 System.out.println("✓ Flyway 迁移已执行"); try (Connection connection = dataSource.getConnection()) { var metadata = connection.getMetaData(); var tables = metadata.getTables(null, null, "%", new String[]{"TABLE"}); int tableCount = 0; System.out.println(" 已创建的表:"); while (tables.next()) { String tableName = tables.getString("TABLE_NAME"); System.out.println(" - " + tableName); tableCount++; } assertTrue(tableCount >= 3, "至少应该有 3 张表 (diagnosis_record, case_library, api_document)"); } catch (Exception e) { fail("验证表结构失败: " + e.getMessage()); } } }