Skip to content

Commit

Permalink
fix(get): Fix from_id cache-misses (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ketiltrout authored Oct 18, 2023
2 parents c58ab2d + 035643d commit 5006d22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
18 changes: 9 additions & 9 deletions chimedb/dataset/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ def from_id(cls, state_id, load_data=True):
try:
_logger.debug(f"Loading state {state_id} from database...")
if load_data:
_state_cache[state_id] = DatasetState.get(
DatasetState.id == state_id
)
_state_cache[state_id] = DatasetState.get_by_id(state_id)
else:
_state_cache[state_id] = DatasetState.select(
DatasetState.type, DatasetState.time
).get()
except orm.DatasetState.DoesNotExist:
_state_cache[state_id] = (
DatasetState.select(DatasetState.type, DatasetState.time)
.where(DatasetState.id == state_id)
.get()
)
except DatasetState.DoesNotExist:
_logger.warning(f"Could not find state {state_id}.")
return None
return _state_cache[state_id]
Expand Down Expand Up @@ -191,8 +191,8 @@ def from_id(cls, ds_id):
)
if ds_id not in _dataset_cache:
try:
_dataset_cache[ds_id] = Dataset.get(Dataset.id == ds_id)
except orm.Dataset.DoesNotExist:
_dataset_cache[ds_id] = Dataset.get_by_id(ds_id)
except Dataset.DoesNotExist:
_logger.warning(f"Could not find dataset {ds_id}.")
return None
return _dataset_cache[ds_id]
Expand Down
14 changes: 14 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,17 @@ def tests():
# pre-fetch
dget.index()
tests()

def test_get_missing(self):
# These don't exist
assert dget.Dataset.from_id("42") is None
assert dget.DatasetState.from_id("42") is None

# But these do
ds = dget.Dataset.from_id("1337")
assert ds is not None
assert ds.id == "1337"

state = dget.DatasetState.from_id("23")
assert state is not None
assert state.id == "23"

0 comments on commit 5006d22

Please sign in to comment.