File: test_api.py

package info (click to toggle)
python-docx 1.1.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,904 kB
  • sloc: xml: 25,311; python: 23,028; makefile: 176
file content (67 lines) | stat: -rw-r--r-- 2,327 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Test suite for the docx.api module."""

import pytest

import docx
from docx.api import Document
from docx.opc.constants import CONTENT_TYPE as CT

from .unitutil.mock import class_mock, function_mock, instance_mock


class DescribeDocument:
    def it_opens_a_docx_file(self, open_fixture):
        docx, Package_, document_ = open_fixture
        document = Document(docx)
        Package_.open.assert_called_once_with(docx)
        assert document is document_

    def it_opens_the_default_docx_if_none_specified(self, default_fixture):
        docx, Package_, document_ = default_fixture
        document = Document()
        Package_.open.assert_called_once_with(docx)
        assert document is document_

    def it_raises_on_not_a_Word_file(self, raise_fixture):
        not_a_docx = raise_fixture
        with pytest.raises(ValueError, match="file 'foobar.xlsx' is not a Word file,"):
            Document(not_a_docx)

    # fixtures -------------------------------------------------------

    @pytest.fixture
    def default_fixture(self, _default_docx_path_, Package_, document_):
        docx = "barfoo.docx"
        _default_docx_path_.return_value = docx
        document_part = Package_.open.return_value.main_document_part
        document_part.document = document_
        document_part.content_type = CT.WML_DOCUMENT_MAIN
        return docx, Package_, document_

    @pytest.fixture
    def open_fixture(self, Package_, document_):
        docx = "foobar.docx"
        document_part = Package_.open.return_value.main_document_part
        document_part.document = document_
        document_part.content_type = CT.WML_DOCUMENT_MAIN
        return docx, Package_, document_

    @pytest.fixture
    def raise_fixture(self, Package_):
        not_a_docx = "foobar.xlsx"
        Package_.open.return_value.main_document_part.content_type = "BOGUS"
        return not_a_docx

    # fixture components ---------------------------------------------

    @pytest.fixture
    def _default_docx_path_(self, request):
        return function_mock(request, "docx.api._default_docx_path")

    @pytest.fixture
    def document_(self, request):
        return instance_mock(request, docx.document.Document)

    @pytest.fixture
    def Package_(self, request):
        return class_mock(request, "docx.api.Package")