Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LIKE and NOT LIKE Queries for Strings #162

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions examples/postgres/tests/mutation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ pub async fn get_schema() -> Schema {
let database = Database::connect("postgres://sea:[email protected]/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
}
Expand Down
70 changes: 66 additions & 4 deletions examples/postgres/tests/query_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ pub async fn get_schema() -> Schema {
let database = Database::connect("postgres://sea:[email protected]/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
}
Expand Down Expand Up @@ -918,4 +916,68 @@ async fn test_boolean_field() {
}
"#,
)
}
}

#[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"
}
]
}
}"#
)
}
32 changes: 28 additions & 4 deletions src/builder_context/filter_types_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>(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::<T>(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::<T>(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::<T>(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 => {
Expand Down
Loading