use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use tokio::{runtime::Runtime, join};
use std::sync::Arc;
use vorm;
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Cat {
name: String,
drinks_from_mugs: bool,
}
#[tokio::test]
async fn retort() {
let rt = Runtime::new().unwrap();
let rt_2 = Runtime::new().unwrap();
let records = vorm::Records::<Cat>::new(
"cats",
&vorm::get_db("mongodb://127.0.0.1:27017", "testing")
.await
.unwrap(),
);
let records = Arc::new(records);
let records_for_benben = records.clone();
let a = rt.spawn(async move {
records_for_benben.put(&Cat {
name: "笨笨".to_owned(),
drinks_from_mugs: true,
}).await.unwrap();
});
let records_for_mimi = records.clone();
let b = rt_2.spawn(async move {
records_for_mimi.put(&Cat {
name: "咪咪".to_owned(),
drinks_from_mugs: false,
}).await.unwrap();
});
let results = join!(a, b);
assert!(results.0.is_ok());
assert!(results.1.is_ok());
assert_eq!(records.count().await.unwrap(), 2);
rt.shutdown_background();
rt_2.shutdown_background();
}