|
11 | 11 | import zstandard |
12 | 12 | from databento.common.dbnstore import DBNStore |
13 | 13 | from databento.common.enums import Schema, SType |
| 14 | +from databento.historical.error import BentoError |
14 | 15 |
|
15 | 16 |
|
16 | 17 | def test_from_file_when_not_exists_raises_expected_exception() -> None: |
@@ -820,11 +821,78 @@ def test_dbnstore_compression_equality( |
820 | 821 | with zstandard by default. |
821 | 822 | """ |
822 | 823 | zstd_stub_data = test_data(schema) |
823 | | - dbn_stub_data = zstandard.ZstdDecompressor().stream_reader(test_data(schema)).read() |
| 824 | + dbn_stub_data = zstandard.ZstdDecompressor().stream_reader(zstd_stub_data).read() |
824 | 825 |
|
825 | 826 | zstd_dbnstore = DBNStore.from_bytes(zstd_stub_data) |
826 | 827 | dbn_dbnstore = DBNStore.from_bytes(dbn_stub_data) |
827 | 828 |
|
828 | 829 | assert len(zstd_dbnstore.to_ndarray()) == len(dbn_dbnstore.to_ndarray()) |
829 | 830 | assert zstd_dbnstore.metadata == dbn_dbnstore.metadata |
830 | 831 | assert zstd_dbnstore.reader.read() == dbn_dbnstore.reader.read() |
| 832 | + |
| 833 | + |
| 834 | +def test_dbnstore_buffer_short( |
| 835 | + test_data: Callable[[Schema], bytes], |
| 836 | + tmp_path: Path, |
| 837 | +) -> None: |
| 838 | + """ |
| 839 | + Test that creating a DBNStore with missing bytes raises a |
| 840 | + BentoError when decoding. |
| 841 | + """ |
| 842 | + # Arrange |
| 843 | + dbn_stub_data = ( |
| 844 | + zstandard.ZstdDecompressor().stream_reader(test_data(Schema.MBO)).read() |
| 845 | + ) |
| 846 | + |
| 847 | + # Act |
| 848 | + dbnstore = DBNStore.from_bytes(data=dbn_stub_data[:-2]) |
| 849 | + |
| 850 | + # Assert |
| 851 | + with pytest.raises(BentoError): |
| 852 | + list(dbnstore) |
| 853 | + |
| 854 | + with pytest.raises(BentoError): |
| 855 | + dbnstore.to_ndarray() |
| 856 | + |
| 857 | + with pytest.raises(BentoError): |
| 858 | + dbnstore.to_df() |
| 859 | + |
| 860 | + with pytest.raises(BentoError): |
| 861 | + dbnstore.to_csv(tmp_path / "test.csv") |
| 862 | + |
| 863 | + with pytest.raises(BentoError): |
| 864 | + dbnstore.to_json(tmp_path / "test.json") |
| 865 | + |
| 866 | + |
| 867 | +def test_dbnstore_buffer_long( |
| 868 | + test_data: Callable[[Schema], bytes], |
| 869 | + tmp_path: Path, |
| 870 | +) -> None: |
| 871 | + """ |
| 872 | + Test that creating a DBNStore with excess bytes raises a |
| 873 | + BentoError when decoding. |
| 874 | + """ |
| 875 | + # Arrange |
| 876 | + dbn_stub_data = ( |
| 877 | + zstandard.ZstdDecompressor().stream_reader(test_data(Schema.MBO)).read() |
| 878 | + ) |
| 879 | + |
| 880 | + # Act |
| 881 | + dbn_stub_data += b"\xF0\xFF" |
| 882 | + dbnstore = DBNStore.from_bytes(data=dbn_stub_data) |
| 883 | + |
| 884 | + # Assert |
| 885 | + with pytest.raises(BentoError): |
| 886 | + list(dbnstore) |
| 887 | + |
| 888 | + with pytest.raises(BentoError): |
| 889 | + dbnstore.to_ndarray() |
| 890 | + |
| 891 | + with pytest.raises(BentoError): |
| 892 | + dbnstore.to_df() |
| 893 | + |
| 894 | + with pytest.raises(BentoError): |
| 895 | + dbnstore.to_csv(tmp_path / "test.csv") |
| 896 | + |
| 897 | + with pytest.raises(BentoError): |
| 898 | + dbnstore.to_json(tmp_path / "test.json") |
0 commit comments