Skip to content

Commit 4d9a807

Browse files
committed
test: add unit tests to improve coverage from 42% to 50%
Add new test files: - tests/unit/builder.rs - MessageBuilder tests (0% → 100%) - tests/unit/subscriptions_legacy.rs - SubscriptionChannel tests (0% → 100%) - tests/unit/notification.rs - NotificationHandler tests (11% → 100%) - tests/unit/response.rs - ResponseHandler tests (12% → 100%) Update existing tests: - tests/unit/session.rs - WebSocketSession async tests (17% → 100%) - tests/unit/config.rs - Builder methods tests (58% → 98%) Coverage improved from 42.70% to 50.62% (+7.92%)
1 parent 79fde90 commit 4d9a807

File tree

7 files changed

+747
-0
lines changed

7 files changed

+747
-0
lines changed

tests/unit/builder.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//! Unit tests for message/builder.rs
2+
3+
use deribit_websocket::message::{MessageBuilder, MessageType};
4+
5+
#[test]
6+
fn test_message_builder_new() {
7+
let builder = MessageBuilder::new();
8+
// Verify builder is created successfully
9+
assert!(format!("{:?}", builder).contains("MessageBuilder"));
10+
}
11+
12+
#[test]
13+
fn test_message_builder_default() {
14+
let builder = MessageBuilder::default();
15+
assert!(format!("{:?}", builder).contains("MessageBuilder"));
16+
}
17+
18+
#[test]
19+
fn test_message_builder_request_builder() {
20+
let mut builder = MessageBuilder::new();
21+
let request_builder = builder.request_builder();
22+
// Verify we can access the request builder
23+
assert!(format!("{:?}", request_builder).contains("RequestBuilder"));
24+
}
25+
26+
#[test]
27+
fn test_message_builder_response_handler() {
28+
let builder = MessageBuilder::new();
29+
let response_handler = builder.response_handler();
30+
assert!(format!("{:?}", response_handler).contains("ResponseHandler"));
31+
}
32+
33+
#[test]
34+
fn test_message_builder_notification_handler() {
35+
let builder = MessageBuilder::new();
36+
let notification_handler = builder.notification_handler();
37+
assert!(format!("{:?}", notification_handler).contains("NotificationHandler"));
38+
}
39+
40+
#[test]
41+
fn test_parse_message_response() {
42+
let builder = MessageBuilder::new();
43+
44+
let response_json = r#"{
45+
"jsonrpc": "2.0",
46+
"id": 1,
47+
"result": {"version": "1.2.26"}
48+
}"#;
49+
50+
let result = builder.parse_message(response_json);
51+
assert!(result.is_ok());
52+
53+
if let Ok(MessageType::Response(response)) = result {
54+
assert_eq!(response.id.as_u64().unwrap(), 1);
55+
} else {
56+
panic!("Expected Response message type");
57+
}
58+
}
59+
60+
#[test]
61+
fn test_parse_message_notification() {
62+
let builder = MessageBuilder::new();
63+
64+
let notification_json = r#"{
65+
"jsonrpc": "2.0",
66+
"method": "subscription",
67+
"params": {
68+
"channel": "ticker.BTC-PERPETUAL",
69+
"data": {"price": 50000}
70+
}
71+
}"#;
72+
73+
let result = builder.parse_message(notification_json);
74+
assert!(result.is_ok());
75+
76+
if let Ok(MessageType::Notification(notification)) = result {
77+
assert_eq!(notification.method, "subscription");
78+
} else {
79+
panic!("Expected Notification message type");
80+
}
81+
}
82+
83+
#[test]
84+
fn test_parse_message_invalid() {
85+
let builder = MessageBuilder::new();
86+
87+
let invalid_json = r#"not valid json"#;
88+
89+
let result = builder.parse_message(invalid_json);
90+
assert!(result.is_err());
91+
}
92+
93+
#[test]
94+
fn test_message_type_debug() {
95+
let builder = MessageBuilder::new();
96+
97+
let response_json = r#"{
98+
"jsonrpc": "2.0",
99+
"id": 1,
100+
"result": {"version": "1.2.26"}
101+
}"#;
102+
103+
if let Ok(msg_type) = builder.parse_message(response_json) {
104+
let debug_str = format!("{:?}", msg_type);
105+
assert!(debug_str.contains("Response"));
106+
}
107+
}
108+
109+
#[test]
110+
fn test_message_type_clone() {
111+
let builder = MessageBuilder::new();
112+
113+
let response_json = r#"{
114+
"jsonrpc": "2.0",
115+
"id": 1,
116+
"result": {"version": "1.2.26"}
117+
}"#;
118+
119+
if let Ok(msg_type) = builder.parse_message(response_json) {
120+
let cloned = msg_type.clone();
121+
assert!(format!("{:?}", cloned).contains("Response"));
122+
}
123+
}

tests/unit/config.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,89 @@ fn test_config_debug() {
8888
assert!(debug_str.contains("ws_url"));
8989
assert!(debug_str.contains("heartbeat_interval"));
9090
}
91+
92+
#[test]
93+
fn test_config_with_connection_timeout() {
94+
let config = WebSocketConfig::default()
95+
.with_connection_timeout(Duration::from_secs(120));
96+
97+
assert_eq!(config.connection_timeout, Duration::from_secs(120));
98+
}
99+
100+
#[test]
101+
fn test_config_with_credentials() {
102+
let config = WebSocketConfig::default()
103+
.with_credentials("client_id".to_string(), "client_secret".to_string());
104+
105+
assert_eq!(config.client_id, Some("client_id".to_string()));
106+
assert_eq!(config.client_secret, Some("client_secret".to_string()));
107+
}
108+
109+
#[test]
110+
fn test_config_with_client_id() {
111+
let config = WebSocketConfig::default()
112+
.with_client_id("my_client_id".to_string());
113+
114+
assert_eq!(config.client_id, Some("my_client_id".to_string()));
115+
}
116+
117+
#[test]
118+
fn test_config_with_client_secret() {
119+
let config = WebSocketConfig::default()
120+
.with_client_secret("my_secret".to_string());
121+
122+
assert_eq!(config.client_secret, Some("my_secret".to_string()));
123+
}
124+
125+
#[test]
126+
fn test_config_with_logging() {
127+
let config = WebSocketConfig::default()
128+
.with_logging(true);
129+
130+
assert!(config.enable_logging);
131+
}
132+
133+
#[test]
134+
fn test_config_with_logging_disabled() {
135+
let config = WebSocketConfig::default()
136+
.with_logging(false);
137+
138+
assert!(!config.enable_logging);
139+
}
140+
141+
#[test]
142+
fn test_config_with_log_level() {
143+
let config = WebSocketConfig::default()
144+
.with_log_level("debug".to_string());
145+
146+
assert_eq!(config.log_level, "debug");
147+
}
148+
149+
#[test]
150+
fn test_config_with_test_mode() {
151+
let config = WebSocketConfig::default()
152+
.with_test_mode(true);
153+
154+
assert!(config.test_mode);
155+
}
156+
157+
#[test]
158+
fn test_config_has_credentials_true() {
159+
let config = WebSocketConfig::default()
160+
.with_credentials("client_id".to_string(), "client_secret".to_string());
161+
162+
assert!(config.has_credentials());
163+
}
164+
165+
#[test]
166+
fn test_config_get_credentials_some() {
167+
let config = WebSocketConfig::default()
168+
.with_credentials("client_id".to_string(), "client_secret".to_string());
169+
170+
let creds = config.get_credentials();
171+
assert!(creds.is_some());
172+
173+
let (id, secret) = creds.unwrap();
174+
assert_eq!(id, "client_id");
175+
assert_eq!(secret, "client_secret");
176+
}

tests/unit/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
//! Each submodule tests a specific component of the WebSocket client.
55
66
pub mod account;
7+
pub mod builder;
78
pub mod callback;
89
pub mod client;
910
pub mod config;
1011
pub mod connection;
1112
pub mod error;
1213
pub mod message;
1314
pub mod model;
15+
pub mod notification;
1416
pub mod position;
17+
pub mod response;
1518
pub mod session;
1619
pub mod subscription_channel;
1720
pub mod subscriptions;
21+
pub mod subscriptions_legacy;
1822
pub mod trading;

0 commit comments

Comments
 (0)