|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Tests for bloom filter utility functions.""" |
| 18 | + |
| 19 | +from unittest.mock import MagicMock |
| 20 | + |
| 21 | +from pyiceberg.table.bloom_filter import bloom_filter_might_contain, get_parquet_bloom_filter_for_column |
| 22 | + |
| 23 | + |
| 24 | +class TestBloomFilterUtilities: |
| 25 | + """Test cases for Parquet bloom filter reading utilities.""" |
| 26 | + |
| 27 | + def test_get_parquet_bloom_filter_returns_none_when_not_available(self) -> None: |
| 28 | + """Test that getting a bloom filter returns None when not available.""" |
| 29 | + # Mock a ParquetFile without bloom filters |
| 30 | + mock_parquet_file = MagicMock() |
| 31 | + mock_row_group = MagicMock() |
| 32 | + mock_column = MagicMock() |
| 33 | + mock_column.path_in_schema = "test_column" |
| 34 | + del mock_column.bloom_filter # Ensure bloom_filter attribute doesn't exist |
| 35 | + |
| 36 | + mock_row_group.num_columns = 1 |
| 37 | + mock_row_group.column.return_value = mock_column |
| 38 | + mock_parquet_file.metadata.row_group.return_value = mock_row_group |
| 39 | + |
| 40 | + result = get_parquet_bloom_filter_for_column(mock_parquet_file, "test_column", 0) |
| 41 | + assert result is None |
| 42 | + |
| 43 | + def test_bloom_filter_might_contain_returns_true_when_filter_is_none(self) -> None: |
| 44 | + """Test that might_contain returns True conservatively when filter is None.""" |
| 45 | + result = bloom_filter_might_contain(None, "test_value") |
| 46 | + assert result is True |
| 47 | + |
| 48 | + def test_bloom_filter_might_contain_returns_true_when_value_is_none(self) -> None: |
| 49 | + """Test that might_contain returns True conservatively when value is None.""" |
| 50 | + mock_filter = MagicMock() |
| 51 | + result = bloom_filter_might_contain(mock_filter, None) |
| 52 | + assert result is True |
| 53 | + |
| 54 | + def test_bloom_filter_might_contain_uses_check_method(self) -> None: |
| 55 | + """Test that might_contain uses the check method if available.""" |
| 56 | + mock_filter = MagicMock() |
| 57 | + mock_filter.check.return_value = True |
| 58 | + |
| 59 | + result = bloom_filter_might_contain(mock_filter, "test_value") |
| 60 | + assert result is True |
| 61 | + mock_filter.check.assert_called_once_with("test_value") |
| 62 | + |
| 63 | + def test_bloom_filter_might_contain_uses_contains_method(self) -> None: |
| 64 | + """Test that might_contain uses __contains__ if check is not available.""" |
| 65 | + mock_filter = MagicMock() |
| 66 | + del mock_filter.check # Remove check method |
| 67 | + mock_filter.__contains__.return_value = True |
| 68 | + |
| 69 | + result = bloom_filter_might_contain(mock_filter, "test_value") |
| 70 | + assert result is True |
| 71 | + |
| 72 | + def test_bloom_filter_might_contain_returns_true_on_exception(self) -> None: |
| 73 | + """Test that might_contain returns True conservatively on exception.""" |
| 74 | + mock_filter = MagicMock() |
| 75 | + mock_filter.check.side_effect = Exception("Test error") |
| 76 | + |
| 77 | + result = bloom_filter_might_contain(mock_filter, "test_value") |
| 78 | + assert result is True |
0 commit comments