-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql.rs
More file actions
209 lines (196 loc) · 6.28 KB
/
Copy pathsql.rs
File metadata and controls
209 lines (196 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Integration tests for `qn sql …`.
mod common;
use common::run_qn;
use serde_json::json;
use std::io::Write;
use wiremock::matchers::{body_partial_json, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
fn query_body() -> serde_json::Value {
json!({
"meta": [
{"name": "time", "type": "DateTime('UTC')"},
{"name": "action_type", "type": "LowCardinality(String)"}
],
"data": [
{"time": "2026-06-24 19:43:44", "action_type": "SystemSpotSendAction"},
{"time": "2026-06-24 19:43:42", "action_type": "SystemSendAssetAction"}
],
"rows": 2,
"rows_before_limit_at_least": 18251,
"statistics": {"elapsed": 0.0067, "rows_read": 31341, "bytes_read": 1247178},
"credits": 135
})
}
#[tokio::test]
async fn query_inline_sends_camel_case_cluster_id() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/sql/rest/v1/query"))
.and(body_partial_json(json!({
"query": "SELECT 1",
"clusterId": "hyperliquid-core-mainnet"
})))
.respond_with(ResponseTemplate::new(200).set_body_json(query_body()))
.mount(&server)
.await;
let out = run_qn(
&server.uri(),
&[
"sql",
"query",
"SELECT 1",
"--cluster-id",
"hyperliquid-core-mainnet",
],
)
.await;
assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}
#[tokio::test]
async fn query_reads_from_file() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/sql/rest/v1/query"))
.and(body_partial_json(json!({ "query": "SELECT 42 FROM t" })))
.respond_with(ResponseTemplate::new(200).set_body_json(query_body()))
.mount(&server)
.await;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("query.sql");
let mut f = std::fs::File::create(&path).unwrap();
write!(f, "SELECT 42 FROM t").unwrap();
let out = run_qn(
&server.uri(),
&[
"sql",
"query",
"--file",
path.to_str().unwrap(),
"--cluster-id",
"c1",
],
)
.await;
assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}
#[tokio::test]
async fn query_missing_file_is_arg_error() {
let server = MockServer::start().await;
// No request should reach the server when the file can't be read.
Mock::given(method("POST"))
.and(path("/sql/rest/v1/query"))
.respond_with(ResponseTemplate::new(200).set_body_json(query_body()))
.expect(0)
.mount(&server)
.await;
let out = run_qn(
&server.uri(),
&[
"sql",
"query",
"--file",
"/no/such/query.sql",
"--cluster-id",
"c1",
],
)
.await;
assert_eq!(out.exit_code, 1, "stderr={}", out.stderr);
}
#[tokio::test]
async fn query_requires_a_source() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/sql/rest/v1/query"))
.respond_with(ResponseTemplate::new(200).set_body_json(query_body()))
.expect(0)
.mount(&server)
.await;
// Neither inline SQL nor --file: clap ArgGroup rejects (parse error, exit 1).
let out = run_qn(&server.uri(), &["sql", "query", "--cluster-id", "c1"]).await;
assert_eq!(out.exit_code, 1, "stderr={}", out.stderr);
}
#[tokio::test]
async fn query_inline_and_file_conflict() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/sql/rest/v1/query"))
.respond_with(ResponseTemplate::new(200).set_body_json(query_body()))
.expect(0)
.mount(&server)
.await;
// Both inline SQL and --file: clap ArgGroup rejects (parse error, exit 1).
let out = run_qn(
&server.uri(),
&[
"sql",
"query",
"SELECT 1",
"--file",
"q.sql",
"--cluster-id",
"c1",
],
)
.await;
assert_eq!(out.exit_code, 1, "stderr={}", out.stderr);
}
#[tokio::test]
async fn query_api_error_maps_to_exit_2() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/sql/rest/v1/query"))
.respond_with(ResponseTemplate::new(403).set_body_json(
json!({"statusCode": 403, "message": "only SELECT queries are allowed"}),
))
.mount(&server)
.await;
let out = run_qn(
&server.uri(),
&["sql", "query", "DELETE FROM t", "--cluster-id", "c1"],
)
.await;
assert_eq!(out.exit_code, 2, "stderr={}", out.stderr);
}
#[tokio::test]
async fn schema_happy_path() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/sql/rest/v1/schema/hyperliquid-core-mainnet"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"chain": "Hyperliquid (HyperCore)",
"cluster_id": "hyperliquid-core-mainnet",
"tables": [
{
"name": "hyperliquid_agents",
"engine": "SharedReplacingMergeTree",
"total_rows": 3322574607i64,
"partition_key": "toYYYYMM(snapshot_time)",
"sorting_key": ["block_number", "agent"],
"columns": [
{"name": "agent", "type": "FixedString(42)"},
{"name": "block_number", "type": "UInt64"}
]
}
]
})))
.mount(&server)
.await;
let out = run_qn(
&server.uri(),
&["sql", "schema", "hyperliquid-core-mainnet"],
)
.await;
assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}
#[tokio::test]
async fn schema_not_found_maps_to_exit_2() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/sql/rest/v1/schema/bad-cluster"))
.respond_with(ResponseTemplate::new(404).set_body_string("Not Found"))
.mount(&server)
.await;
let out = run_qn(&server.uri(), &["sql", "schema", "bad-cluster"]).await;
assert_eq!(out.exit_code, 2, "stderr={}", out.stderr);
}