#![allow(non_snake_case)] use chrono::NaiveDateTime; use serde::{Deserialize, Serialize}; use ssql::prelude::*; #[tokio::test] async fn query() -> SsqlResult<()> { let mut conn = get_client().await; let query = Person::query(); let r1 = query.all(&mut conn).await?; // let r2 = query.json(&mut conn).await?; // dbg!(&r2); let query = query.left_join::(); let r2 = query.all(&mut conn).await?; let r3 = query.all(&mut conn).await?; assert!(r1.len() == r2.len() && r2.len() == r3.len()); Ok(()) } #[tokio::test] async fn stream() { use futures_lite::stream::StreamExt; let mut client = get_client().await; let mut stream = Customerlist::query().stream(&mut client).await.unwrap(); while let Some(v) = stream.next().await { dbg!(&v); } } #[tokio::test] async fn filter() -> SsqlResult<()> { let mut client = get_client().await; let query = Customerlist::query().filter(Customerlist::col("ship_to_id")?.contains(&"9706"))?; // .filter( // Customerlist::col("volume")?.eq(&666) // )?; assert!(query.all(&mut client).await.is_ok()); Ok(()) } #[tokio::test] async fn insert_many() { let mut conn = get_client().await; let it = vec![ Person { id: 5, Email: "a".to_string(), dt: None, }, Person { id: 6, Email: "a".to_string(), dt: None, }, ]; let _a = Person::insert_many(it.clone().into_iter(), &mut conn).await; let a = Person::insert_many(it, &mut conn).await; assert_eq!(_a.unwrap(), 2); assert_eq!(a.unwrap(), 2); } #[tokio::test] async fn insert_one() { let mut conn = get_client().await; let item = Person { id: 1, Email: "f".to_string(), dt: None, }; let ret = item.insert(&mut conn).await; assert_eq!(ret.is_ok(), true); } #[tokio::test] async fn delete() { let p = Person { id: 1, Email: "".to_string(), dt: None, }; let mut conn = get_client().await; assert_eq!(p.delete(&mut conn).await.is_ok(), true); } #[tokio::test] async fn update() { let p = Person { id: 99, Email: "".to_string(), dt: None, }; let mut conn = get_client().await; assert_eq!(p.update(&mut conn).await.is_ok(), true); } #[tokio::test] async fn raw_query_and_chrono() { use futures_lite::stream::StreamExt; let mut conn = get_client().await; let m = PersonRaw::raw_query("SELECT * FROM Person where id = @p1", &[&"5"]); assert!(m.one(&mut conn).await.is_ok()); assert!(m.all(&mut conn).await.is_ok()); let mut stream = m.stream(&mut conn).await.unwrap(); while let Some(v) = stream.next().await { dbg!(&v); } } #[test] fn is_normal() { fn async_safe(_: T) {} // fn _object_safety(_: &dyn SsqlMarker) {} async_safe(PersonRaw::default()); } pub async fn get_client() -> Client> { ssql::utils::get_client("username", "password", "host", "database").await } #[derive(ORM, Debug, Default, Serialize, Deserialize)] #[ssql(table = CUSTOMER_LIST, schema = MASTER_DATA)] pub struct Customerlist { pub(crate) ship_to_id: Option, #[ssql(foreign_key = "DALI_DATA.SLOW_MOVING.stock_in_day")] pub(crate) ship_to: Option, pub(crate) volume: Option, pub(crate) container: Option, } #[derive(ORM, Debug, Default)] #[ssql(table = SLOW_MOVING, schema = DALI_DATA)] pub struct SlowMoving { pub(crate) stock_in_day: Option, pub(crate) total_value: Option, pub(crate) Week: Option, // pub(crate) Generated_Time: Option, } #[derive(ORM, Debug, Clone, Default)] #[ssql(table = Person)] pub struct Person { #[ssql(primary_key)] pub(crate) id: i32, #[ssql(foreign_key = "DALI_DATA.SLOW_MOVING.stock_in_day")] pub(crate) Email: String, dt: Option, } #[derive(ORM, Debug, Default, Serialize, Deserialize)] #[ssql(table)] pub struct PersonRaw { #[ssql(primary_key)] pub(crate) id: i32, pub(crate) Email: String, dt: Option, } #[derive(ORM, Debug, Default)] #[ssql(table = FORECAST)] pub struct Fcst { pub(crate) Customer: Option, pub(crate) Material: Option, Dv: Option, Route: Option, TransitTime: Option, Plant: Option, } #[derive(ORM)] #[ssql(table = Logs)] struct Logs { id: i32, num: String, }