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: missing keys should not cause failure in backend #234

Merged
merged 1 commit into from
Mar 4, 2022
Merged
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
17 changes: 12 additions & 5 deletions happi/backends/json_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
fcntl = None


# A sentinel for keys that are missing for comparisons below.
_MISSING = object()


@contextlib.contextmanager
def _load_and_store_context(backend):
"""Context manager used to load, and optionally store the JSON database."""
Expand Down Expand Up @@ -164,8 +168,10 @@ def find(self, to_match):
"""

def comparison(name, doc):
return all(value == doc[key]
for key, value in to_match.items())
return all(
value == doc.get(key, _MISSING)
for key, value in to_match.items()
)

yield from self._iterative_compare(comparison)

Expand All @@ -187,12 +193,13 @@ def find_range(self, key, *, start, stop=None, to_match):
"""

def comparison(name, doc):
if all(value == doc[k] for k, value in to_match.items()):
value = doc.get(key)
if all(value == doc.get(k, _MISSING)
for k, value in to_match.items()):
try:
return start <= value < stop
return start <= doc[key] < stop
except Exception:
...
return False

if key in to_match:
raise ValueError('Cannot specify the same key in `to_match` as '
Expand Down