Skip to content

Commit

Permalink
Add test coverage for query expression NULL syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dhirving committed Aug 10, 2024
1 parent ff10942 commit 7aa86be
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions python/lsst/daf/butler/tests/butler_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,8 @@ def test_boolean_columns(self) -> None:
NULL_ID_2 = 903342 # already exists in the YAML file
records = [
{"id": TRUE_ID, "obs_id": "true-1", "can_see_sky": True},
{"id": FALSE_ID_1, "obs_id": "false-1", "can_see_sky": False},
{"id": FALSE_ID_2, "obs_id": "false-2", "can_see_sky": False},
{"id": FALSE_ID_1, "obs_id": "false-1", "can_see_sky": False, "observation_type": "science"},
{"id": FALSE_ID_2, "obs_id": "false-2", "can_see_sky": False, "observation_type": None},
{"id": NULL_ID_1, "obs_id": "null-1", "can_see_sky": None},
]
for record in records:
Expand Down Expand Up @@ -988,18 +988,37 @@ def _run_query(where: str) -> list[int]:
query_func("exposure.can_see_sky OR exposure = 2001"), [TRUE_ID, FALSE_ID_1]
)

# Find nulls and non-nulls. This is run only against the new query
# system. It appears that the `= NULL` syntax never had test coverage
# in the old query system and doesn't appear to work for any column
# types, not just bool. Not worth fixing since we are dropping that
# code soon.
# Find nulls and non-nulls.
#
# This is run only against the new query system. It appears that the
# `= NULL` syntax never had test coverage in the old query system and
# doesn't work for any column types. Not worth fixing since we are
# dropping that code soon.
nulls = [NULL_ID_1, NULL_ID_2]
non_nulls = [TRUE_ID, FALSE_ID_1, FALSE_ID_2]
self.assertCountEqual(_run_query("exposure.can_see_sky = NULL"), nulls)
self.assertCountEqual(_run_query("exposure.can_see_sky != NULL"), non_nulls)
self.assertCountEqual(_run_query("NULL = exposure.can_see_sky"), nulls)
self.assertCountEqual(_run_query("NULL != exposure.can_see_sky"), non_nulls)

# You can't do a NULL check on an arbitrary boolean predicate.
with self.assertRaises(InvalidQueryError):
_run_query("NULL = (exposure.can_see_sky AND exposure = 2001)")

# Check null finding for non-boolean columns, too.
self.assertEqual(
_run_query("exposure.observation_type = NULL AND NOT exposure.can_see_sky"), [FALSE_ID_2]
)
self.assertEqual(
_run_query("exposure.observation_type != NULL AND NOT exposure.can_see_sky"), [FALSE_ID_1]
)
self.assertEqual(
_run_query("NULL = exposure.observation_type AND NOT exposure.can_see_sky"), [FALSE_ID_2]
)
self.assertEqual(
_run_query("NULL != exposure.observation_type AND NOT exposure.can_see_sky"), [FALSE_ID_1]
)

# Test boolean columns in ExpressionFactory.
with butler._query() as query:
x = query.expression_factory
Expand Down

0 comments on commit 7aa86be

Please sign in to comment.