This commit is contained in:
2026-09-01 15:46:30 +08:00
parent 8a94c541cb
commit e02c463be7
3 changed files with 280 additions and 186 deletions
@@ -4,10 +4,32 @@ import org.junit.jupiter.api.Test;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
class CustomsSqlContractTest {
private static final Set<String> CUSTOMS_TABLES = Set.of(
"fl_customs_container",
"fl_customs_customer_document",
"fl_customs_customer_document_inbound",
"fl_customs_customer_document_version",
"fl_customs_frozen_document_inbound",
"fl_customs_completion_document",
"fl_customs_completion_document_version",
"fl_customs_container_history",
"fl_customs_command");
private static final Pattern CREATE_TABLE = Pattern.compile(
"(?i)CREATE\\s+TABLE\\s+IF\\s+NOT\\s+EXISTS\\s+`?([a-zA-Z0-9_]+)`?\\s*\\(");
private static final Pattern COLUMN = Pattern.compile(
"(?i)^\\s*`?([a-zA-Z0-9_]+)`?\\s+(bigint|char|datetime|int|json|tinyint|varchar)\\b");
private static final Pattern CHINESE_COMMENT = Pattern.compile(
"(?i)\\bCOMMENT\\s*=?\\s*'(?:(?:''|\\\\'|[^'])*[\\u3400-\\u9fff](?:''|\\\\'|[^'])*)'");
@Test
void moduleSqlContainsContainerStateManyToManyFreezeVersionAndIdempotencyContracts() throws Exception {
String sql = Files.readString(Path.of("src/main/resources/db/phase3-container-customs-schema.sql"));
@@ -19,4 +41,76 @@ class CustomsSqlContractTest {
.contains("fl_customs_command", "uk_customs_command(actor_id,action,idempotency_key)")
.doesNotContain("platform:firstleg:loading-list:export");
}
@Test
void everyCustomsColumnHasAnAccurateChineseComment() throws Exception {
String sql = Files.readString(Path.of("src/main/resources/db/phase3-container-customs-schema.sql"));
Matcher tableMatcher = CREATE_TABLE.matcher(sql);
Set<String> found = new java.util.HashSet<>();
List<String> missingComments = new ArrayList<>();
while (tableMatcher.find()) {
String table = tableMatcher.group(1);
if (!CUSTOMS_TABLES.contains(table)) continue;
found.add(table);
int open = tableMatcher.end() - 1;
int close = matchingParen(sql, open);
for (String definition : splitDefinitions(sql.substring(open + 1, close))) {
Matcher columnMatcher = COLUMN.matcher(definition);
if (columnMatcher.find() && !CHINESE_COMMENT.matcher(definition).find()) {
missingComments.add(table + "." + columnMatcher.group(1));
}
}
}
assertThat(found).containsExactlyInAnyOrderElementsOf(CUSTOMS_TABLES);
assertThat(missingComments).isEmpty();
}
private static int matchingParen(String sql, int open) {
int depth = 0;
char quote = 0;
for (int index = open; index < sql.length(); index++) {
char current = sql.charAt(index);
if (quote != 0) {
if (current == '\\') index++;
else if (current == quote) {
if (index + 1 < sql.length() && sql.charAt(index + 1) == quote) index++;
else quote = 0;
}
continue;
}
if (current == '\'' || current == '"' || current == '`') quote = current;
else if (current == '(') depth++;
else if (current == ')' && --depth == 0) return index;
}
throw new AssertionError("unclosed CREATE TABLE at " + open);
}
private static List<String> splitDefinitions(String body) {
List<String> definitions = new ArrayList<>();
int start = 0;
int depth = 0;
char quote = 0;
for (int index = 0; index < body.length(); index++) {
char current = body.charAt(index);
if (quote != 0) {
if (current == '\\') index++;
else if (current == quote) {
if (index + 1 < body.length() && body.charAt(index + 1) == quote) index++;
else quote = 0;
}
continue;
}
if (current == '\'' || current == '"' || current == '`') quote = current;
else if (current == '(') depth++;
else if (current == ')') depth--;
else if (current == ',' && depth == 0) {
definitions.add(body.substring(start, index));
start = index + 1;
}
}
definitions.add(body.substring(start));
return definitions;
}
}