diff --git a/sea-orm-macros/src/derives/active_model.rs b/sea-orm-macros/src/derives/active_model.rs index fe198063..701c0d71 100644 --- a/sea-orm-macros/src/derives/active_model.rs +++ b/sea-orm-macros/src/derives/active_model.rs @@ -31,7 +31,7 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result = fields.into_iter().map(|Field { ty, .. }| ty).collect(); Ok(quote!( - #[derive(Clone, Debug)] + #[derive(Clone, Debug, PartialEq)] pub struct ActiveModel { #(pub #field: sea_orm::ActiveValue<#ty>),* } diff --git a/src/database/statement.rs b/src/database/statement.rs index f27578bd..8b265433 100644 --- a/src/database/statement.rs +++ b/src/database/statement.rs @@ -7,6 +7,15 @@ pub struct Statement { pub values: Option, } +impl From for Statement { + fn from(stmt: String) -> Statement { + Statement { + sql: stmt, + values: None, + } + } +} + impl From<(String, Values)> for Statement { fn from(stmt: (String, Values)) -> Statement { Statement { diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index 3969826c..f306cb6a 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -188,6 +188,15 @@ where } } +impl PartialEq for ActiveValue +where + V: Into + Default + std::cmp::PartialEq, +{ + fn eq(&self, other: &Self) -> bool { + self.value == other.value + } +} + impl OneOrManyActiveModel for A where A: ActiveModelTrait, diff --git a/tests/basic.rs b/tests/basic.rs new file mode 100644 index 00000000..f920ff02 --- /dev/null +++ b/tests/basic.rs @@ -0,0 +1,91 @@ +use sea_orm::{DbConn, entity::*, query::*, sea_query, tests_cfg::*}; + +mod setup; + +#[async_std::test] +// cargo test --test basic -- --nocapture +async fn main() { + let db: DbConn = setup::setup().await; + + setup_schema(&db).await; + + crud_cake(&db).await.unwrap(); +} + +async fn setup_schema(db: &DbConn) { + use sea_query::*; + + let stmt = sea_query::Table::create() + .table(cake::Entity) + .col( + ColumnDef::new(cake::Column::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(cake::Column::Name).string()) + .build(SqliteQueryBuilder); + + let result = db.execute(stmt.into()).await; + println!("Create table cake: {:?}", result); +} + +async fn crud_cake(db: &DbConn) -> Result<(), ExecErr> { + let apple = cake::ActiveModel { + name: Set("Apple Pie".to_owned()), + ..Default::default() + }; + + let mut apple = apple.save(db).await?; + + println!(); + println!("Inserted: {:?}", apple); + + assert_eq!( + cake::ActiveModel { + id: Set(1), + name: Set("Apple Pie".to_owned()), + }, + apple + ); + + apple.name = Set("Lemon Tart".to_owned()); + + let apple = apple.save(db).await?; + + println!(); + println!("Updated: {:?}", apple); + + let apple = cake::Entity::find_by_id(1) + .one(db) + .await + .map_err(|_| ExecErr)?; + + assert_eq!( + Some(cake::Model { + id: 1, + name: "Lemon Tart".to_owned(), + }), + apple + ); + + let apple: cake::ActiveModel = apple.unwrap().into(); + + let result = apple.delete(db).await?; + + println!(); + println!("Deleted: {:?}", result); + + let apple = cake::Entity::find_by_id(1) + .one(db) + .await + .map_err(|_| ExecErr)?; + + assert_eq!( + None, + apple + ); + + Ok(()) +} \ No newline at end of file diff --git a/tests/setup/mod.rs b/tests/setup/mod.rs new file mode 100644 index 00000000..d6a5079b --- /dev/null +++ b/tests/setup/mod.rs @@ -0,0 +1,7 @@ +use sea_orm::{Database, DatabaseConnection}; + +pub async fn setup() -> DatabaseConnection { + Database::connect("sqlite::memory:") + .await + .unwrap() +} \ No newline at end of file