aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pyrsistent/py3/tests/checked_set_test.py
diff options
context:
space:
mode:
authorshmel1k <shmel1k@ydb.tech>2023-11-26 18:16:14 +0300
committershmel1k <shmel1k@ydb.tech>2023-11-26 18:43:30 +0300
commitb8cf9e88f4c5c64d9406af533d8948deb050d695 (patch)
tree218eb61fb3c3b96ec08b4d8cdfef383104a87d63 /contrib/python/pyrsistent/py3/tests/checked_set_test.py
parent523f645a83a0ec97a0332dbc3863bb354c92a328 (diff)
downloadydb-b8cf9e88f4c5c64d9406af533d8948deb050d695.tar.gz
add kikimr_configure
Diffstat (limited to 'contrib/python/pyrsistent/py3/tests/checked_set_test.py')
-rw-r--r--contrib/python/pyrsistent/py3/tests/checked_set_test.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/contrib/python/pyrsistent/py3/tests/checked_set_test.py b/contrib/python/pyrsistent/py3/tests/checked_set_test.py
new file mode 100644
index 0000000000..f0be4963e2
--- /dev/null
+++ b/contrib/python/pyrsistent/py3/tests/checked_set_test.py
@@ -0,0 +1,85 @@
+import pickle
+import pytest
+from pyrsistent import CheckedPSet, PSet, InvariantException, CheckedType, CheckedPVector, CheckedValueTypeError
+
+
+class Naturals(CheckedPSet):
+ __type__ = int
+ __invariant__ = lambda value: (value >= 0, 'Negative value')
+
+def test_instantiate():
+ x = Naturals([1, 2, 3, 3])
+
+ assert list(x) == [1, 2, 3]
+ assert isinstance(x, Naturals)
+ assert isinstance(x, PSet)
+ assert isinstance(x, CheckedType)
+
+def test_add():
+ x = Naturals()
+ x2 = x.add(1)
+
+ assert list(x2) == [1]
+ assert isinstance(x2, Naturals)
+
+def test_invalid_type():
+ with pytest.raises(CheckedValueTypeError):
+ Naturals([1, 2.0])
+
+def test_breaking_invariant():
+ try:
+ Naturals([1, -1])
+ assert False
+ except InvariantException as e:
+ assert e.invariant_errors == ('Negative value',)
+
+def test_repr():
+ x = Naturals([1, 2])
+
+ assert str(x) == 'Naturals([1, 2])'
+
+def test_default_serialization():
+ x = Naturals([1, 2])
+
+ assert x.serialize() == set([1, 2])
+
+class StringNaturals(Naturals):
+ @staticmethod
+ def __serializer__(format, value):
+ return format.format(value)
+
+def test_custom_serialization():
+ x = StringNaturals([1, 2])
+
+ assert x.serialize("{0}") == set(["1", "2"])
+
+class NaturalsVector(CheckedPVector):
+ __type__ = Naturals
+
+def test_multi_level_serialization():
+ x = NaturalsVector.create([[1, 2], [3, 4]])
+
+ assert str(x) == "NaturalsVector([Naturals([1, 2]), Naturals([3, 4])])"
+
+ sx = x.serialize()
+ assert sx == [set([1, 2]), set([3, 4])]
+ assert isinstance(sx[0], set)
+
+def test_create():
+ assert Naturals.create([1, 2]) == Naturals([1, 2])
+
+def test_evolver_returns_same_instance_when_no_updates():
+ x = Naturals([1, 2])
+ assert x.evolver().persistent() is x
+
+def test_pickling():
+ x = Naturals([1, 2])
+ y = pickle.loads(pickle.dumps(x, -1))
+
+ assert x == y
+ assert isinstance(y, Naturals)
+
+
+def test_supports_weakref():
+ import weakref
+ weakref.ref(Naturals([1, 2])) \ No newline at end of file