Skip to content

Commit fdd9ab2

Browse files
author
Steve Canny
committed
add PartFactory.__new__()
1 parent b074ad6 commit fdd9ab2

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

opc/package.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@ def open(pkg_file):
3232
return pkg
3333

3434

35+
class Part(object):
36+
"""
37+
Base class for package parts. Provides common properties and methods, but
38+
intended to be subclassed in client code to implement specific part
39+
behaviors.
40+
"""
41+
42+
3543
class PartFactory(object):
3644
"""
3745
Provides a way for client code to specify a subclass of |Part| to be
3846
constructed by |Unmarshaller| based on its content type.
3947
"""
48+
def __new__(cls, partname, content_type, blob):
49+
return Part(partname, content_type, blob)
4050

4151

4252
class Unmarshaller(object):

tests/test_package.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from mock import call, Mock
1515

16-
from opc.package import OpcPackage, Unmarshaller
16+
from opc.package import OpcPackage, PartFactory, Unmarshaller
1717

1818
from .unitutil import class_mock, method_mock
1919

@@ -46,6 +46,25 @@ def it_can_open_a_pkg_file(self, PackageReader_, PartFactory_,
4646
assert isinstance(pkg, OpcPackage)
4747

4848

49+
class DescribePartFactory(object):
50+
51+
@pytest.fixture
52+
def Part_(self, request):
53+
return class_mock('opc.package.Part', request)
54+
55+
def it_constructs_a_part_instance(self, Part_):
56+
# mockery ----------------------
57+
partname, content_type, blob = (
58+
Mock(name='partname'), Mock(name='content_type'),
59+
Mock(name='blob')
60+
)
61+
# exercise ---------------------
62+
part = PartFactory(partname, content_type, blob)
63+
# verify -----------------------
64+
Part_.assert_called_once_with(partname, content_type, blob)
65+
assert part == Part_.return_value
66+
67+
4968
class DescribeUnmarshaller(object):
5069

5170
@pytest.fixture

0 commit comments

Comments
 (0)