This is an automated email from the ASF dual-hosted git repository.
blue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git
The following commit(s) were added to refs/heads/master by this push:
new 3b5ff3f21a Python: Add more tests (#6504)
3b5ff3f21a is described below
commit 3b5ff3f21ab164fa78f4e1ba67316edf9de46180
Author: Fokko Driesprong <[email protected]>
AuthorDate: Fri Dec 30 19:49:06 2022 +0100
Python: Add more tests (#6504)
---
python/pyiceberg/typedef.py | 11 ++++-----
python/tests/test_typedef.py | 15 ++++++++++++-
python/tests/test_types.py | 15 +++++++++++++
.../{test_typedef.py => utils/test_deprecated.py} | 26 +++++++++++++---------
4 files changed, 49 insertions(+), 18 deletions(-)
diff --git a/python/pyiceberg/typedef.py b/python/pyiceberg/typedef.py
index ccdeacad30..c924d703bd 100644
--- a/python/pyiceberg/typedef.py
+++ b/python/pyiceberg/typedef.py
@@ -54,12 +54,9 @@ class KeyDefaultDict(Dict[K, V]):
self.default_factory = default_factory
def __missing__(self, key: K) -> V:
- if self.default_factory is None:
- raise KeyError(key)
- else:
- val = self.default_factory(key)
- self[key] = val
- return val
+ val = self.default_factory(key)
+ self[key] = val
+ return val
Identifier = Tuple[str, ...]
@@ -100,4 +97,4 @@ class Record(StructProtocol):
return True if isinstance(other, Record) and other._data == self._data
else False
def __repr__(self) -> str:
- return "[" + ", ".join([repr(e) for e in self._data]) + "]"
+ return f"{self.__class__.__name__}[" + ", ".join([repr(e) for e in
self._data]) + "]"
diff --git a/python/tests/test_typedef.py b/python/tests/test_typedef.py
index 01eed9cde0..628e674443 100644
--- a/python/tests/test_typedef.py
+++ b/python/tests/test_typedef.py
@@ -16,7 +16,7 @@
# under the License.
import pytest
-from pyiceberg.typedef import FrozenDict
+from pyiceberg.typedef import FrozenDict, KeyDefaultDict, Record
def test_setitem_frozendict() -> None:
@@ -29,3 +29,16 @@ def test_update_frozendict() -> None:
d = FrozenDict(foo=1, bar=2)
with pytest.raises(AttributeError):
d.update({"yes": 2})
+
+
+def test_keydefaultdict() -> None:
+ def one(_: int) -> int:
+ return 1
+
+ defaultdict = KeyDefaultDict(one)
+ assert defaultdict[22] == 1
+
+
+def test_record_repr() -> None:
+ r = Record(1, "vo", True)
+ assert repr(r) == "Record[1, 'vo', True]"
diff --git a/python/tests/test_types.py b/python/tests/test_types.py
index 4b1d7698db..cda1309ede 100644
--- a/python/tests/test_types.py
+++ b/python/tests/test_types.py
@@ -18,6 +18,7 @@
from typing import Type
import pytest
+from pydantic import ValidationError
from pyiceberg.types import (
BinaryType,
@@ -408,6 +409,13 @@ def test_deserialization_fixed() -> None:
assert len(inner) == 22
+def test_deserialization_fixed_failure() -> None:
+ with pytest.raises(ValidationError) as exc_info:
+ _ = IcebergTestType.parse_raw('"fixed[abc]"')
+
+ assert "Could not match fixed[abc], expected format fixed[22]" in
str(exc_info.value)
+
+
def test_str_fixed() -> None:
assert str(FixedType(22)) == "fixed[22]"
@@ -446,6 +454,13 @@ def test_deserialization_decimal() -> None:
assert inner.scale == 25
+def test_deserialization_decimal_failure() -> None:
+ with pytest.raises(ValidationError) as exc_info:
+ _ = IcebergTestType.parse_raw('"decimal(abc, def)"')
+
+ assert "Could not parse decimal(abc, def) into a DecimalType" in
str(exc_info.value)
+
+
def test_str_decimal() -> None:
assert str(DecimalType(19, 25)) == "decimal(19, 25)"
diff --git a/python/tests/test_typedef.py
b/python/tests/utils/test_deprecated.py
similarity index 58%
copy from python/tests/test_typedef.py
copy to python/tests/utils/test_deprecated.py
index 01eed9cde0..7c44c45859 100644
--- a/python/tests/test_typedef.py
+++ b/python/tests/utils/test_deprecated.py
@@ -14,18 +14,24 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-import pytest
+from unittest.mock import Mock, patch
-from pyiceberg.typedef import FrozenDict
+from pyiceberg.utils.deprecated import deprecated
-def test_setitem_frozendict() -> None:
- d = FrozenDict(foo=1, bar=2)
- with pytest.raises(AttributeError):
- d["foo"] = 3
+@patch("warnings.warn")
+def test_deprecated(warn: Mock) -> None:
+ @deprecated(
+ deprecated_in="0.1.0",
+ removed_in="0.2.0",
+ help_message="Please use load_something_else() instead",
+ )
+ def deprecated_method() -> None:
+ pass
+ deprecated_method()
-def test_update_frozendict() -> None:
- d = FrozenDict(foo=1, bar=2)
- with pytest.raises(AttributeError):
- d.update({"yes": 2})
+ assert warn.called
+ assert warn.call_args[0] == (
+ "Call to deprecated_method, deprecated in 0.1.0, will be removed in
0.2.0. Please use load_something_else() instead.",
+ )