|
| 1 | +#![allow(unused_imports, dead_code)] |
| 2 | + |
| 3 | +pub mod common; |
| 4 | + |
| 5 | +pub use sea_orm::{DbBackend, DbErr}; |
| 6 | + |
| 7 | +// DATABASE_URL=cockroachdb://...:26257/... cargo test --features cockroachdb,sqlx-postgres,runtime-tokio --test cockroachdb_tests |
| 8 | +#[sea_orm_macros::test] |
| 9 | +#[cfg(feature = "cockroachdb")] |
| 10 | +async fn test_cockroachdb_backend_detection() -> Result<(), DbErr> { |
| 11 | + // Test is_prefix_of for various URL schemes |
| 12 | + assert!(DbBackend::Cockroach.is_prefix_of("cockroachdb://localhost:26257/test")); |
| 13 | + assert!(DbBackend::Cockroach.is_prefix_of("postgres://localhost:26257/test")); |
| 14 | + assert!(DbBackend::Cockroach.is_prefix_of("postgresql://localhost:26257/test")); |
| 15 | + assert!(!DbBackend::Cockroach.is_prefix_of("mysql://localhost:3306/test")); |
| 16 | + assert!(!DbBackend::Cockroach.is_prefix_of("sqlite://test.db")); |
| 17 | + |
| 18 | + // Test as_str |
| 19 | + assert_eq!(DbBackend::Cockroach.as_str(), "Cockroach"); |
| 20 | + |
| 21 | + // Test support_returning |
| 22 | + assert!(DbBackend::Cockroach.support_returning()); |
| 23 | + |
| 24 | + Ok(()) |
| 25 | +} |
| 26 | + |
| 27 | +#[sea_orm_macros::test] |
| 28 | +#[cfg(feature = "cockroachdb")] |
| 29 | +async fn test_cockroachdb_schema_identity() -> Result<(), DbErr> { |
| 30 | + use sea_orm::sea_query::{ColumnDef, Alias, PostgresQueryBuilder, Table}; |
| 31 | + |
| 32 | + // Create a table with auto-increment primary key |
| 33 | + let table = Table::create() |
| 34 | + .table(Alias::new("test_table")) |
| 35 | + .col( |
| 36 | + ColumnDef::new(Alias::new("id")) |
| 37 | + .integer() |
| 38 | + .not_null() |
| 39 | + .auto_increment() |
| 40 | + .primary_key(), |
| 41 | + ) |
| 42 | + .col(ColumnDef::new(Alias::new("name")).string()) |
| 43 | + .to_owned(); |
| 44 | + |
| 45 | + // Build the SQL - should use GENERATED BY DEFAULT AS IDENTITY, not SERIAL |
| 46 | + let sql = table.to_string(PostgresQueryBuilder); |
| 47 | + |
| 48 | + // CockroachDB uses GENERATED BY DEFAULT AS IDENTITY instead of SERIAL |
| 49 | + assert!( |
| 50 | + sql.contains("GENERATED BY DEFAULT AS IDENTITY"), |
| 51 | + "Expected GENERATED BY DEFAULT AS IDENTITY in schema, got: {sql}" |
| 52 | + ); |
| 53 | + assert!( |
| 54 | + !sql.contains("SERIAL"), |
| 55 | + "SERIAL should not be used for CockroachDB, got: {sql}" |
| 56 | + ); |
| 57 | + |
| 58 | + Ok(()) |
| 59 | +} |
| 60 | + |
| 61 | +#[sea_orm_macros::test] |
| 62 | +#[cfg(feature = "cockroachdb")] |
| 63 | +async fn test_cockroachdb_boolean_value() -> Result<(), DbErr> { |
| 64 | + use sea_orm::sea_query::Value; |
| 65 | + |
| 66 | + let true_val = DbBackend::Cockroach.boolean_value(true); |
| 67 | + let false_val = DbBackend::Cockroach.boolean_value(false); |
| 68 | + |
| 69 | + // Boolean values should be converted correctly |
| 70 | + assert_eq!(true_val, Value::Bool(Some(true))); |
| 71 | + assert_eq!(false_val, Value::Bool(Some(false))); |
| 72 | + |
| 73 | + Ok(()) |
| 74 | +} |
0 commit comments