SpecFormula AI
ISA後端指令

EntityNonExistenceValidate

驗證資料庫記錄的不存在性

EntityNonExistenceValidate 讓你驗證記錄已從資料庫中刪除,常用於測試刪除操作。

基本用法

Then 應該不存在一個待辦事項, with table:
  | todoId    | userId    |
  | $todo1.id | $Alice.id |

這段指令會:

  1. 透過 Entity Spec 將「待辦事項」對應到 todos 資料表,並取得欄位定義
  2. 以提供的欄位作為查詢條件
  3. 驗證查詢結果為空(記錄不存在)

使用場景

刪除操作後,確認記錄已從資料庫移除:

When (UID="$Alice.id") 刪除待辦事項, call table:
  | P:todoId  |
  | $todo1.id |
Then 刪除待辦事項(200)回應, with table:
  | message  |
  | 刪除成功 |
Then 應該不存在一個待辦事項, with table:
  | todoId    |
  | $todo1.id |

與 Entity Spec 的對應關係

EntityNonExistenceValidate 指令透過 Entity Spec 將業務實體名稱對應到實際的資料表,並根據 DDL 驗證欄位是否存在。

# entity_to_table_mapping.yml
entity_to_table_mapping:
  - 使用者: users
  - 待辦事項: todos
-- schema.sql
CREATE TABLE todos (
    todo_id BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT NOT NULL,
    title VARCHAR(200) NOT NULL,
    completed BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Then 應該不存在一個待辦事項, with table:
  | todoId    |
  | $todo1.id |

# SpecFormula 自動對應到 todos 資料表
# SELECT * FROM todos WHERE todo_id = ?
# 驗證查詢結果為空
Gherkin 元素對應來源
待辦事項entity_to_table_mapping 的 key
todos 資料表entity_to_table_mapping 的 value
todoId 欄位DDL 欄位定義

查找策略

PK 查詢(優先)

提供完整主鍵,直接以 PK 查詢:

| todoId    |
| $todo1.id |

# SELECT * FROM todos WHERE todo_id = ?
# 驗證查詢結果為空

Probe 查詢

使用非 PK 欄位組合作為查詢條件:

| userId    | title  |
| $Alice.id | 買牛奶 |

# SELECT * FROM todos WHERE user_id = ? AND title = ?
# 驗證查詢結果為空

限制

此指令不支援:

  • CAS 約束符號(&...
  • VAR 擷取鍵(>contextKey
  • 回應來源欄位(<executionKey

錯誤處理

Lint 階段

錯誤說明
Entity 未定義entity_to_table_mapping 中找不到
欄位不存在欄位在資料表定義中找不到
不支援的符號使用了 CAS 或 VAR 擷取語法

執行階段

錯誤說明
記錄仍然存在預期不存在的記錄實際存在
缺少查詢條件未提供任何欄位作為查詢條件

完整範例

Feature: 刪除待辦事項

  Background:
    # EntitySetup 指令(參見 EntitySetup 文件)
    Given 準備一個使用者, with table:
      | >Alice.id | name  | email             | status |
      | <userId   | Alice | alice@example.com | ACTIVE |

  Example: 刪除一筆待辦事項

    # ApiCall 指令(參見 ApiCall 文件)
    When (UID="$Alice.id") 新增待辦事項, call table:
      | >todo1.id | title  |
      | <todoId   | 買牛奶 |

    # ApiCall 指令 — P: 前綴表示 Path 參數
    When (UID="$Alice.id") 刪除待辦事項, call table:
      | P:todoId  |
      | $todo1.id |

    # ResponseValidate 指令(參見 ResponseValidate 文件)
    Then 刪除待辦事項(200)回應, with table:
      | message  |
      | 刪除成功 |

    # EntityNonExistenceValidate 指令 — 驗證記錄已刪除
    And 應該不存在一個待辦事項, with table:
      | todoId    | userId    |
      | $todo1.id | $Alice.id |

目錄