Skip to content

Commit

Permalink
Add new operator for case insensitivity (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsangmeister committed May 20, 2022
1 parent eacfa31 commit 8bf3073
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion datastore/shared/postgresql_backend/sql_query_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ def build_filter_str(
condition = f"{table_alias}data->>%s {operator} NULL"
arguments += [filter.field]
else:
condition = f"{table_alias}data->>%s {filter.operator} %s::text"
if filter.operator == "~=":
condition = f"LOWER({table_alias}data->>%s) = LOWER(%s::text)"
else:
condition = f"{table_alias}data->>%s {filter.operator} %s::text"
arguments += [filter.field, filter.value]
return condition
else:
Expand Down
7 changes: 5 additions & 2 deletions datastore/shared/util/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@dataclass
class FilterOperator(SelfValidatingDataclass):
field: Field
operator: Literal["=", "!=", "<", ">", ">=", "<="]
operator: Literal["=", "!=", "<", ">", ">=", "<=", "~="]
value: Any


Expand Down Expand Up @@ -45,7 +45,10 @@ class Or:
"properties": {
"field": {"type": "string"},
"value": {},
"operator": {"type": "string", "enum": ["=", "!=", "<", ">", ">=", "<="]},
"operator": {
"type": "string",
"enum": ["=", "!=", "<", ">", ">=", "<=", "~="],
},
},
"required": ["field", "value", "operator"],
},
Expand Down
13 changes: 13 additions & 0 deletions tests/reader/system/filter/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def test_leq(json_client, db_connection, db_cur):
}


def test_eq_ignore_case(json_client, db_connection, db_cur):
setup_data(db_connection, db_cur, data)
response = json_client.post(
Route.FILTER.URL,
{
"collection": "a",
"filter": {"field": "field_1", "operator": "~=", "value": "DATA"},
},
)
assert_success_response(response)
assert response.json == {"data": {"1": data["a/1"]}, "position": 3}


def test_and(json_client, db_connection, db_cur):
setup_data(db_connection, db_cur, data)
response = json_client.post(
Expand Down

0 comments on commit 8bf3073

Please sign in to comment.