Refactor timestamp test

This commit is contained in:
Chris Tsang 2021-09-18 15:17:23 +08:00
parent 004653e86d
commit fc769a89df
4 changed files with 14 additions and 22 deletions

View File

@ -1,7 +1,7 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "log")] #[sea_orm(table_name = "applog")]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: i32, pub id: i32,
@ -9,13 +9,7 @@ pub struct Model {
pub created_at: DateTimeWithTimeZone, pub created_at: DateTimeWithTimeZone,
} }
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {} pub enum Relation {}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
unreachable!()
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -1,19 +1,19 @@
pub mod applog;
pub mod baker; pub mod baker;
pub mod bakery; pub mod bakery;
pub mod cake; pub mod cake;
pub mod cakes_bakers; pub mod cakes_bakers;
pub mod customer; pub mod customer;
pub mod lineitem; pub mod lineitem;
pub mod log;
pub mod metadata; pub mod metadata;
pub mod order; pub mod order;
pub use super::applog::Entity as Applog;
pub use super::baker::Entity as Baker; pub use super::baker::Entity as Baker;
pub use super::bakery::Entity as Bakery; pub use super::bakery::Entity as Bakery;
pub use super::cake::Entity as Cake; pub use super::cake::Entity as Cake;
pub use super::cakes_bakers::Entity as CakesBakers; pub use super::cakes_bakers::Entity as CakesBakers;
pub use super::customer::Entity as Customer; pub use super::customer::Entity as Customer;
pub use super::lineitem::Entity as Lineitem; pub use super::lineitem::Entity as Lineitem;
pub use super::log::Entity as Log;
pub use super::metadata::Entity as Metadata; pub use super::metadata::Entity as Metadata;
pub use super::order::Entity as Order; pub use super::order::Entity as Order;

View File

@ -289,22 +289,22 @@ pub async fn create_metadata_table(db: &DbConn) -> Result<ExecResult, DbErr> {
pub async fn create_log_table(db: &DbConn) -> Result<ExecResult, DbErr> { pub async fn create_log_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create() let stmt = sea_query::Table::create()
.table(log::Entity) .table(applog::Entity)
.if_not_exists() .if_not_exists()
.col( .col(
ColumnDef::new(log::Column::Id) ColumnDef::new(applog::Column::Id)
.integer() .integer()
.not_null() .not_null()
.auto_increment() .auto_increment()
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(log::Column::Json).json().not_null()) .col(ColumnDef::new(applog::Column::Json).json().not_null())
.col( .col(
ColumnDef::new(log::Column::CreatedAt) ColumnDef::new(applog::Column::CreatedAt)
.timestamp_with_time_zone() .timestamp_with_time_zone()
.not_null(), .not_null(),
) )
.to_owned(); .to_owned();
create_table(db, &stmt, Log).await create_table(db, &stmt, Applog).await
} }

View File

@ -7,27 +7,25 @@ use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel};
#[cfg(feature = "sqlx-postgres")] #[cfg(feature = "sqlx-postgres")]
async fn main() -> Result<(), DbErr> { async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("bakery_chain_schema_timestamp_tests").await; let ctx = TestContext::new("bakery_chain_schema_timestamp_tests").await;
create_applog(&ctx.db).await?;
create_log(&ctx.db).await?;
ctx.delete().await; ctx.delete().await;
Ok(()) Ok(())
} }
pub async fn create_log(db: &DatabaseConnection) -> Result<(), DbErr> { pub async fn create_applog(db: &DatabaseConnection) -> Result<(), DbErr> {
let log = log::Model { let log = applog::Model {
id: 1, id: 1,
json: Json::String("HI".to_owned()), json: Json::String("HI".to_owned()),
created_at: "2021-09-17T17:50:20+08:00".parse().unwrap(), created_at: "2021-09-17T17:50:20+08:00".parse().unwrap(),
}; };
let res = Log::insert(log.clone().into_active_model()) let res = Applog::insert(log.clone().into_active_model())
.exec(db) .exec(db)
.await?; .await?;
assert_eq!(log.id.clone(), res.last_insert_id); assert_eq!(log.id.clone(), res.last_insert_id);
assert_eq!(Log::find().one(db).await?, Some(log.clone())); assert_eq!(Applog::find().one(db).await?, Some(log.clone()));
Ok(()) Ok(())
} }