Skip to content

Commit

Permalink
Merge pull request #54 from bento-platform/develop
Browse files Browse the repository at this point in the history
Version 2.1.0
  • Loading branch information
davidlougheed authored Mar 29, 2021
2 parents c5319fc + df110a4 commit 1090099
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 14 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Install dependencies
run: pip install -r requirements.txt
- uses: actions/setup-python@v2
name: Set up Python
with:
python-version: 3.9
- name: Install flake8
run: python -m pip install flake8
- name: Run linter
run: flake8 ./bento_lib ./tests
15 changes: 12 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- master
- develop
pull_request:
jobs:
test:
Expand All @@ -22,10 +23,14 @@ jobs:
--health-retries 5
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v2
name: Set up Python
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install -r requirements.txt
run: python -m pip install -r requirements.txt
- name: Test
run: pytest -svv --cov=bento_lib --cov-branch
run: mkdir -p tmp && pytest -svv --cov=bento_lib --cov-branch
env:
TEST_REDIS_HOST: localhost
TEST_REDIS_PORT: 6379
Expand All @@ -38,5 +43,9 @@ jobs:
python-version: [ 3.6, 3.9 ]
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v2
name: Set up Python
with:
python-version: ${{ matrix.python-version }}
- name: Install bento_lib
run: pip install .[flask,django]
run: python -m pip install .
2 changes: 1 addition & 1 deletion bento_lib/package.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = bento_lib
version = 2.0.0
version = 2.1.0
authors = David Lougheed
author_emails = [email protected]
12 changes: 12 additions & 0 deletions bento_lib/search/data_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
ArrayLengthData = Tuple[str, int, Tuple["ArrayLengthData", ...]]


def _icontains(lhs: str, rhs: str):
"""
Same as the "contains" operator, except with case-folded (i.e. case
insensitive) arguments.
:param lhs: The LHS for the operation.
:param rhs: The RHS for the operation.
:return: The result of the icontains operation.
"""
return contains(lhs.casefold(), rhs.casefold())


def _validate_data_structure_against_schema(data_structure: QueryableStructure, schema: JSONSchema):
"""
Validates a queryable data structure of some type against a JSON schema. This is an important validation step,
Expand Down Expand Up @@ -434,6 +445,7 @@ def _resolve(resolve: Tuple[q.Literal, ...], resolving_ds: QueryableStructure, _
q.FUNCTION_GE: _binary_op(ge),

q.FUNCTION_CO: _binary_op(contains),
q.FUNCTION_ICO: _binary_op(_icontains),

q.FUNCTION_RESOLVE: _resolve
}
3 changes: 3 additions & 0 deletions bento_lib/search/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"SEARCH_OP_GE",

"SEARCH_OP_CO",
"SEARCH_OP_ICO",

"SEARCH_OPERATIONS",
]
Expand All @@ -18,6 +19,7 @@
SEARCH_OP_GE = "ge"

SEARCH_OP_CO = "co"
SEARCH_OP_ICO = "ico"

SEARCH_OPERATIONS = (
SEARCH_OP_EQ,
Expand All @@ -26,4 +28,5 @@
SEARCH_OP_GT,
SEARCH_OP_GE,
SEARCH_OP_CO,
SEARCH_OP_ICO,
)
13 changes: 10 additions & 3 deletions bento_lib/search/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,17 @@ def _resolve(args: q.Args, params: tuple, schema: JSONSchema, _internal: bool =
sql.Identifier(f_id) if f_id is not None else sql.SQL("*")), params


def _contains(args: q.Args, params: tuple, schema: JSONSchema, internal: bool = False) -> SQLComposableWithParams:
# TODO rename the function ?
def _contains(op: str, args: q.Args, params: tuple, schema: JSONSchema, internal: bool = False, )\
-> SQLComposableWithParams:
lhs_sql, lhs_params = search_ast_to_psycopg2_expr(args[0], params, schema, internal)
rhs_sql, rhs_params = search_ast_to_psycopg2_expr(q.Expression(fn=q.FUNCTION_HELPER_WC, args=[args[1]]), params,
schema, internal)
return sql.SQL("({}) LIKE ({})").format(lhs_sql, rhs_sql), params + lhs_params + rhs_params
return sql.SQL("({}) {} ({})").format(lhs_sql, sql.SQL(op), rhs_sql), params + lhs_params + rhs_params


def _contains_op(op) -> Callable[[q.Args, tuple, JSONSchema, bool], SQLComposableWithParams]:
return lambda args, params, schema, internal: _contains(op, args, params, schema, internal)


POSTGRES_SEARCH_LANGUAGE_FUNCTIONS: Dict[
Expand All @@ -350,7 +356,8 @@ def _contains(args: q.Args, params: tuple, schema: JSONSchema, internal: bool =
q.FUNCTION_GT: _binary_op(">"),
q.FUNCTION_GE: _binary_op(">="),

q.FUNCTION_CO: _contains,
q.FUNCTION_CO: _contains_op("LIKE"),
q.FUNCTION_ICO: _contains_op("ILIKE"),

q.FUNCTION_RESOLVE: _resolve,

Expand Down
8 changes: 7 additions & 1 deletion bento_lib/search/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
SEARCH_OP_EQ,
SEARCH_OP_GT,
SEARCH_OP_GE,
SEARCH_OP_CO
SEARCH_OP_CO,
SEARCH_OP_ICO
)


Expand All @@ -21,6 +22,7 @@
"FUNCTION_GT",
"FUNCTION_GE",
"FUNCTION_CO",
"FUNCTION_ICO",
"FUNCTION_RESOLVE",
"FUNCTION_HELPER_WC",

Expand Down Expand Up @@ -56,6 +58,7 @@
FUNCTION_GE = "#ge"

FUNCTION_CO = "#co"
FUNCTION_ICO = "#ico"

FUNCTION_RESOLVE = "#resolve"

Expand All @@ -71,6 +74,7 @@
FUNCTION_GT,
FUNCTION_GE,
FUNCTION_CO,
FUNCTION_ICO,
FUNCTION_RESOLVE,
FUNCTION_HELPER_WC,
)
Expand All @@ -87,6 +91,7 @@
FUNCTION_GT: BINARY_RANGE,
FUNCTION_GE: BINARY_RANGE,
FUNCTION_CO: BINARY_RANGE,
FUNCTION_ICO: BINARY_RANGE,
FUNCTION_RESOLVE: (0, None),
FUNCTION_HELPER_WC: (1, 1),
}
Expand All @@ -100,6 +105,7 @@
FUNCTION_GE: SEARCH_OP_GE,

FUNCTION_CO: SEARCH_OP_CO,
FUNCTION_ICO: SEARCH_OP_ICO,
}


Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ filelock==3.0.12
flake8==3.9.0
Flask==1.1.2
idna==2.10
importlib-metadata==3.7.3
importlib-metadata==3.9.1
iniconfig==1.1.1
itsdangerous==1.1.0
Jinja2==2.11.3
Expand All @@ -26,7 +26,7 @@ pluggy==0.13.1
psycopg2-binary==2.8.6
py==1.10.0
pycodestyle==2.7.0
pyflakes==2.3.0
pyflakes==2.3.1
pyparsing==2.4.7
pyrsistent==0.17.3
pytest==6.2.2
Expand All @@ -36,7 +36,7 @@ python-dateutil==2.8.1
pytz==2021.1
redis==3.5.3
requests==2.25.1
responses==0.13.1
responses==0.13.2
six==1.15.0
sqlparse==0.4.1
toml==0.10.2
Expand Down
14 changes: 13 additions & 1 deletion tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@
"queryable": "all"
}
},
"label": {"type": "string"}
"label": {
"type": "string",
"search": {
"operations": [operations.SEARCH_OP_ICO],
"queryable": "all"
}
}
},
"search": {
"database": {
Expand Down Expand Up @@ -390,6 +396,7 @@
[queries.FUNCTION_GT, 5, 6],
[queries.FUNCTION_GE, 5, 6],
[queries.FUNCTION_CO, "hello", "h"],
[queries.FUNCTION_ICO, "LABEL", "label"],
[queries.FUNCTION_RESOLVE, "biosamples", "[item]", "procedure", "code", "id"],
)

Expand All @@ -411,6 +418,7 @@
[queries.FUNCTION_GT, 5],
[queries.FUNCTION_GE, 5],
[queries.FUNCTION_CO, "hello"],
[queries.FUNCTION_ICO, "LABEL"],
*TEST_INVALID_FUNCTIONS
)

Expand Down Expand Up @@ -474,6 +482,8 @@
["#eq", ["#resolve", "test_op_3", "[item]", "[item]"], 8]
]

TEST_QUERY_24 = ["#ico", ["#resolve", "biosamples", "[item]", "procedure", "code", "label"], "label"]

TEST_LARGE_QUERY_1 = [
"#and",
["#eq",
Expand Down Expand Up @@ -625,6 +635,7 @@
(TEST_QUERY_21, False, True, 27, 1), # Accessing 3 elements in test_op_2, plus 9 in test_op_3 (non-flattened)
(TEST_QUERY_22, False, True, 9, 9), # Accessing 9 in test_op_3 and checking them against itself
(TEST_QUERY_23, False, True, 27, 1), # test_op_3: 9, test_op_1: 3
(TEST_QUERY_24, False, True, 2, 2),
)

# Query, Internal, Exception
Expand Down Expand Up @@ -679,6 +690,7 @@
(TEST_QUERY_21, False, ()),
(TEST_QUERY_22, False, ()),
(TEST_QUERY_23, False, (6, 8)),
(TEST_QUERY_24, True, ("%label%",)),
)

PG_INVALID_EXPRESSIONS = (
Expand Down

0 comments on commit 1090099

Please sign in to comment.