From 0c457edae9f6869d740d022796adbc197f13ce87 Mon Sep 17 00:00:00 2001 From: "kingsley.hendrickse" Date: Sat, 21 Dec 2024 07:44:24 +0000 Subject: [PATCH] fmt --- src/lib/filtering.rs | 63 +++++++++++++------------ src/lib/mod.rs | 66 ++++++++++++--------------- tests/integration/integration_test.rs | 60 ++++++++++++------------ tests/unit/combined_test.rs | 4 +- 4 files changed, 94 insertions(+), 99 deletions(-) diff --git a/src/lib/filtering.rs b/src/lib/filtering.rs index 8e7f88f..427f93e 100644 --- a/src/lib/filtering.rs +++ b/src/lib/filtering.rs @@ -1,9 +1,9 @@ -use std::collections::HashMap; +use crate::ColumnDef; use eyre::Result; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use std::fmt; use std::fmt::Debug; -use crate::ColumnDef; #[derive(Debug, Clone, PartialEq)] pub enum LogicalOperator { @@ -421,7 +421,7 @@ impl FilterCondition { operator.as_sql(), formatted_values )) - }, + } // Numeric types FilterCondition::SmallIntValue { @@ -685,7 +685,10 @@ impl FilterBuilder { return Ok(Self::new()); } - fn create_condition(filter: &JsonFilter, column_defs: &HashMap<&str, ColumnDef>) -> FilterCondition { + fn create_condition( + filter: &JsonFilter, + column_defs: &HashMap<&str, ColumnDef>, + ) -> FilterCondition { match column_defs.get(filter.n.as_str()) { Some(ColumnDef::Uuid(_)) => { FilterCondition::uuid(&filter.n, parse_operator(&filter.f), Some(&filter.v)) @@ -717,23 +720,30 @@ impl FilterBuilder { } Some(ColumnDef::Boolean(_)) => { if let Ok(bool_val) = filter.v.parse::() { - FilterCondition::boolean(&filter.n, parse_operator(&filter.f), Some(bool_val)) + FilterCondition::boolean( + &filter.n, + parse_operator(&filter.f), + Some(bool_val), + ) } else { FilterCondition::text(&filter.n, parse_operator(&filter.f), Some(&filter.v)) } } - Some(ColumnDef::Timestamp(_)) => { - FilterCondition::timestamp(&filter.n, parse_operator(&filter.f), Some(&filter.v)) - } + Some(ColumnDef::Timestamp(_)) => FilterCondition::timestamp( + &filter.n, + parse_operator(&filter.f), + Some(&filter.v), + ), Some(ColumnDef::Text(_)) | Some(ColumnDef::Varchar(_)) => { FilterCondition::text(&filter.n, parse_operator(&filter.f), Some(&filter.v)) } - _ => FilterCondition::text(&filter.n, parse_operator(&filter.f), Some(&filter.v)) + _ => FilterCondition::text(&filter.n, parse_operator(&filter.f), Some(&filter.v)), } } // Always start with the first filter as the base condition - let first_condition = FilterExpression::Condition(create_condition(&filters[0], column_defs)); + let first_condition = + FilterExpression::Condition(create_condition(&filters[0], column_defs)); // If only one filter, just return it if filters.len() == 1 { @@ -812,7 +822,6 @@ impl FilterBuilder { } } - fn parse_operator(op: &str) -> FilterOperator { match op { "LIKE" => FilterOperator::Like, @@ -845,7 +854,10 @@ mod tests { fn setup_test_columns() -> HashMap<&'static str, ColumnDef> { let mut columns = HashMap::new(); columns.insert("id", ColumnDef::Uuid("id")); - columns.insert("property_full_address", ColumnDef::Text("property_full_address")); + columns.insert( + "property_full_address", + ColumnDef::Text("property_full_address"), + ); columns.insert("client_name", ColumnDef::Text("client_name")); columns.insert("name", ColumnDef::Text("name")); columns.insert("email", ColumnDef::Text("email")); @@ -1019,7 +1031,8 @@ mod tests { ); // Test case insensitive - let sql_insensitive = FilterBuilder::from_json_filters(&filters, true, &columns)?.build()?; + let sql_insensitive = + FilterBuilder::from_json_filters(&filters, true, &columns)?.build()?; assert_eq!( sql_insensitive, " WHERE (LOWER(name) LIKE LOWER('%John%') OR LOWER(email) LIKE LOWER('%gmail.com'))" @@ -1078,21 +1091,16 @@ mod tests { #[test] fn test_uuid_conditions() -> Result<()> { let columns = setup_test_columns(); - let filters = vec![ - JsonFilter { - n: "id".to_string(), - f: "=".to_string(), - v: "123e4567-e89b-12d3-a456-426614174000".to_string(), - c: None, - }, - ]; + let filters = vec![JsonFilter { + n: "id".to_string(), + f: "=".to_string(), + v: "123e4567-e89b-12d3-a456-426614174000".to_string(), + c: None, + }]; // Case sensitivity should be ignored for UUID let sql = FilterBuilder::from_json_filters(&filters, true, &columns)?.build()?; - assert_eq!( - sql, - " WHERE id = '123e4567-e89b-12d3-a456-426614174000'" - ); + assert_eq!(sql, " WHERE id = '123e4567-e89b-12d3-a456-426614174000'"); Ok(()) } @@ -1154,10 +1162,7 @@ mod tests { }]; let sql = FilterBuilder::from_json_filters(&filters, true, &columns)?.build()?; - assert_eq!( - sql, - " WHERE created_at > '2024-01-01 00:00:00'" - ); + assert_eq!(sql, " WHERE created_at > '2024-01-01 00:00:00'"); Ok(()) } diff --git a/src/lib/mod.rs b/src/lib/mod.rs index dda9fa7..97a8610 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -1,5 +1,5 @@ -use std::collections::HashMap; use eyre::Result; +use std::collections::HashMap; pub mod filtering; pub mod pagination; @@ -323,7 +323,10 @@ pub struct FilteringOptions { } impl FilteringOptions { - pub fn new(expressions: Vec, column_defs: HashMap<&'static str, ColumnDef>) -> Self { + pub fn new( + expressions: Vec, + column_defs: HashMap<&'static str, ColumnDef>, + ) -> Self { Self { expressions, case_insensitive: true, @@ -331,7 +334,10 @@ impl FilteringOptions { } } - pub fn case_sensitive(expressions: Vec, column_defs: HashMap<&'static str, ColumnDef>) -> Self { + pub fn case_sensitive( + expressions: Vec, + column_defs: HashMap<&'static str, ColumnDef>, + ) -> Self { Self { expressions, case_insensitive: false, @@ -339,13 +345,18 @@ impl FilteringOptions { } } - pub fn from_json_filters(filters: &[JsonFilter], column_defs: HashMap<&'static str, ColumnDef>) -> Result> { + pub fn from_json_filters( + filters: &[JsonFilter], + column_defs: HashMap<&'static str, ColumnDef>, + ) -> Result> { if filters.is_empty() { return Ok(None); } let filter_builder = FilterBuilder::from_json_filters(filters, true, &column_defs)?; - Ok(filter_builder.root.map(|root| Self::new(vec![root], column_defs))) + Ok(filter_builder + .root + .map(|root| Self::new(vec![root], column_defs))) } pub fn to_filter_builder(&self) -> Result { @@ -449,7 +460,10 @@ mod tests { fn setup_test_columns() -> HashMap<&'static str, ColumnDef> { let mut columns = HashMap::new(); columns.insert("id", ColumnDef::Uuid("id")); - columns.insert("property_full_address", ColumnDef::Text("property_full_address")); + columns.insert( + "property_full_address", + ColumnDef::Text("property_full_address"), + ); columns.insert("client_name", ColumnDef::Text("client_name")); columns.insert("name", ColumnDef::Text("name")); columns.insert("email", ColumnDef::Text("email")); @@ -483,10 +497,7 @@ mod tests { ]; let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?; - let sql = filtering_options - .unwrap() - .to_filter_builder()? - .build()?; + let sql = filtering_options.unwrap().to_filter_builder()?.build()?; assert_eq!( sql, " WHERE (LOWER(property_full_address) LIKE LOWER('%James%') OR LOWER(client_name) LIKE LOWER('%James%'))" @@ -519,10 +530,7 @@ mod tests { ]; let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?; - let sql = filtering_options - .unwrap() - .to_filter_builder()? - .build()?; + let sql = filtering_options.unwrap().to_filter_builder()?.build()?; assert_eq!( sql, " WHERE (LOWER(field1) = LOWER('value1') OR LOWER(field2) = LOWER('value2') OR LOWER(field3) = LOWER('value3'))" @@ -555,10 +563,7 @@ mod tests { ]; let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?; - let sql = filtering_options - .unwrap() - .to_filter_builder()? - .build()?; + let sql = filtering_options.unwrap().to_filter_builder()?.build()?; assert_eq!( sql, " WHERE (LOWER(name) LIKE LOWER('%John%') AND (age > 18 OR LOWER(city) LIKE LOWER('%York%')))" @@ -603,10 +608,7 @@ mod tests { ]; let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?; - let sql = filtering_options - .unwrap() - .to_filter_builder()? - .build()?; + let sql = filtering_options.unwrap().to_filter_builder()?.build()?; assert_eq!( sql, " WHERE (LOWER(status) = LOWER('active') AND (age > 21 OR LOWER(city) = LOWER('New York') OR LOWER(city) = LOWER('London') OR LOWER(department) = LOWER('Sales')))" @@ -634,10 +636,7 @@ mod tests { }]; let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?; - let sql = filtering_options - .unwrap() - .to_filter_builder()? - .build()?; + let sql = filtering_options.unwrap().to_filter_builder()?.build()?; assert_eq!(sql, " WHERE LOWER(name) LIKE LOWER('%John%')"); Ok(()) } @@ -661,10 +660,7 @@ mod tests { ]; let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?; - let sql = filtering_options - .unwrap() - .to_filter_builder()? - .build()?; + let sql = filtering_options.unwrap().to_filter_builder()?.build()?; assert_eq!(sql, " WHERE (age > 25 OR salary < 50000)"); Ok(()) } @@ -701,10 +697,7 @@ mod tests { // Test case insensitive let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?; - let sql = filtering_options - .unwrap() - .to_filter_builder()? - .build()?; + let sql = filtering_options.unwrap().to_filter_builder()?.build()?; assert_eq!( sql, " WHERE (LOWER(name) LIKE LOWER('%John%') OR LOWER(email) LIKE LOWER('%gmail.com'))" @@ -724,10 +717,7 @@ mod tests { }]; let filtering_options = FilteringOptions::from_json_filters(&filters, columns)?; - let sql = filtering_options - .unwrap() - .to_filter_builder()? - .build()?; + let sql = filtering_options.unwrap().to_filter_builder()?.build()?; assert_eq!(sql, format!(" WHERE id = '{}'", uuid)); Ok(()) } diff --git a/tests/integration/integration_test.rs b/tests/integration/integration_test.rs index 22fc242..9f8284f 100644 --- a/tests/integration/integration_test.rs +++ b/tests/integration/integration_test.rs @@ -3,10 +3,10 @@ use chrono::NaiveDateTime; use pg_filters::{ filtering::{FilterCondition, FilterExpression, FilterOperator}, sorting::{SortOrder, SortedColumn}, - FilteringOptions, PaginationOptions, PgFilters, ColumnDef, + ColumnDef, FilteringOptions, PaginationOptions, PgFilters, }; -use uuid::Uuid; use std::collections::HashMap; +use uuid::Uuid; fn setup_test_columns() -> HashMap<&'static str, ColumnDef> { let mut columns = HashMap::new(); @@ -66,9 +66,9 @@ async fn test_logical_filters() { ], columns.clone(), )), - columns + columns, ) - .unwrap(); + .unwrap(); let sql = filters.sql().unwrap(); println!("Generated SQL: {}", sql); @@ -90,7 +90,7 @@ async fn test_logical_filters() { assert_eq!(rows, expected_rows); }) - .await; + .await; } #[tokio::test] @@ -126,9 +126,9 @@ async fn test_date_and_uuid() { ], columns.clone(), )), - columns + columns, ) - .unwrap(); + .unwrap(); let sql = filters.sql().unwrap(); println!("Generated SQL: {}", sql); @@ -162,7 +162,7 @@ async fn test_date_and_uuid() { assert_eq!(rows, expected_rows); }) - .await; + .await; } #[tokio::test] @@ -195,9 +195,9 @@ async fn test_boolean_and_capacity() { ], columns.clone(), )), - columns + columns, ) - .unwrap(); + .unwrap(); let sql = filters.sql().unwrap(); println!("Generated SQL: {}", sql); @@ -227,7 +227,7 @@ async fn test_boolean_and_capacity() { assert_eq!(rows, expected_rows); }) - .await; + .await; } #[tokio::test] @@ -266,9 +266,9 @@ async fn test_name_and_age() { ], columns.clone(), )), - columns + columns, ) - .unwrap(); + .unwrap(); let sql = filters.sql().unwrap(); println!("Generated SQL: {}", sql); @@ -301,7 +301,7 @@ async fn test_name_and_age() { assert_eq!(rows, expected_rows); }) - .await; + .await; } #[tokio::test] @@ -340,9 +340,9 @@ async fn test_string_int() { ], columns.clone(), )), - columns + columns, ) - .unwrap(); + .unwrap(); let sql = filters.sql().unwrap(); println!("Generated SQL: {}", sql); @@ -374,7 +374,7 @@ async fn test_string_int() { assert_eq!(rows, expected_rows); }) - .await; + .await; } #[tokio::test] @@ -418,9 +418,9 @@ async fn test_float_bool() { ], columns.clone(), )), - columns + columns, ) - .unwrap(); + .unwrap(); let sql = filters.sql().unwrap(); println!("Generated SQL: {}", sql); @@ -443,7 +443,7 @@ async fn test_float_bool() { assert_eq!(rows, expected_rows); }) - .await; + .await; } #[tokio::test] @@ -475,9 +475,9 @@ async fn test_in() { })], columns.clone(), )), - columns + columns, ) - .unwrap(); + .unwrap(); let sql = filters.sql().unwrap(); println!("Generated SQL: {}", sql); @@ -503,7 +503,7 @@ async fn test_in() { assert_eq!(rows, expected_rows); }) - .await; + .await; } #[tokio::test] @@ -542,9 +542,9 @@ async fn test_starts_with() { ], columns.clone(), )), - columns + columns, ) - .unwrap(); + .unwrap(); let sql = filters.sql().unwrap(); println!("Generated SQL: {}", sql); @@ -570,7 +570,7 @@ async fn test_starts_with() { assert_eq!(rows, expected_rows); }) - .await; + .await; } #[tokio::test] @@ -603,9 +603,9 @@ async fn test_text_search() { ], columns.clone(), )), - columns + columns, ) - .unwrap(); + .unwrap(); let sql = filters.sql().unwrap(); println!("Generated SQL: {}", sql); @@ -633,5 +633,5 @@ async fn test_text_search() { assert_eq!(rows, expected_rows); }) - .await; -} \ No newline at end of file + .await; +} diff --git a/tests/unit/combined_test.rs b/tests/unit/combined_test.rs index a8fb1ee..71e2754 100644 --- a/tests/unit/combined_test.rs +++ b/tests/unit/combined_test.rs @@ -2,7 +2,7 @@ use eyre::Result; use pg_filters::{ filtering::{FilterCondition, FilterExpression, FilterOperator, LogicalOperator}, sorting::{SortOrder, SortedColumn}, - FilteringOptions, PaginationOptions, PgFilters, ColumnDef, + ColumnDef, FilteringOptions, PaginationOptions, PgFilters, }; use std::collections::HashMap; @@ -374,4 +374,4 @@ fn test_complex_filtering_with_and_or_conditions() -> Result<()> { " WHERE ((LOWER(name) = LOWER('John') AND age > 18) OR LOWER(city) IN LOWER('New York,London')) ORDER BY name ASC LIMIT 10 OFFSET 0" ); Ok(()) -} \ No newline at end of file +}