From 2e5231ea4f0df39ff56044109fe6b06ebb551cf9 Mon Sep 17 00:00:00 2001 From: Justin Sievenpiper Date: Thu, 14 Dec 2023 22:23:12 -0800 Subject: [PATCH] fix like and not like queries for strings --- examples/postgres/tests/mutation_tests.rs | 4 +- examples/postgres/tests/query_tests.rs | 70 +++++++++++++++++++++-- src/builder_context/filter_types_map.rs | 32 +++++++++-- 3 files changed, 95 insertions(+), 11 deletions(-) diff --git a/examples/postgres/tests/mutation_tests.rs b/examples/postgres/tests/mutation_tests.rs index 87931b28..690197ae 100644 --- a/examples/postgres/tests/mutation_tests.rs +++ b/examples/postgres/tests/mutation_tests.rs @@ -5,9 +5,7 @@ pub async fn get_schema() -> Schema { let database = Database::connect("postgres://sea:sea@127.0.0.1/sakila") .await .unwrap(); - let schema = - seaography_postgres_example::query_root::schema(database, None, None) - .unwrap(); + let schema = seaography_postgres_example::query_root::schema(database, None, None).unwrap(); schema } diff --git a/examples/postgres/tests/query_tests.rs b/examples/postgres/tests/query_tests.rs index 2b120bd0..9deaa09b 100644 --- a/examples/postgres/tests/query_tests.rs +++ b/examples/postgres/tests/query_tests.rs @@ -5,9 +5,7 @@ pub async fn get_schema() -> Schema { let database = Database::connect("postgres://sea:sea@127.0.0.1/sakila") .await .unwrap(); - let schema = - seaography_postgres_example::query_root::schema(database, None, None) - .unwrap(); + let schema = seaography_postgres_example::query_root::schema(database, None, None).unwrap(); schema } @@ -918,4 +916,68 @@ async fn test_boolean_field() { } "#, ) -} \ No newline at end of file +} + +#[tokio::test] +async fn test_like_filter() { + let schema = get_schema().await; + + assert_eq( + schema + .execute( + r#" + { + country(filters: { country: { like: "Au%" } }) { + nodes { + country + } + } + } + "#, + ) + .await, + r#" + { + "country": { + "nodes": [ + { + "country": "Australia" + }, + { + "country":"Austria" + } + ] + } + }"#, + ) +} + +#[tokio::test] +async fn test_not_like_filter() { + let schema = get_schema().await; + + assert_eq( + schema + .execute( + r#" + { + country(filters: { country: { not_like: "A%"}}, pagination: { page: { limit: 1, page: 0}}) { + nodes { + country + } + } + } + "# + ).await, + r#" + { + "country": { + "nodes": [ + { + "country": "Bahrain" + } + ] + } + }"# + ) +} diff --git a/src/builder_context/filter_types_map.rs b/src/builder_context/filter_types_map.rs index 527f3175..0391b52a 100644 --- a/src/builder_context/filter_types_map.rs +++ b/src/builder_context/filter_types_map.rs @@ -528,28 +528,52 @@ impl FilterTypesMapHelper { if let Some(value) = filter.get("starts_with") { let value = types_map_helper .async_graphql_value_to_sea_orm_value::(column, &value)?; - condition = condition.add(column.starts_with(&value.to_string())); + + let value: String = match value { + sea_orm::Value::String(Some(inner)) => *inner, + _ => value.to_string(), + }; + + condition = condition.add(column.starts_with(value)); } } FilterOperation::EndsWith => { if let Some(value) = filter.get("ends_with") { let value = types_map_helper .async_graphql_value_to_sea_orm_value::(column, &value)?; - condition = condition.add(column.ends_with(&value.to_string())); + + let value: String = match value { + sea_orm::Value::String(Some(inner)) => *inner, + _ => value.to_string(), + }; + + condition = condition.add(column.ends_with(value)); } } FilterOperation::Like => { if let Some(value) = filter.get("like") { let value = types_map_helper .async_graphql_value_to_sea_orm_value::(column, &value)?; - condition = condition.add(column.like(&value.to_string())); + + let value: String = match value { + sea_orm::Value::String(Some(inner)) => *inner, + _ => value.to_string(), + }; + + condition = condition.add(column.like(value)); } } FilterOperation::NotLike => { if let Some(value) = filter.get("not_like") { let value = types_map_helper .async_graphql_value_to_sea_orm_value::(column, &value)?; - condition = condition.add(column.not_like(&value.to_string())); + + let value: String = match value { + sea_orm::Value::String(Some(inner)) => *inner, + _ => value.to_string(), + }; + + condition = condition.add(column.not_like(value)); } } FilterOperation::Between => {