Skip to content

Commit 78836a0

Browse files
committed
test pickling and metaclass
1 parent 71042ec commit 78836a0

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

test_sentinel.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#!/usr/bin/env python3
22
# -*- coding: UTF-8 -*-
33

4+
import pickle
5+
46
import pytest
57

68
import sentinel
79

10+
# Must be defined at module-level due to limitations in how the pickle module works :/
11+
Pickleable = sentinel.create("Pickleable")
12+
813

914
def test_basic_usage():
1015
"""
@@ -88,5 +93,34 @@ def test_always_greater():
8893
assert (AlwaysGreater < (1, ..., ...)) == False
8994

9095

91-
# TODO: test pickling
96+
def test_new_returns_singleton():
97+
"""
98+
Tests that getting the class and calling it returns the same singleton instance.
99+
"""
100+
ExistingSentinel = sentinel.create("ExistingSentinel")
101+
Constructor = type(ExistingSentinel)
102+
assert Constructor() is ExistingSentinel
103+
104+
105+
def test_pickle(tmp_path):
106+
"""
107+
Save a singleton to a pickle and get it back out.
108+
"""
109+
110+
# Just put it everywhere:
111+
arbitrary_data_structure = {Pickleable: (Pickleable, [Pickleable])}
112+
113+
pickle_path = tmp_path / "data.pickle"
114+
with pickle_path.open(mode="wb") as fp:
115+
pickle.dump(arbitrary_data_structure, fp)
116+
117+
with pickle_path.open(mode="rb") as fp:
118+
unpickled = pickle.load(fp)
119+
120+
# Open up the data structure
121+
assert Pickleable in unpickled
122+
assert unpickled[Pickleable][0] is Pickleable
123+
assert unpickled[Pickleable][1][0] is Pickleable
124+
125+
92126
# TODO: test copy() and deepcopy()

0 commit comments

Comments
 (0)