/
mcmare
/
RustAPI
Обзор
Документация
Войти
/
mcmare
/
RustAPI
Код
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
engine/tests/crud_sqlite.rs
84 строки
3 KB
mcmare
Add multi-DB CRUD engine: pooled connections, auto table creation, create/read/list/update/delete
18 июл 2026, 16:21
18 июл 2026, 16:21
c40e4bc
Код
Авторство
О чём код?
use engine::config::{ConnectionDef, CrudOperation, DbDriver, EndpointDef, EndpointKind, FieldType, HttpMethod}; use engine::db::crud; use serde_json::json; use std::{collections::HashMap, path::PathBuf}; fn schema() -> (HashMap<String, FieldType>, HashMap<String, FieldType>) { let mut input = HashMap::new(); input.insert("name".to_string(), FieldType::String); input.insert("price".to_string(), FieldType::Number); input.insert("tags".to_string(), FieldType::Array(Box::new(FieldType::String))); let mut output = input.clone(); output.insert("id".to_string(), FieldType::Uuid); output.insert("created_at".to_string(), FieldType::Datetime); (input, output) } #[tokio::test] async fn crud_roundtrip_on_sqlite() { let mut connections = HashMap::new(); connections.insert( "test_db".to_string(), ConnectionDef { driver: DbDriver::Sqlite, url: "sqlite::memory:".to_string(), }, ); let pools = engine::connect_all(&connections).await.expect("connect"); let (input, output) = schema(); let endpoint = EndpointDef { method: HttpMethod::Post, path: "/orders".to_string(), kind: EndpointKind::Crud { connection: "test_db".to_string(), table: "orders".to_string(), operation: CrudOperation::Create, primary_key: "id".to_string(), input: input.clone(), output: output.clone(), }, source_file: PathBuf::from("test"), }; engine::ensure_tables(std::slice::from_ref(&endpoint), &pools) .await .expect("auto-create table"); let pool = pools.get("test_db").unwrap(); // create let body = json!({ "name": "widget", "price": 9.99, "tags": ["a", "b"] }); let created = crud::create(pool, "orders", "id", &input, &output, &body) .await .expect("create"); assert_eq!(created["name"], json!("widget")); assert_eq!(created["price"], json!(9.99)); assert_eq!(created["tags"], json!(["a", "b"])); let id = created["id"].as_str().expect("id present").to_string(); assert!(created["created_at"].is_string()); // read let fetched = crud::read(pool, "orders", "id", &id, &output).await.expect("read"); assert_eq!(fetched["name"], json!("widget")); // list let listed = crud::list(pool, "orders", &output, 10, 0, &HashMap::new()) .await .expect("list"); assert_eq!(listed.as_array().unwrap().len(), 1); // update let update_body = json!({ "price": 12.5 }); let updated = crud::update(pool, "orders", "id", &id, &input, &output, &update_body) .await .expect("update"); assert_eq!(updated["price"], json!(12.5)); assert_eq!(updated["name"], json!("widget")); // delete crud::delete(pool, "orders", "id", &id, &output).await.expect("delete"); let err = crud::read(pool, "orders", "id", &id, &output).await.unwrap_err(); assert!(matches!(err, rustapi_core::ApiError::NotFound)); }