Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsleyh committed Aug 4, 2024
1 parent e131bea commit 52eff6e
Show file tree
Hide file tree
Showing 10 changed files with 989 additions and 579 deletions.
392 changes: 260 additions & 132 deletions src/lib/filtering.rs

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ impl PgFilters {
let sorting = Sorting::new(sorting_columns);

let filters = filtering_rules.map(|filtering_rules| {
Filtering::new(filtering_rules.filtering_rules, filtering_rules.case_insensitive)
Filtering::new(
filtering_rules.filtering_rules,
filtering_rules.case_insensitive,
)
});

PgFilters {
Expand Down
16 changes: 12 additions & 4 deletions src/lib/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl Pagination {
};
let next_page = if current_page < total_pages && total_pages > 0 {
current_page + 1
} else if current_page < total_pages{
total_pages
} else if current_page < total_pages {
total_pages
} else {
current_page
};
Expand Down Expand Up @@ -120,7 +120,11 @@ impl Paginate {
per_page_limit: i64,
total_records: i64,
) -> Paginate {
let per_page_limit = if per_page_limit > 0 { per_page_limit } else { 10 };
let per_page_limit = if per_page_limit > 0 {
per_page_limit
} else {
10
};
let per_page = if per_page > per_page_limit {
per_page_limit
} else {
Expand All @@ -130,7 +134,11 @@ impl Paginate {

let total_records = if total_records > 0 { total_records } else { 0 };

let total_pages = if total_records > 0 { (total_records as f64 / per_page as f64).ceil() as i64 } else { 0 };
let total_pages = if total_records > 0 {
(total_records as f64 / per_page as f64).ceil() as i64
} else {
0
};
let current_page = if current_page < 1 { 1 } else { current_page };
let current_page = if current_page > total_pages && total_pages > 0 {
total_pages
Expand Down
75 changes: 45 additions & 30 deletions tests/integration/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::integration::{get_client, setup_data};
use pg_filters::{filtering::{FilteringRule, ColumnName}, sorting::{SortedColumn}, PaginationOptions, PgFilters};
use pg_filters::FilteringOptions;
use pg_filters::{
filtering::{ColumnName, FilteringRule},
sorting::SortedColumn,
PaginationOptions, PgFilters,
};

#[tokio::test]
async fn test_string_int() {
Expand Down Expand Up @@ -28,11 +32,14 @@ async fn test_string_int() {
let query = format!("SELECT * FROM person {}", sql);
let rows = client.query(query.as_str(), &[]).await.unwrap();

let rows: Vec<(String, i32)> = rows.iter().map(|row| {
let name: String = row.get("name");
let age: i32 = row.get("age");
(name, age)
}).collect();
let rows: Vec<(String, i32)> = rows
.iter()
.map(|row| {
let name: String = row.get("name");
let age: i32 = row.get("age");
(name, age)
})
.collect();

let expected_rows = vec![
("name19".to_string(), 19),
Expand Down Expand Up @@ -76,21 +83,21 @@ async fn test_float_bool() {
let query = format!("SELECT * FROM person {}", sql);
let rows = client.query(query.as_str(), &[]).await.unwrap();

let rows: Vec<(String, f64, bool)> = rows.iter().map(|row| {
let name: String = row.get("name");
let capacity: f64 = row.get("capacity");
let active: bool = row.get("active");
(name, capacity, active)
}).collect();
let rows: Vec<(String, f64, bool)> = rows
.iter()
.map(|row| {
let name: String = row.get("name");
let capacity: f64 = row.get("capacity");
let active: bool = row.get("active");
(name, capacity, active)
})
.collect();

let expected_rows = vec![
("name4".to_string(), 4.0, true),
];
let expected_rows = vec![("name4".to_string(), 4.0, true)];

assert_eq!(rows, expected_rows);
}


#[tokio::test]
async fn test_in() {
let client = get_client().await;
Expand All @@ -107,20 +114,26 @@ async fn test_in() {
SortedColumn::new("age", "desc"),
SortedColumn::new("name", "asc"),
],
Some(FilteringOptions::new(vec![
FilteringRule::new("where", ColumnName::Int("age"), "in", "11,12,13"),
])),
Some(FilteringOptions::new(vec![FilteringRule::new(
"where",
ColumnName::Int("age"),
"in",
"11,12,13",
)])),
);

let sql = filters.sql();
let query = format!("SELECT * FROM person {}", sql);
let rows = client.query(query.as_str(), &[]).await.unwrap();

let rows: Vec<(String, i32)> = rows.iter().map(|row| {
let name: String = row.get("name");
let age: i32 = row.get("age");
(name, age)
}).collect();
let rows: Vec<(String, i32)> = rows
.iter()
.map(|row| {
let name: String = row.get("name");
let age: i32 = row.get("age");
(name, age)
})
.collect();

let expected_rows = vec![
("name13".to_string(), 13),
Expand All @@ -131,7 +144,6 @@ async fn test_in() {
assert_eq!(rows, expected_rows);
}


#[tokio::test]
async fn test_starts_with() {
let client = get_client().await;
Expand Down Expand Up @@ -159,11 +171,14 @@ async fn test_starts_with() {
let query = format!("SELECT * FROM person {}", sql);
let rows = client.query(query.as_str(), &[]).await.unwrap();

let rows: Vec<(String, i32)> = rows.iter().map(|row| {
let name: String = row.get("name");
let age: i32 = row.get("age");
(name, age)
}).collect();
let rows: Vec<(String, i32)> = rows
.iter()
.map(|row| {
let name: String = row.get("name");
let age: i32 = row.get("age");
(name, age)
})
.collect();

let expected_rows = vec![
("name19".to_string(), 19),
Expand Down
16 changes: 10 additions & 6 deletions tests/integration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use testcontainers_modules::postgres::Postgres;
use testcontainers_modules::testcontainers::ContainerAsync;
use testcontainers_modules::testcontainers::core::error;
use testcontainers_modules::testcontainers::runners::AsyncRunner;
use testcontainers_modules::testcontainers::ContainerAsync;
use tokio::sync::OnceCell;
use tokio_postgres::{Client, NoTls};

Expand All @@ -11,10 +11,14 @@ async fn get_client() -> Client {
let container = get_container().await.as_ref().unwrap();
let port = container.get_host_port_ipv4(5432).await.unwrap();
let host = container.get_host().await.unwrap();
let connection_string = format!("host={} user=postgres password=postgres port={}", host, port);
let connection_string = format!(
"host={} user=postgres password=postgres port={}",
host, port
);

let (client, connection) =
tokio_postgres::connect(connection_string.as_str(), NoTls).await.unwrap();
let (client, connection) = tokio_postgres::connect(connection_string.as_str(), NoTls)
.await
.unwrap();

tokio::spawn(async move {
if let Err(e) = connection.await {
Expand All @@ -26,7 +30,8 @@ async fn get_client() -> Client {

pub async fn get_container() -> &'static error::Result<ContainerAsync<Postgres>> {
static ONCE: OnceCell<error::Result<ContainerAsync<Postgres>>> = OnceCell::const_new();
ONCE.get_or_init(|| async { Postgres::default().start().await }).await
ONCE.get_or_init(|| async { Postgres::default().start().await })
.await
}

pub async fn setup_data() -> &'static bool {
Expand Down Expand Up @@ -77,4 +82,3 @@ async fn create_rows() {
.unwrap();
}
}

2 changes: 1 addition & 1 deletion tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod integration;
pub mod unit;
pub mod integration;
28 changes: 19 additions & 9 deletions tests/unit/combined_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use pg_filters::filtering::FilterColumn;
use pg_filters::{
filtering::{ConditionalOperator, FilteringRule, FilterOperator}, FilteringOptions, PaginationOptions, PgFilters, sorting::{SortedColumn, SortOrder}
filtering::{ConditionalOperator, FilterOperator, FilteringRule},
sorting::{SortOrder, SortedColumn},
FilteringOptions, PaginationOptions, PgFilters,
};
use pg_filters::filtering::FilterColumn;

#[test]
fn test_filtering_with_sorting_with_pagination() {
Expand All @@ -27,7 +29,7 @@ fn test_filtering_with_sorting_with_pagination() {
filter_operator: FilterOperator::GreaterThan,
conditional_operator: ConditionalOperator::Or,
}),
]))
])),
);

let sql = filters.sql();
Expand Down Expand Up @@ -61,7 +63,7 @@ fn test_filtering_with_case_sensitive() {
filter_operator: FilterOperator::GreaterThan,
conditional_operator: ConditionalOperator::Or,
}),
]))
])),
);

let sql = filters.sql();
Expand All @@ -71,7 +73,6 @@ fn test_filtering_with_case_sensitive() {
);
}


#[test]
fn test_filtering_without_sorting_with_pagination() {
let filters = PgFilters::new(
Expand All @@ -97,7 +98,10 @@ fn test_filtering_without_sorting_with_pagination() {
);

let sql = filters.sql();
assert_eq!(sql, " WHERE LOWER(name) = LOWER('John') OR age > 18 LIMIT 10 OFFSET 0");
assert_eq!(
sql,
" WHERE LOWER(name) = LOWER('John') OR age > 18 LIMIT 10 OFFSET 0"
);
}

#[test]
Expand All @@ -123,7 +127,10 @@ fn test_filtering_with_sorting_without_pagination() {
);

let sql = filters.sql();
assert_eq!(sql, " WHERE LOWER(name) = LOWER('John') OR age > 18 ORDER BY name ASC");
assert_eq!(
sql,
" WHERE LOWER(name) = LOWER('John') OR age > 18 ORDER BY name ASC"
);
}

#[test]
Expand Down Expand Up @@ -162,7 +169,7 @@ fn test_filtering_with_sorting_with_pagination_with_empty_filters() {
column: "name".to_string(),
order: SortOrder::Asc,
}],
Some(FilteringOptions::new(vec![]))
Some(FilteringOptions::new(vec![])),
);

let sql = filters.sql();
Expand Down Expand Up @@ -234,7 +241,10 @@ fn test_filtering_with_sorting_with_pagination_with_empty_sorting() {
);

let sql = filters.sql();
assert_eq!(sql, " WHERE LOWER(name) = LOWER('John') OR age > 18 LIMIT 10 OFFSET 0");
assert_eq!(
sql,
" WHERE LOWER(name) = LOWER('John') OR age > 18 LIMIT 10 OFFSET 0"
);
}

#[test]
Expand Down
Loading

0 comments on commit 52eff6e

Please sign in to comment.