Test CRUD on bit datatype (#1612)

This commit is contained in:
Billy Chan 2023-05-03 12:41:52 +08:00 committed by GitHub
parent d514b511a4
commit 381e175847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 156 additions and 0 deletions

59
tests/bits_tests.rs Normal file
View File

@ -0,0 +1,59 @@
pub mod common;
use common::{features::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
#[sea_orm_macros::test]
#[cfg(feature = "sqlx-postgres")]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("bits_tests").await;
create_tables(&ctx.db).await?;
create_and_update(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn create_and_update(db: &DatabaseConnection) -> Result<(), DbErr> {
let bits = bits::Model {
id: 1,
bit0: 0,
bit1: 1,
bit8: 8,
bit16: 16,
bit32: 32,
bit64: 64,
};
let res = bits.clone().into_active_model().insert(db).await?;
let model = Bits::find().one(db).await?;
assert_eq!(model, Some(res));
assert_eq!(model, Some(bits.clone()));
let res = bits::ActiveModel {
bit32: Set(320),
bit64: Set(640),
..bits.clone().into_active_model()
}
.update(db)
.await?;
let model = Bits::find().one(db).await?;
assert_eq!(model, Some(res));
assert_eq!(
model,
Some(bits::Model {
id: 1,
bit0: 0,
bit1: 1,
bit8: 8,
bit16: 16,
bit32: 320,
bit64: 640,
})
);
Ok(())
}

View File

@ -0,0 +1,49 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "bits")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(
column_type = r#"custom("BIT")"#,
select_as = "BIGINT",
save_as = "BIT"
)]
pub bit0: i64,
#[sea_orm(
column_type = r#"custom("BIT(1)")"#,
select_as = "BIGINT",
save_as = "BIT(1)"
)]
pub bit1: i64,
#[sea_orm(
column_type = r#"custom("BIT(8)")"#,
select_as = "BIGINT",
save_as = "BIT(8)"
)]
pub bit8: i64,
#[sea_orm(
column_type = r#"custom("BIT(16)")"#,
select_as = "BIGINT",
save_as = "BIT(16)"
)]
pub bit16: i64,
#[sea_orm(
column_type = r#"custom("BIT(32)")"#,
select_as = "BIGINT",
save_as = "BIT(32)"
)]
pub bit32: i64,
#[sea_orm(
column_type = r#"custom("BIT(64)")"#,
select_as = "BIGINT",
save_as = "BIT(64)"
)]
pub bit64: i64,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -2,6 +2,7 @@ pub mod active_enum;
pub mod active_enum_child;
pub mod applog;
pub mod binary;
pub mod bits;
pub mod byte_primary_key;
pub mod collection;
pub mod custom_active_model;
@ -26,6 +27,7 @@ pub use active_enum::Entity as ActiveEnum;
pub use active_enum_child::Entity as ActiveEnumChild;
pub use applog::Entity as Applog;
pub use binary::Entity as Binary;
pub use bits::Entity as Bits;
pub use byte_primary_key::Entity as BytePrimaryKey;
pub use collection::Entity as Collection;
pub use dyn_table_name_lazy_static::Entity as DynTableNameLazyStatic;

View File

@ -48,6 +48,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
create_edit_log_table(db).await?;
create_teas_table(db).await?;
create_binary_table(db).await?;
create_bits_table(db).await?;
create_dyn_table_name_lazy_static_table(db).await?;
if DbBackend::Postgres == db_backend {
@ -561,6 +562,51 @@ pub async fn create_binary_table(db: &DbConn) -> Result<ExecResult, DbErr> {
create_table(db, &create_table_stmt, Binary).await
}
pub async fn create_bits_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let create_table_stmt = sea_query::Table::create()
.table(bits::Entity.table_ref())
.col(
ColumnDef::new(bits::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(bits::Column::Bit0)
.custom(Alias::new("BIT"))
.not_null(),
)
.col(
ColumnDef::new(bits::Column::Bit1)
.custom(Alias::new("BIT(1)"))
.not_null(),
)
.col(
ColumnDef::new(bits::Column::Bit8)
.custom(Alias::new("BIT(8)"))
.not_null(),
)
.col(
ColumnDef::new(bits::Column::Bit16)
.custom(Alias::new("BIT(16)"))
.not_null(),
)
.col(
ColumnDef::new(bits::Column::Bit32)
.custom(Alias::new("BIT(32)"))
.not_null(),
)
.col(
ColumnDef::new(bits::Column::Bit64)
.custom(Alias::new("BIT(64)"))
.not_null(),
)
.to_owned();
create_table(db, &create_table_stmt, Bits).await
}
pub async fn create_dyn_table_name_lazy_static_table(db: &DbConn) -> Result<(), DbErr> {
use dyn_table_name_lazy_static::*;