#[cfg(test)] mod tests { use std::net::TcpStream; use json::{array, object}; use df_helper::orm::db::Db; use df_helper::{datetime, files}; use df_helper::files::file; use std::io::{Read, Write}; #[test] fn select_not_in() { let conf = files::file::file_content_get_json("tests/config/db_srv.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fields("name,id") .fetch_sql(false) .where_and("name", "notin", array![3,6]) .select(); println!("{:#}", sql); } #[test] fn order() { let conf = files::file::file_content_get_json("tests/config/db_srv.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .order("num_int", 1) .order("num_decimal", 1) .fields("num_int,num_decimal,id") .limit(10) .fetch_sql(false) .select(); println!("{:#}", sql); assert_eq!(sql.len(), 10); } #[test] fn select_where_column() { let conf = files::file::file_content_get_json("tests/config/db_srv.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .where_column("num_int", ">=", "num_decimal") .fetch_sql(false) .count(); println!("{}", sql); } #[test] fn find() { let conf = files::file::file_content_get_json("tests/config/db_srv.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .order("id", 0) .fetch_sql(false) .find(); println!("{:#}", sql); } #[test] fn column() { let conf = files::file::file_content_get_json("tests/config/db_srv.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .distinct(true) .fetch_sql(false) .fields("name") .limit(15) .column(); println!("{:#}", sql); } #[test] fn value() { let conf = files::file::file_content_get_json("tests/config/db_srv.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .order("id", 1) .fetch_sql(false) .value("id"); println!("{:#}", sql); } #[test] fn update() { let conf = files::file::file_content_get_json("tests/config/db_srv.json"); let mut db = Db::connect(conf); let data = object! { name:"5555" }; let sql = db.table("tests") .fetch_sql(false) .update(data); println!("{:#}", sql); } #[test] fn update_where() { let conf = files::file::file_content_get_json("tests/config/db_srv.json"); let mut db = Db::connect(conf); let data = object! { name:"6666" }; let sql = db.table("tests") .fetch_sql(false) .where_and("name", "in", array![3333,4444]) .update(data); println!("{:#}", sql); } #[test] fn update_case_when() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let data = array![ object! { name:6, num_decimal:1, num_int:3 }, object! { name:7, num_decimal:3, num_int:5 }, object! { name:8, num_decimal:4, num_int:2 } ]; let sql = db.table("tests") .fetch_sql(false) // .where_and("name", "in", array![3333,4444]) .update_case("name", data); println!("{:#}", sql); } #[test] fn delete() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(false) .where_and("str_name", "in", array![5555]) .delete(); println!("{:#}", sql); } #[test] fn count() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(false) // .where_and("name", "in", array![4444]) .count(); println!("{:#}", sql); } #[test] fn group() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(true) .group("name,num_int") .column(); println!("{:#}", sql); } #[test] fn max() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(false) .max("name"); println!("{:#}", sql); } #[test] fn min() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(false) .min("name"); println!("{:#}", sql); let sql = db.table("tests") .fetch_sql(false) .min("name,num_int"); println!("{:#}", sql); } #[test] fn avg() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(false) .avg("name"); println!("{:#}", sql); let sql = db.table("tests") .fetch_sql(false) .avg("name,num_int"); println!("{:#}", sql); } #[test] fn sum() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(false) .sum("name,num_int"); println!("{:#}", sql); let sql = db.table("tests") .fetch_sql(false) .sum("name"); println!("{:#}", sql); } #[test] fn inc() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(false) .inc("name", 1.into()) .update(object! { "num_int":"123123" }); println!("{:#}", sql); } #[test] fn dec() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(false) .dec("name", 1.into()) .update(object! { "num_int":"33" }); println!("{:#}", sql); } #[test] fn testssss() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .distinct(true) .fetch_sql(false) .fields("name") .column(); println!("{:#}", sql); let sql = db.table("tests") .distinct(true) .fetch_sql(false) .fields("name") .column(); println!("{:#}", sql); } #[test] fn delete_all() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fetch_sql(false) .delete(); println!("{:#}", sql); } #[test] fn transaction() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); db.transaction(); let count = db.table("tests").count(); println!("{:#}", count); db.table("tests").limit(1).delete(); db.commit(); let count = db.table("tests").count(); println!("{:#}", count); } #[test] fn select_where_null() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .where_and("str_text", "null", true.into()) .fetch_sql(false) .count(); println!("{:#}", sql); } #[test] fn select_where_between() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fields("name") .where_and("name", "between", array!["4","8"]) .fetch_sql(false) .select(); println!("{:#}", sql); } #[test] fn select_where_notbetween() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fields("name") .where_and("name", "notbetween", array!["2",8]) .fetch_sql(false) .select(); println!("{:#}", sql); } #[test] fn union() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("tests") .fields("name") .fetch_sql(false) .union("tests1", true).select(); println!("{:#}", sql); } #[test] fn fieldsinfo() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("admin_admin") .hidden("add_time") .fieldsinfo(); println!("{:#}", sql); } #[test] fn hidden() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("admin_admin") .hidden("pass,phone,add_time,sex").select(); println!("{:#}", sql); } #[test] fn like() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let sql = db.table("admin_admin") .where_and("phone", "like", "%137080%".into()) .where_or("name", "like", "%测试%".into()) .select(); println!("{:#}", sql); } #[test] fn admin() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf); let data = db.table("order_admin").select(); println!("{:#}", data); } #[test] fn order_temp() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf.clone()); let data = db.set_connection("order").table("order_temp").select(); let mut list = array![]; for item in data.members() { let mut row = object! {}; if item["deltime"].as_f64().unwrap() > 0.0 { continue; } row["add_time"] = item["addtime"].clone(); row["upt_time"] = item["uptime"].clone(); row["img"] = item["img"].clone(); row["imgs"] = item["imgs"].clone(); row["pv"] = item["pv"].clone(); row["mode"] = item["type"].clone(); row["name"] = item["name"].clone(); list.push(row).unwrap(); } let data = db.set_connection("text").table("order_temp").add(list); println!("{:#}", data); } #[test] fn admin_admin() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf.clone()); // let data = db.set_connection("text").table("admin_admin").select(); // println!("{:#}", data); let data = db.table("admin_admin") // .where_and("state", "=", "已启用".into()) .where_and("note", "=", "123213".into()) .select(); println!("{:#}", data); } #[test] fn test_c() { let conf = files::file::file_content_get_json("tests/config/db.json"); let mut db = Db::connect(conf.clone()); let table_list = vec!["admin_admin", "user_user", "order_temp", "order_order", "order_orderlist", "file_file"]; for table in table_list { let tables = file::file_content_get_json(format!("tests/sql/{}.json", table).as_str()); if db.query_table(table) { db.update_table(table, tables); } else { db.create_table(table, tables); } } } }