use super::*;
use proptest::prelude::*;
use std::collections::BTreeMap;
#[test]
fn test_new_api_request() {
let req = ApiRequest::new("market_state", r#"{"market":"US"}"#);
assert_eq!(req.method, "market_state");
assert_eq!(req.biz_content, r#"{"market":"US"}"#);
}
#[test]
fn test_new_api_request_empty_biz_content() {
let req = ApiRequest::new("market_state", "{}");
assert_eq!(req.method, "market_state");
assert_eq!(req.biz_content, "{}");
}
#[test]
fn test_from_params_with_map() {
let mut params = BTreeMap::new();
params.insert("market", "US");
params.insert("symbol", "AAPL");
let req = ApiRequest::from_params("quote_real_time", ¶ms).unwrap();
assert_eq!(req.method, "quote_real_time");
let parsed: serde_json::Value = serde_json::from_str(&req.biz_content).unwrap();
assert_eq!(parsed["market"], "US");
assert_eq!(parsed["symbol"], "AAPL");
}
#[test]
fn test_api_request_serialization() {
let req = ApiRequest::new("market_state", r#"{"market":"US"}"#);
let json = serde_json::to_string(&req).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(parsed["method"], "market_state");
assert_eq!(parsed["biz_content"], r#"{"market":"US"}"#);
}
fn valid_api_method() -> impl Strategy<Value = String> {
prop_oneof![
Just("market_state".to_string()),
Just("quote_real_time".to_string()),
Just("kline".to_string()),
Just("place_order".to_string()),
Just("modify_order".to_string()),
Just("cancel_order".to_string()),
Just("get_position".to_string()),
Just("get_orders".to_string()),
Just("option_chain".to_string()),
Just("future_exchange".to_string()),
]
}
fn valid_biz_params() -> impl Strategy<Value = BTreeMap<String, String>> {
proptest::collection::btree_map(
"[a-z_]{1,15}",
"[a-zA-Z0-9]{1,20}",
0..5,
)
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
#[test]
fn api_request_construction_correctness(
method in valid_api_method(),
params in valid_biz_params(),
) {
let req = ApiRequest::from_params(&method, ¶ms).unwrap();
prop_assert_eq!(&req.method, &method);
let parsed: BTreeMap<String, String> = serde_json::from_str(&req.biz_content).unwrap();
prop_assert_eq!(&parsed, ¶ms);
}
}