test(crud): 补路径3写 numeric/int 列回归测试 + 强化 status 断言 + 测试数据自清理

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
adminandClaude Code committed 2026-10-26 01:25:57 +08:00
1 parent 9a2b3fb770
commit f49dc36b44
1 file changed
+68 -2
+68 -2
View File
@@ -755,14 +755,17 @@ async fn path3_writes_creator_time_and_keeps_other_columns() {
.expect("清理失败");
// ---- 第一轮:先把这一行**真的建出来**(路径 3 的判定是「库里查得到该 id」)----
// 🔴 第一轮 body 里**显式给 status**(不是 spec 的默认值 `"草稿"`)——
// 这样第二轮的 status 断言才有鉴别力。
let r = crud::demo_item_save(
State(state.clone()),
headers.clone(),
Bytes::from(json!({"title": title}).to_string()),
Bytes::from(json!({"title": title, "status": "进行中"}).to_string()),
)
.await;
let first = data_obj(&r);
let id = first["id"].as_str().expect("id 必须是字符串").to_string();
assert_eq!(first["status"], "进行中", "第一轮必须写成 body 给的 status");
// ---- 第二轮:同一个 id + body 里带 creatorTime ⇒ 路径 3 覆盖更新 ----
let r2 = crud::demo_item_save(
@@ -782,5 +785,68 @@ async fn path3_writes_creator_time_and_keeps_other_columns() {
// 🔴 同时断言**其它列没被这一轮改动**(防止本测试退化成「只测 creatorTime 变了」)。
assert_eq!(data2["id"], id.as_str(), "id 不得变");
assert_eq!(data2["title"], title, "title 必须保持上一轮的值");
assert_eq!(data2["status"], "草稿", "status 必须保持上一轮的值");
assert_eq!(
data2["status"], "进行中",
"status 不在第二轮的 body 里 ⇒ 不得被改写(更不得被重置成默认值)"
);
// ---- 清理本测试造的那一行(成功路径不留行;断言失败时的残留是「测试失败」的自然结果)----
db.exec(&format!("DELETE FROM demo_item WHERE f_id = '{id}'"))
.await
.expect("清理失败");
}
/// 路径 3 写 **numeric / int 列**的回归测试(复审 M1)。
///
/// 🔴 这条是 UPDATE 占位符 `$n::text{cast}` 的**防回归钉子**:
/// 改回 `$n{cast}` ⇒ PG 把参数**推断成** `numeric` / `integer`
/// ⇒ pglite 拿 `Option<String>` 去绑 ⇒ `code=400`(而不是 200)。
/// 用 `book_shop_book` —— 它同时有 `f_price`(numeric)与 `f_stock`(int)⇒ 一条覆盖两种类型。
#[tokio::test]
async fn path3_writes_numeric_and_int_columns() {
let db = db().await;
let _guard = LOCK.lock().await;
let (state, headers) = admin_state(db).await;
let title = "it-路径3-numeric-1";
db.exec(&format!("DELETE FROM book_shop_book WHERE f_title = '{title}'"))
.await
.expect("清理失败");
// ---- 第一轮:建行(专属 title + price)----
let r = crud::book_save(
State(state.clone()),
headers.clone(),
Bytes::from(json!({"title": title, "price": "1.00"}).to_string()),
)
.await;
let first = data_obj(&r);
let id = first["id"].as_str().expect("id 必须是字符串").to_string();
assert_eq!(first["price"], "1.00", "第一轮的 price 必须是字符串 1.00");
// ---- 第二轮:同一个 id + body 里带 price / stock ⇒ 路径 3 覆盖更新 ----
let r2 = crud::book_save(
State(state.clone()),
headers.clone(),
Bytes::from(json!({"id": id, "price": "9.90", "stock": 100}).to_string()),
)
.await;
// 🔴 改前这里是 400 ⇒ 这条断言就是那颗钉子。
assert_eq!(r2.0.code, 200, "路径 3 必须成功: code={}", r2.0.code);
let data2 = data_obj(&r2);
// 🔴 numeric 列读回恒是**字符串**(投影走 `f_price::text AS "price"`)。
assert_eq!(data2["price"], "9.90", "price 必须是字符串 9.90");
assert!(data2["price"].is_string(), "price 必须是 JSON 字符串");
// 🔴 int 列读回是**数字**(不加 `::text`)。
assert_eq!(data2["stock"], 100, "stock 必须是数字 100");
assert!(data2["stock"].is_number(), "stock 必须是 JSON 数字");
// 🔴 body 没给的列保持原值(这一轮只覆盖 body 里出现的列)。
assert_eq!(data2["title"], title, "title 必须保持第一轮的值");
assert_eq!(data2["id"], id.as_str(), "id 不得变");
// ---- 清理本测试造的那一行(成功路径不留行)----
db.exec(&format!("DELETE FROM book_shop_book WHERE f_id = '{id}'"))
.await
.expect("清理失败");
}