Basic test suite (sqlite)
This commit is contained in:
parent
ec290156c5
commit
7158e78f65
@ -31,7 +31,7 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
|
||||
let ty: Vec<Type> = 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>),*
|
||||
}
|
||||
|
@ -7,6 +7,15 @@ pub struct Statement {
|
||||
pub values: Option<Values>,
|
||||
}
|
||||
|
||||
impl From<String> 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 {
|
||||
|
@ -188,6 +188,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> PartialEq for ActiveValue<V>
|
||||
where
|
||||
V: Into<Value> + Default + std::cmp::PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.value == other.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> OneOrManyActiveModel<A> for A
|
||||
where
|
||||
A: ActiveModelTrait,
|
||||
|
91
tests/basic.rs
Normal file
91
tests/basic.rs
Normal file
@ -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(())
|
||||
}
|
7
tests/setup/mod.rs
Normal file
7
tests/setup/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
use sea_orm::{Database, DatabaseConnection};
|
||||
|
||||
pub async fn setup() -> DatabaseConnection {
|
||||
Database::connect("sqlite::memory:")
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user