1# encoding: utf-8
2
3"""
4Gherkin step implementations for core properties-related features.
5"""
6
7from __future__ import (
8    absolute_import, division, print_function, unicode_literals
9)
10
11from datetime import datetime, timedelta
12
13from behave import given, then, when
14
15from docx import Document
16from docx.opc.coreprops import CoreProperties
17
18from helpers import test_docx
19
20
21# given ===================================================
22
23@given('a document having known core properties')
24def given_a_document_having_known_core_properties(context):
25    context.document = Document(test_docx('doc-coreprops'))
26
27
28@given('a document having no core properties part')
29def given_a_document_having_no_core_properties_part(context):
30    context.document = Document(test_docx('doc-no-coreprops'))
31
32
33# when ====================================================
34
35@when('I access the core properties object')
36def when_I_access_the_core_properties_object(context):
37    context.document.core_properties
38
39
40@when("I assign new values to the properties")
41def when_I_assign_new_values_to_the_properties(context):
42    context.propvals = (
43        ('author',           'Creator'),
44        ('category',         'Category'),
45        ('comments',         'Description'),
46        ('content_status',   'Content Status'),
47        ('created',          datetime(2013, 6, 15, 12, 34, 56)),
48        ('identifier',       'Identifier'),
49        ('keywords',         'key; word; keyword'),
50        ('language',         'Language'),
51        ('last_modified_by', 'Last Modified By'),
52        ('last_printed',     datetime(2013, 6, 15, 12, 34, 56)),
53        ('modified',         datetime(2013, 6, 15, 12, 34, 56)),
54        ('revision',         9),
55        ('subject',          'Subject'),
56        ('title',            'Title'),
57        ('version',          'Version'),
58    )
59    core_properties = context.document.core_properties
60    for name, value in context.propvals:
61        setattr(core_properties, name, value)
62
63
64# then ====================================================
65
66@then('a core properties part with default values is added')
67def then_a_core_properties_part_with_default_values_is_added(context):
68    core_properties = context.document.core_properties
69    assert core_properties.title == 'Word Document'
70    assert core_properties.last_modified_by == 'python-docx'
71    assert core_properties.revision == 1
72    # core_properties.modified only stores time with seconds resolution, so
73    # comparison needs to be a little loose (within two seconds)
74    modified_timedelta = datetime.utcnow() - core_properties.modified
75    max_expected_timedelta = timedelta(seconds=2)
76    assert modified_timedelta < max_expected_timedelta
77
78
79@then('I can access the core properties object')
80def then_I_can_access_the_core_properties_object(context):
81    document = context.document
82    core_properties = document.core_properties
83    assert isinstance(core_properties, CoreProperties)
84
85
86@then('the core property values match the known values')
87def then_the_core_property_values_match_the_known_values(context):
88    known_propvals = (
89        ('author',           'Steve Canny'),
90        ('category',         'Category'),
91        ('comments',         'Description'),
92        ('content_status',   'Content Status'),
93        ('created',          datetime(2014, 12, 13, 22, 2, 0)),
94        ('identifier',       'Identifier'),
95        ('keywords',         'key; word; keyword'),
96        ('language',         'Language'),
97        ('last_modified_by', 'Steve Canny'),
98        ('last_printed',     datetime(2014, 12, 13, 22, 2, 42)),
99        ('modified',         datetime(2014, 12, 13, 22, 6, 0)),
100        ('revision',         2),
101        ('subject',          'Subject'),
102        ('title',            'Title'),
103        ('version',          '0.7.1a3'),
104    )
105    core_properties = context.document.core_properties
106    for name, expected_value in known_propvals:
107        value = getattr(core_properties, name)
108        assert value == expected_value, (
109            "got '%s' for core property '%s'" % (value, name)
110        )
111
112
113@then('the core property values match the new values')
114def then_the_core_property_values_match_the_new_values(context):
115    core_properties = context.document.core_properties
116    for name, expected_value in context.propvals:
117        value = getattr(core_properties, name)
118        assert value == expected_value, (
119            "got '%s' for core property '%s'" % (value, name)
120        )
121