Skip to content

Commit 16dab1d

Browse files
committed
Add a tad more clarity to Constraint::Display
1 parent 2abb2a4 commit 16dab1d

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

common/src/db/query.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,12 @@ pub struct Constraint {
232232

233233
impl fmt::Display for Constraint {
234234
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
235-
write!(
236-
f,
237-
"{}{}{}",
238-
self.field.as_deref().unwrap_or(""),
239-
self.op
240-
.map_or("".to_string(), |operator| operator.to_string()),
241-
self.value.join("|")
242-
)
235+
let value = self.value.join("|");
236+
match (&self.field, &self.op) {
237+
(Some(field), Some(op)) => write!(f, "{field}{op}{value}"),
238+
(None, None) => write!(f, "{value}"),
239+
_ => write!(f, "<invalid constraint>"),
240+
}
243241
}
244242
}
245243

@@ -265,9 +263,8 @@ impl Constraint {
265263
pub(crate) mod tests {
266264
use super::*;
267265
use sea_orm::{EntityTrait, QueryOrder, QuerySelect, QueryTrait};
268-
use test_log::test;
269266

270-
#[test(tokio::test)]
267+
#[test_log::test(tokio::test)]
271268
async fn happy_path() -> Result<(), anyhow::Error> {
272269
let stmt = advisory::Entity::find()
273270
.select_only()
@@ -283,6 +280,18 @@ pub(crate) mod tests {
283280
Ok(())
284281
}
285282

283+
#[test_log::test(rstest::rstest)]
284+
#[case("f=x", vec!["f=x"])]
285+
#[case("f=x|y", vec!["f=x|y"])]
286+
#[case("x", vec!["x"])]
287+
#[case("x|y", vec!["x|y"])]
288+
#[case("x|y&f>x", vec!["x|y", "f>x"])]
289+
#[case("x!=\0&foo", vec!["x!=\0", "foo"])]
290+
fn parsing(#[case] input: &str, #[case] expected: Vec<&str>) {
291+
let constraints: Vec<_> = q(input).parse().iter().map(ToString::to_string).collect();
292+
assert_eq!(expected, constraints)
293+
}
294+
286295
/////////////////////////////////////////////////////////////////////////
287296
// Dummy Entity used for multiple tests in the crate
288297
/////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)