use sz_orm_core::schema_diff_viz::{
schema_diff_readonly, ChangeType, ConstraintDiffType, SchemaDiffReportV2, TypeDiffType,
};
#[tokio::test]
async fn test_source_write_count_zero() {
let report = schema_diff_readonly("mysql://left", "mysql://right")
.await
.unwrap();
assert_eq!(report.source_write_count, 0, "只读连接必须 0 写入");
}
#[tokio::test]
async fn test_empty_diff_no_sql() {
let report = schema_diff_readonly("mysql://a", "mysql://b")
.await
.unwrap();
assert!(report.forward_migration_sql.is_empty());
assert!(report.backward_migration_sql.is_empty());
assert_eq!(report.table_diff_count, 0);
assert_eq!(report.column_diff_count, 0);
}
#[tokio::test]
async fn test_invalid_url_protocol() {
let result = schema_diff_readonly("invalid://url", "mysql://b");
assert!(result.await.is_err());
let result = schema_diff_readonly("mysql://a", "ftp://b");
assert!(result.await.is_err());
}
#[tokio::test]
async fn test_postgres_url() {
let report = schema_diff_readonly("postgres://a", "postgres://b")
.await
.unwrap();
assert_eq!(report.source_write_count, 0);
assert_eq!(report.left_url, "postgres://a");
assert_eq!(report.right_url, "postgres://b");
}
#[tokio::test]
async fn test_sqlite_url() {
let report = schema_diff_readonly("sqlite://a.db", "sqlite://b.db")
.await
.unwrap();
assert_eq!(report.source_write_count, 0);
}
#[test]
fn test_report_v2_serde() {
let report = SchemaDiffReportV2 {
left_url: "mysql://a".to_string(),
right_url: "mysql://b".to_string(),
table_diff_count: 3,
column_diff_count: 5,
index_diff_count: 2,
constraint_diff_count: 1,
type_diff_count: 4,
forward_migration_sql: vec!["CREATE TABLE x (id BIGINT)".to_string()],
backward_migration_sql: vec!["DROP TABLE x".to_string()],
duration_ms: 100,
source_write_count: 0,
};
let json = serde_json::to_string(&report).unwrap();
let back: SchemaDiffReportV2 = serde_json::from_str(&json).unwrap();
assert_eq!(report.table_diff_count, back.table_diff_count);
assert_eq!(report.column_diff_count, back.column_diff_count);
assert_eq!(report.index_diff_count, back.index_diff_count);
assert_eq!(report.constraint_diff_count, back.constraint_diff_count);
assert_eq!(report.type_diff_count, back.type_diff_count);
assert_eq!(report.source_write_count, back.source_write_count);
assert_eq!(report.forward_migration_sql, back.forward_migration_sql);
assert_eq!(report.backward_migration_sql, back.backward_migration_sql);
}
#[test]
fn test_change_type_new_variants() {
let variants = vec![
ChangeType::AddedConstraint,
ChangeType::DroppedConstraint,
ChangeType::IndexDiff,
];
for v in &variants {
let json = serde_json::to_string(v).unwrap();
assert!(!json.is_empty());
}
}
#[test]
fn test_constraint_diff_type() {
assert_ne!(
ConstraintDiffType::AddedConstraint,
ConstraintDiffType::DroppedConstraint
);
let json = serde_json::to_string(&ConstraintDiffType::AddedConstraint).unwrap();
assert!(json.contains("AddedConstraint"));
}
#[test]
fn test_type_diff_type() {
assert_ne!(TypeDiffType::Widening, TypeDiffType::Narrowing);
let json = serde_json::to_string(&TypeDiffType::Narrowing).unwrap();
assert!(json.contains("Narrowing"));
}
#[tokio::test]
async fn test_duration_non_negative() {
let report = schema_diff_readonly("mysql://a", "mysql://b")
.await
.unwrap();
let _ = report.duration_ms;
}