|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# test_pkgwriter.py |
| 4 | +# |
| 5 | +# Copyright (C) 2013 Steve Canny scanny@cisco.com |
| 6 | +# |
| 7 | +# This module is part of python-pptx and is released under the MIT License: |
| 8 | +# http://www.opensource.org/licenses/mit-license.php |
| 9 | + |
| 10 | +"""Test suite for opc.pkgwriter module.""" |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from mock import call, Mock, patch |
| 15 | + |
| 16 | +from opc.pkgwriter import PackageWriter |
| 17 | + |
| 18 | + |
| 19 | +class DescribePackageWriter(object): |
| 20 | + |
| 21 | + @pytest.fixture |
| 22 | + def PhysPkgWriter_(self, request): |
| 23 | + _patch = patch('opc.pkgwriter.PhysPkgWriter') |
| 24 | + request.addfinalizer(_patch.stop) |
| 25 | + return _patch.start() |
| 26 | + |
| 27 | + @pytest.fixture |
| 28 | + def _write_methods(self, request): |
| 29 | + """Mock that patches all the _write_* methods of PackageWriter""" |
| 30 | + root_mock = Mock(name='PackageWriter') |
| 31 | + patch1 = patch.object(PackageWriter, '_write_content_types_stream') |
| 32 | + patch2 = patch.object(PackageWriter, '_write_pkg_rels') |
| 33 | + patch3 = patch.object(PackageWriter, '_write_parts') |
| 34 | + root_mock.attach_mock(patch1.start(), '_write_content_types_stream') |
| 35 | + root_mock.attach_mock(patch2.start(), '_write_pkg_rels') |
| 36 | + root_mock.attach_mock(patch3.start(), '_write_parts') |
| 37 | + |
| 38 | + def fin(): |
| 39 | + patch1.stop() |
| 40 | + patch2.stop() |
| 41 | + patch3.stop() |
| 42 | + |
| 43 | + request.addfinalizer(fin) |
| 44 | + return root_mock |
| 45 | + |
| 46 | + def it_can_write_a_package(self, PhysPkgWriter_, _write_methods): |
| 47 | + # mockery ---------------------- |
| 48 | + pkg_file = Mock(name='pkg_file') |
| 49 | + pkg_rels = Mock(name='pkg_rels') |
| 50 | + parts = Mock(name='parts') |
| 51 | + phys_writer = PhysPkgWriter_.return_value |
| 52 | + # exercise --------------------- |
| 53 | + PackageWriter.write(pkg_file, pkg_rels, parts) |
| 54 | + # verify ----------------------- |
| 55 | + expected_calls = [ |
| 56 | + call._write_content_types_stream(phys_writer, parts), |
| 57 | + call._write_pkg_rels(phys_writer, pkg_rels), |
| 58 | + call._write_parts(phys_writer, parts), |
| 59 | + ] |
| 60 | + PhysPkgWriter_.assert_called_once_with(pkg_file) |
| 61 | + assert _write_methods.mock_calls == expected_calls |
| 62 | + phys_writer.close.assert_called_once_with() |
0 commit comments