test(sql): align contracts with clean initialization

This commit is contained in:
2026-09-17 15:10:27 +08:00
parent 1fe99eb6d0
commit 9dc344bdef
68 changed files with 851 additions and 1448 deletions
@@ -13,8 +13,14 @@ class InitialWarehouseResourceSeedSqlContractTest {
void initializesReusablePalletsAndYiwuLocationGrid() throws Exception {
assertModuleSeed(Files.readString(Path.of("src", "main", "resources", "db", "phase2-pallet-seed.sql"),
StandardCharsets.UTF_8));
assertUnifiedSeed(Files.readString(Path.of("..", "deploy", "sql", "头程数据库统一增量-MySQL8.sql"),
StandardCharsets.UTF_8));
String unified = Files.readString(Path.of("..", "deploy", "sql", "头程数据库统一增量-MySQL8.sql"),
StandardCharsets.UTF_8);
assertThat(unified)
.contains("CREATE TABLE wh_pallet (")
.contains("CREATE TABLE wh_location (")
.doesNotContain("assert_yiwu_location_seed")
.doesNotContain("INSERT INTO wh_location_reservation")
.doesNotContain("INSERT INTO wh_location_occupancy");
}
private static void assertModuleSeed(String sql) {
@@ -22,10 +28,6 @@ class InitialWarehouseResourceSeedSqlContractTest {
assertThat(sql).doesNotContain("color,status,remark");
}
private static void assertUnifiedSeed(String sql) {
assertSeed(sql, "193000000000200001", "193000000000200002", "193000000000200");
}
private static void assertSeed(String sql, String yiwuId, String dongguanId, String idPrefix) {
String compactSql = sql.replaceAll("\\s+", "");
int locationSeedStart = sql.indexOf("WHEN zones.zone_index = 0 AND slots.slot_number = 1 THEN 201");
@@ -14,32 +14,15 @@ class OverseasStorageFeeSqlContractTest {
String sql = Files.readString(Path.of("..", "deploy", "sql", "头程数据库统一增量-MySQL8.sql"));
assertThat(sql)
.contains("CREATE TABLE IF NOT EXISTS wh_overseas_storage_fee_rule")
.contains("CREATE TABLE IF NOT EXISTS wh_overseas_storage_fee_tier")
.containsOnlyOnce("CREATE PROCEDURE migrate_overseas_storage_fee_schema()")
.doesNotContain("ADD COLUMN IF NOT EXISTS initialization_key")
.doesNotContain("ADD COLUMN IF NOT EXISTS fee_type")
.doesNotContain("ADD COLUMN IF NOT EXISTS billing_unit")
.contains("table_name = 'wh_overseas_storage_fee_rule'")
.contains("column_name = 'initialization_key'")
.contains("column_name = 'fee_type'")
.contains("column_name = 'billing_unit'")
.contains("PREPARE overseas_fee_statement FROM @overseas_fee_ddl")
.contains("EXECUTE overseas_fee_statement")
.contains("DEALLOCATE PREPARE overseas_fee_statement")
.contains("constraint_name = 'chk_overseas_fee_tier_days'")
.contains("constraint_name = 'chk_overseas_fee_tier_price'")
.contains("index_columns = 'rule_id,fee_type,active_start_day'")
.contains("Warehouse 迁移失败:海外仓费用阶梯存在非法天数或负价格")
.contains("Warehouse 迁移失败:海外仓费用阶梯目标唯一键存在重复数据")
.contains("CALL migrate_overseas_storage_fee_schema()")
.contains("LOWER(column_type) = 'varchar(191)'")
.contains("LOWER(column_type) = 'varchar(32)'")
.contains("LOWER(column_type) = 'varchar(16)'")
.contains("LOWER(column_type) = 'smallint unsigned'")
.contains("LOWER(column_type) = 'decimal(18,6)'")
.contains("统一迁移失败:海外仓阶梯费用字段签名不正确")
.contains("统一迁移失败:海外仓阶梯费用唯一键或 CHECK 结构不正确")
.contains("CREATE TABLE wh_overseas_storage_fee_rule")
.contains("CREATE TABLE wh_overseas_storage_fee_tier")
.contains("initialization_key varchar(191) DEFAULT NULL")
.contains("fee_type varchar(32) NOT NULL DEFAULT 'STORAGE'")
.contains("billing_unit varchar(16) NOT NULL DEFAULT 'CBM'")
.contains("start_day smallint unsigned NOT NULL")
.contains("unit_price decimal(18,6) NOT NULL")
.contains("CONSTRAINT chk_overseas_fee_tier_days CHECK")
.contains("CONSTRAINT chk_overseas_fee_tier_price CHECK")
.contains("uk_overseas_fee_active_customer")
.contains("idx_overseas_fee_page")
.contains("idx_overseas_fee_customer_name")
@@ -49,14 +32,7 @@ class OverseasStorageFeeSqlContractTest {
.contains("platform:overseas-storage-fee:create")
.contains("platform:overseas-storage-fee:edit")
.contains("platform:overseas-storage-fee:delete")
.contains("CALL unified_assert_overseas_storage_fee()")
.contains("统一增量全部执行完成");
int addColumns = sql.indexOf("column_name = 'fee_type'");
int migrate = sql.indexOf("CALL migrate_overseas_storage_fee_schema()");
int assertSchema = sql.indexOf("CALL unified_assert_overseas_storage_fee()");
assertThat(addColumns).isGreaterThan(0);
assertThat(migrate).isGreaterThan(addColumns);
assertThat(assertSchema).isGreaterThan(migrate);
.doesNotContain("migrate_overseas_storage_fee_schema")
.doesNotContain("unified_assert_overseas_storage_fee");
}
}
@@ -17,13 +17,15 @@ class PalletLocationReleaseIdempotencySqlContractTest {
"头程数据库统一增量-MySQL8.sql"));
for (String sql : new String[] {moduleSql, unifiedSql}) {
assertThat(sql)
.contains("CREATE TABLE IF NOT EXISTS wh_pallet_location_release_operation")
.contains("idempotency_key VARCHAR(128) NOT NULL")
.contains("request_hash CHAR(64) NOT NULL")
.contains("response_snapshot JSON NULL")
String normalized = sql.toLowerCase().replace(" default null", " null").replace(", ", ",");
assertThat(normalized)
.contains("create table ")
.contains("wh_pallet_location_release_operation")
.contains("idempotency_key varchar(128) not null")
.contains("request_hash char(64) not null")
.contains("response_snapshot json null")
.contains("uk_pallet_location_release_idempotency")
.contains("tenant_id, source_service, business_id, idempotency_key");
.contains("tenant_id,source_service,business_id,idempotency_key");
}
}
@@ -35,14 +37,17 @@ class PalletLocationReleaseIdempotencySqlContractTest {
"头程数据库统一增量-MySQL8.sql"));
for (String sql : new String[] {moduleSql, unifiedSql}) {
assertThat(sql)
.contains("CREATE TABLE IF NOT EXISTS wh_pallet_location_settlement_operation")
.contains("source_business_id BIGINT UNSIGNED NOT NULL")
.contains("target_business_id BIGINT UNSIGNED NOT NULL")
.contains("response_snapshot JSON NULL")
String normalized = sql.toLowerCase().replace(" default null", " null").replace(", ", ",");
assertThat(normalized)
.contains("create table ")
.contains("wh_pallet_location_settlement_operation")
.contains("source_business_id bigint unsigned not null")
.contains("target_business_id bigint unsigned not null")
.contains("response_snapshot json null")
.contains("uk_pallet_location_settlement_idempotency")
.contains("tenant_id, source_service, source_business_id, idempotency_key")
.contains("DROP INDEX uk_pallet_location_settlement_idempotency");
.contains("tenant_id,source_service,source_business_id,idempotency_key");
}
assertThat(moduleSql).contains("DROP INDEX uk_pallet_location_settlement_idempotency");
assertThat(unifiedSql).doesNotContain("DROP INDEX uk_pallet_location_settlement_idempotency");
}
}
@@ -29,11 +29,13 @@ class SupplierSqlContractTest {
@Test void unifiedMigrationContainsSupplierSchemaAndPermissionContract() throws IOException {
String sql = Files.readString(resolveUnifiedSql(), StandardCharsets.UTF_8);
assertThat(sql).contains("CREATE TABLE IF NOT EXISTS wh_supplier", "CREATE TABLE IF NOT EXISTS wh_supplier_carrier",
String normalized = sql.replace("PRIMARY KEY (", "PRIMARY KEY(");
assertThat(normalized).contains("CREATE TABLE wh_supplier", "CREATE TABLE wh_supplier_carrier",
"PRIMARY KEY(supplier_id,carrier_code)", "idx_wh_supplier_carrier_code", "uk_wh_supplier_name",
"idx_wh_supplier_filter", "platform:supplier:list", "platform:supplier:create",
"platform:supplier:edit", "platform:supplier:status", "assert_platform_supplier_permissions");
String supplierSection = sql.substring(sql.indexOf("-- 平台公共供应商业务档案及权限"));
"platform:supplier:edit", "platform:supplier:status")
.doesNotContain("assert_platform_supplier_permissions");
String supplierSection = sql.substring(sql.indexOf("CREATE TABLE wh_supplier"));
assertThat(supplierSection).doesNotContain("chk_wh_supplier_scope", "订舱分类范围");
}
@@ -130,43 +130,40 @@ class WarehouseSchemaContractTest {
.contains("193000000000400003,0,193000000000200003,'RECEIVING','海外收货',1")
.contains("'洛杉矶仓海外接柜入仓收货能力'")
.contains("193000000000400004,0,193000000000200004,'RECEIVING','海外收货',1")
.contains("'纽约仓海外接柜入仓收货能力'")
.contains("固定平台作业仓收货能力写入校验失败")
.contains("warehouse.warehouse_type = 'FBA'")
.contains("FBA公共地址不得创建平台作业能力");
.contains("'纽约仓海外接柜入仓收货能力'");
}
}
@Test
void unifiedSqlShouldConvergePalletsToReusableMasterData() throws IOException {
String unified = Files.readString(Path.of("..", "deploy", "sql", "头程数据库统一增量-MySQL8.sql"));
String palletDefinition = tableDefinition(unified, "wh_pallet");
String palletDefinition = tableDefinition(unified.toLowerCase(), "wh_pallet").replace(",", ", ");
assertThat(palletDefinition)
.contains("idx_pallet_warehouse (tenant_id, warehouse_id, deleted, pallet_code)")
.contains("idx_pallet_type (tenant_id, warehouse_id, pallet_type, capacity_cbm, deleted)")
.doesNotContain("status VARCHAR")
.doesNotContain("status varchar")
.doesNotContain("idx_pallet_available");
assertThat(unified)
.contains("ALTER TABLE wh_pallet DROP COLUMN status")
.contains("ALTER TABLE wh_location_reservation DROP COLUMN active_pallet_reservation")
.contains("ALTER TABLE wh_location_occupancy DROP COLUMN active_pallet_occupancy")
.contains("托盘动态资源状态或活动唯一约束仍存在")
.contains("库位活动预占或占用唯一约束不完整");
.doesNotContain("ALTER TABLE wh_pallet DROP COLUMN status")
.doesNotContain("active_pallet_reservation")
.doesNotContain("active_pallet_occupancy");
}
@Test
void unifiedSqlUsesOnlyNonSupplierPartnerTypes() throws IOException {
String unified = Files.readString(Path.of("..", "deploy", "sql", "头程数据库统一增量-MySQL8.sql"));
assertThat(tableDefinition(unified, "wh_partner"))
.contains("chk_wh_partner_type CHECK (partner_type IN ('CARRIER','SHIPPER','CONSIGNEE'))")
.contains("carrier_code VARCHAR(16) NULL")
.contains("chk_wh_partner_carrier_code CHECK (carrier_code IS NULL OR carrier_code IN ('MATSON','SML','ZIM','COSCO','EMC','HEDE','OTHER'))")
.doesNotContain("CUSTOMS_BROKER", "SUPPLIER或BOTH");
String partner = tableDefinition(unified.toLowerCase(), "wh_partner").replace("_utf8mb4", "");
assertThat(partner)
.contains("chk_wh_partner_type check")
.contains("partner_type in ('carrier','shipper','consignee')")
.contains("carrier_code varchar(16) default null")
.contains("chk_wh_partner_carrier_code check")
.contains("carrier_code in ('matson','sml','zim','cosco','emc','hede','other')")
.doesNotContain("customs_broker", "supplier或both");
assertThat(unified)
.contains("CALL add_column_if_missing('wh_partner', 'carrier_code', 'VARCHAR(16) NULL")
.contains("UPDATE wh_partner SET carrier_code = 'OTHER'")
.contains("queue_column_comment('baiyue_warehouse', 'wh_partner', 'carrier_code'");
.doesNotContain("CALL add_column_if_missing('wh_partner'")
.doesNotContain("queue_column_comment('baiyue_warehouse', 'wh_partner', 'carrier_code'");
}
private static String read(String file) throws IOException {
@@ -192,8 +189,14 @@ class WarehouseSchemaContractTest {
}
private static String tableDefinition(String sql, String table) {
int start = sql.indexOf("CREATE TABLE IF NOT EXISTS " + table + " (");
String marker = sql.contains("CREATE TABLE IF NOT EXISTS " + table + " (")
? "CREATE TABLE IF NOT EXISTS " + table + " ("
: "create table " + table + " (";
int start = sql.indexOf(marker);
int end = sql.indexOf("ENGINE=InnoDB", start);
if (end < 0) {
end = sql.indexOf("engine=innodb", start);
}
return sql.substring(start, end);
}
}