Skip to content

Commit 271e863

Browse files
committed
test: Add tests for TrinoRoutingGroupHeaderRouter
1 parent 4527c9b commit 271e863

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

trino-lb/src/routing/python_script.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,22 @@ def targetClusterGroup(query: str, headers: dict[str, str]) -> Optional[str]:
292292
expected.map(ToOwned::to_owned)
293293
);
294294
}
295+
296+
#[tokio::test]
297+
async fn test_invalid_script() {
298+
let script = "malformed python :)".to_string();
299+
let config = PythonScriptRouterConfig { script };
300+
301+
let result = PythonScriptRouter::new(&config, HashSet::new());
302+
assert!(matches!(result, Err(Error::ParsePythonScript { .. })));
303+
}
304+
305+
#[tokio::test]
306+
async fn test_missing_function() {
307+
let script = "foo = 42".to_string();
308+
let config = PythonScriptRouterConfig { script };
309+
310+
let result = PythonScriptRouter::new(&config, HashSet::new());
311+
assert!(matches!(result, Err(Error::FindPythonFunction { .. })));
312+
}
295313
}

trino-lb/src/routing/trino_routing_group_header.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,83 @@ impl RouterImplementationTrait for TrinoRoutingGroupHeaderRouter {
4848
None
4949
}
5050
}
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use http::{HeaderMap, HeaderName, HeaderValue};
55+
use rstest::rstest;
56+
57+
use super::*;
58+
59+
#[rstest]
60+
#[case(None)]
61+
#[case(Some("foo"))]
62+
#[case(Some("bar,bak"))]
63+
#[tokio::test]
64+
async fn test_standard_header(#[case] x_trino_routing_group: Option<&str>) {
65+
let config = serde_yaml::from_str("").unwrap();
66+
let valid_target_groups = HashSet::from(["foo".to_string(), "bar,bak".to_string()]);
67+
let router = TrinoRoutingGroupHeaderRouter::new(&config, valid_target_groups);
68+
69+
let mut headers = HeaderMap::new();
70+
if let Some(x_trino_routing_group) = x_trino_routing_group {
71+
headers.insert(
72+
HeaderName::from_static("x-trino-routing-group"),
73+
HeaderValue::from_str(x_trino_routing_group).unwrap(),
74+
);
75+
}
76+
77+
assert_eq!(
78+
router.route("", &headers).await.as_deref(),
79+
x_trino_routing_group
80+
);
81+
}
82+
83+
#[rstest]
84+
#[case("x-trino-routing-group", None)]
85+
#[case("x-trino-routing-group", Some("foo"))]
86+
#[case("x-trino-routing-group", Some("bar,bak"))]
87+
#[case("custom-header", None)]
88+
#[case("custom-header", Some("foo"))]
89+
#[case("custom-header", Some("bar,bak"))]
90+
#[tokio::test]
91+
async fn test_custom_header(
92+
#[case] header_name: String,
93+
#[case] x_trino_routing_group: Option<&str>,
94+
) {
95+
let config = TrinoRoutingGroupHeaderRouterConfig {
96+
header_name: header_name.clone(),
97+
};
98+
let valid_target_groups = HashSet::from(["foo".to_string(), "bar,bak".to_string()]);
99+
let router = TrinoRoutingGroupHeaderRouter::new(&config, valid_target_groups);
100+
101+
let mut headers = HeaderMap::new();
102+
if let Some(x_trino_routing_group) = x_trino_routing_group {
103+
headers.insert(
104+
HeaderName::from_bytes(header_name.as_bytes()).unwrap(),
105+
HeaderValue::from_str(x_trino_routing_group).unwrap(),
106+
);
107+
}
108+
109+
assert_eq!(
110+
router.route("", &headers).await.as_deref(),
111+
x_trino_routing_group
112+
);
113+
}
114+
115+
#[tokio::test]
116+
async fn test_target_group_does_not_exist() {
117+
let config = serde_yaml::from_str("").unwrap();
118+
let valid_target_groups = HashSet::from(["foo".to_string()]);
119+
let router = TrinoRoutingGroupHeaderRouter::new(&config, valid_target_groups);
120+
121+
let mut headers = HeaderMap::new();
122+
headers.insert(
123+
HeaderName::from_static("x-trino-routing-group"),
124+
HeaderValue::from_str("does not exist").unwrap(),
125+
);
126+
127+
// Currently we don't raise any error to the user and just ignore this request. This might change in the future.
128+
assert_eq!(router.route("", &headers).await, None);
129+
}
130+
}

0 commit comments

Comments
 (0)