chore: 初始化版本控制,添加 .gitignore 纳入撮合自动化测试工程
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
# contract-match-http-autotest — HTTP API Automated Testing Demo for a Perpetual Contract Matching System
|
||||
|
||||
## 1. What Problem This Project Solves
|
||||
|
||||
A perpetual contract matching system is composed of multiple microservices—order acceptance, matching engine, risk control, clearing, etc.—with numerous APIs and complex business states. Asserting only HTTP status codes is far from enough: when an order request is submitted, the system actually performs four things behind the scenes—**funds freezing, order persistence, position changes, and cache updates**—and any failure in any one of them can lead to capital loss incidents.
|
||||
|
||||
This project demonstrates a methodology for API automated testing of such systems. It focuses on three core aspects:
|
||||
|
||||
1. **Triple Assertions**: HTTP response + database persistence + cache, all three are asserted simultaneously, not just the return value;
|
||||
2. **Formula Layer for Expected Values**: Matching formulas such as margin, fees, liquidation price, bankruptcy price are implemented independently as pure functions. Expected values are **calculated**, not “run once and copy down” — if the formula is wrong, the test fails; if the system is wrong, the test also fails;
|
||||
3. **YAML Data-Driven Testing**: Test cases are separated from data; a single test function covers normal, negative, and boundary value cases in one go.
|
||||
|
||||
To allow the project to run **without a real environment**, the system under test is implemented as a local mock engine substitute (standard library `wsgiref` HTTP server + in-memory MySQL/Redis). The verification chain is completely isomorphic to a real project, and `pytest` runs all green without connecting to any external network, database, or middleware.
|
||||
|
||||
## 2. Architecture
|
||||
|
||||
```
|
||||
┌────────────────────────── pytest Test Case Layer (case/) ──────────────────────────┐
|
||||
│ test_order_freeze.py Order placement freezes margin (triple assertions, data-driven with positive/negative cases) │
|
||||
│ test_position_open.py Market order opens position (reconciliation of position/funds/ledger/cache) │
|
||||
│ test_liquidation.py Liquidation trigger verification (price feed to liquidation price, risk fund ledger) │
|
||||
│ test_formula.py Pure function unit tests for the contract formula layer (expected values hand-calculated to prevent self-deception) │
|
||||
└───────────────┬──────────────────────────────┬───────────────────────────┘
|
||||
│ YAML data-driven │ Expected values
|
||||
┌───────────────▼──────────────┐ ┌───────────▼──────────────┐
|
||||
│ data/*.yml │ │ formula/contract_formula │
|
||||
│ addOrder.yml / liquidation.yml│ │ frozen margin/liquidation price/bankruptcy price │
|
||||
│ formula.yml │ │ unrealized PnL/forced liquidation detection │
|
||||
└───────────────────────────────┘ └──────────────────────────┘
|
||||
│ requests(HTTP) ▲ Expected vs actual
|
||||
┌───────────────▼──────────────────────────────▼───────────────────────────┐
|
||||
│ System Under Test Substitute (mock/) │
|
||||
│ server.py wsgiref HTTP server (random port, session-level startup in conftest) │
|
||||
│ engine.py Matching engine: order freeze/market order fill/cancel unfreeze/mark price/liquidation risk control │
|
||||
│ (internal fund calculation independently implemented, intentionally not reusing formula package) │
|
||||
│ fake_db.py / fake_cache.py In-memory MySQL / Redis (data plane for triple assertions) │
|
||||
└───────────────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
Layer Responsibilities:
|
||||
|
||||
| Layer | Directory | Responsibilities |
|
||||
| --- | --- | --- |
|
||||
| Test Case Layer | `case/` | Organizes business assertions; only cares about “what is given, what is expected” |
|
||||
| Data Layer | `data/` | Case parameters and expected values, `[param dict, expected dict]` structure for parametrize unpacking |
|
||||
| Formula Layer | `formula/` | Pure functions for matching business formulas, all in Decimal, the sole source of expected values |
|
||||
| API Wrapper Layer | `api/` | Secondary encapsulation of the system’s HTTP APIs; test cases do not construct URLs directly |
|
||||
| Common Layer | `common/` | YAML reading, HTTP client, Decimal assertion utilities |
|
||||
| System Substitute | `mock/` | Local matching engine + HTTP server + in-memory DB/cache |
|
||||
| Configuration Layer | `config/` | Contract parameters, fee rates, precision, etc. (all placeholder demo values) |
|
||||
|
||||
## 3. How to Run
|
||||
|
||||
```bash
|
||||
cd contract-match-http-autotest
|
||||
python -m venv venv && source venv/bin/activate # optional
|
||||
pip install -r requirements.txt # only pytest / requests / PyYAML
|
||||
python -m pytest # run all tests
|
||||
python -m pytest case/test_liquidation.py -v # run a single scenario
|
||||
python -m pytest -k liquidation # filter by keyword
|
||||
```
|
||||
|
||||
No environment setup is required: `conftest` automatically starts the mock matching service on a random port at `127.0.0.1`, and resets engine data **before each test case**, so cases are isolated, repeatable, and can be executed in any order.
|
||||
|
||||
### What Triple Assertions Look Like in a Test Case
|
||||
|
||||
Take `case/test_order_freeze.py::test_limit_order_freeze` as an example:
|
||||
|
||||
```python
|
||||
# Expected values: independently calculated by the formula layer, independent of the system implementation
|
||||
expect_frozen = formula.frozen_margin(price, quantity, marginRate, maker, taker)
|
||||
|
||||
# First: HTTP response
|
||||
assert body["code"] == 0
|
||||
|
||||
# Second: Database persistence — order fields / fund deduction and freezing / freeze ledger
|
||||
assert_decimal_equal(after["total_money"],
|
||||
Reference in New Issue
Block a user