Skip to content

Commit 2d770ad

Browse files
committed
FIX: Fix DBNStore exception on empty data
1 parent 0d4a5d5 commit 2d770ad

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.14.1 - TBD
44
- Fixed issue where `DBNStore.to_df()` would raise an exception if no records were present
5+
- Fixed exception message when creating a DBNStore from an empty data source
56

67
## 0.14.0 - 2023-06-14
78
- Added support for reusing a `Live` client to reconnect

databento/common/dbnstore.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ def __init__(self, source: PathLike[str] | str):
139139
if not self._path.is_file() or not self._path.exists():
140140
raise FileNotFoundError(source)
141141

142+
if self._path.stat().st_size == 0:
143+
raise ValueError(
144+
f"Cannot create data source from empty file: {self._path.name}",
145+
)
146+
142147
self._name = self._path.name
143148
self.__buffer: IO[bytes] | None = None
144149

@@ -308,6 +313,11 @@ class DBNStore:
308313
to_ndarray : np.ndarray
309314
The data as a numpy `ndarray`.
310315
316+
Raises
317+
------
318+
BentoError
319+
When the data_source does not contain valid DBN data or is corrupted.
320+
311321
See Also
312322
--------
313323
https://docs.databento.com/knowledge-base/new-users/dbn-encoding
@@ -330,7 +340,7 @@ def __init__(self, data_source: DataSource) -> None:
330340
buffer = data_source.reader
331341
else:
332342
# We don't know how to read this file
333-
raise RuntimeError(
343+
raise BentoError(
334344
f"Could not determine compression format of {self._data_source.name}",
335345
)
336346

@@ -736,7 +746,9 @@ def from_file(cls, path: PathLike[str] | str) -> DBNStore:
736746
Raises
737747
------
738748
FileNotFoundError
739-
If a empty or non-existant file is specified.
749+
If a non-existant file is specified.
750+
ValueError
751+
If an empty file is specified.
740752
741753
"""
742754
return cls(FileDataSource(path))
@@ -757,8 +769,8 @@ def from_bytes(cls, data: BytesIO | bytes | IO[bytes]) -> DBNStore:
757769
758770
Raises
759771
------
760-
FileNotFoundError
761-
If a empty or non-existant file is specified.
772+
ValueError
773+
If an empty buffer is specified.
762774
763775
"""
764776
return cls(MemoryDataSource(data))

tests/test_historical_bento.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import collections
44
import datetime as dt
55
import sys
6+
from io import BytesIO
67
from pathlib import Path
78
from typing import Callable
89

@@ -29,15 +30,52 @@ def test_from_file_when_not_exists_raises_expected_exception() -> None:
2930
def test_from_file_when_file_empty_raises_expected_exception(
3031
tmp_path: Path,
3132
) -> None:
33+
"""
34+
Test that creating a DBNStore from an empty file raises a ValueError.
35+
"""
3236
# Arrange
3337
path = tmp_path / "test.dbn"
3438
path.touch()
3539

3640
# Act, Assert
37-
with pytest.raises(RuntimeError):
41+
with pytest.raises(ValueError):
3842
DBNStore.from_file(path)
3943

4044

45+
def test_from_file_when_buffer_corrupted_raises_expected_exception(
46+
tmp_path: Path,
47+
) -> None:
48+
"""
49+
Test that creating a DBNStore from an invalid DBN file raises a BentoError.
50+
"""
51+
# Arrange
52+
path = tmp_path / "corrupted.dbn"
53+
path.write_text("this is a test")
54+
55+
# Act, Assert
56+
with pytest.raises(BentoError):
57+
DBNStore.from_file(path)
58+
59+
60+
def test_from_bytes_when_buffer_empty_raises_expected_exception() -> None:
61+
"""
62+
Test that creating a DBNStore from an empty buffer raises a ValueError.
63+
"""
64+
# Arrange, Act, Assert
65+
with pytest.raises(ValueError):
66+
DBNStore.from_bytes(BytesIO())
67+
68+
69+
def test_from_bytes_when_buffer_corrupted_raises_expected_exception() -> None:
70+
"""
71+
Test that creating a DBNStore from an invalid DBN stream raises a
72+
BentoError.
73+
"""
74+
# Arrange, Act, Assert
75+
with pytest.raises(ValueError):
76+
DBNStore.from_bytes(BytesIO())
77+
78+
4179
def test_sources_metadata_returns_expected_json_as_dict(
4280
test_data: Callable[[Schema], bytes],
4381
) -> None:

0 commit comments

Comments
 (0)