Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Use a template library to render test xml #6

Merged
merged 10 commits into from

3 participants

@jlward
Owner

No description provided.

docx2html/tests/__init__.py
((205 lines not shown))
- <v:shape id="_x0000_i1027" type="#_x0000_t75" style="width:99.75pt;height:116.25pt">
- </v:shape>
- </w:pict>
- </w:r>
- </w:p>
-""".strip()
+env = Environment(
+ loader=PackageLoader(
+ 'docx2html.tests',
+ 'templates',
+ ),
+)
+
+
+def create_xml(body):
+ template = env.get_template('base.xml')
@winhamwr Owner

I would prefer a dictionary of constants for all of these template strings, since some of them are used multiple times (p.xml for example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
docx2html/tests/templates/tr.xml
@@ -0,0 +1,8 @@
+<w:tr>
+ <w:trPr>
+ <w:cantSplit w:val="false"/>
+ </w:trPr>
+ {% for tc in tcs %}
@winhamwr Owner

Can we use some better variable names for these templates things rather than abbreviations? I know that has some meaning to you, but it's kind of rough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
docx2html/tests/__init__.py
((196 lines not shown))
- <w:color w:val="000000"/>
- </w:rPr>
- </w:pPr>
- <w:r w:rsidR="00360165">
- <w:rPr>
- <w:b/>
- <w:color w:val="000000"/>
- </w:rPr>
- <w:pict>
- <v:shape id="_x0000_i1027" type="#_x0000_t75" style="width:99.75pt;height:116.25pt">
- </v:shape>
- </w:pict>
- </w:r>
- </w:p>
-""".strip()
+env = Environment(
@winhamwr Owner

What do you think about moving all of these generators to a DocxBuilder class. You could then use that to make it obvious what they were doing and drop the create_ prefix. Then when in the test files, you do something like:

from docx2html.tests import DocxBuilder as DXB

DXB.table(...)
DXB.p_tag(..)

That lets you be more concise while also making it obvious which functions are just throwing out XML and from where they're coming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
setup.py
@@ -29,7 +29,7 @@ def get_readme():
packages=find_packages(),
scripts=[],
zip_safe=False,
- install_requires=['lxml==2.2.4', 'pillow==1.7.7'],
+ install_requires=['lxml==2.2.4', 'pillow==1.7.7', 'Jinja2==2.6'],
@winhamwr Owner

Shouldn't this just go in test_requirements.txt? If you're having trouble because you have a jinja import in an __init__.py, you could either move the helper code to a tests.docx_builder.py module or do something like:

try:
    import Jinja2
except ImportError:
    Jinja2 = None

I'd say moving the helpers to a different module would probably be cleaner, since you would get an obvious error message when someone tried to run the tests without jinja.

@jlward Owner
jlward added a note

This is residual from when I did not know where I was going to put the template files, I will move it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
docx2html/tests/__init__.py
@@ -1,222 +1,118 @@
-import re
+from jinja2 import Environment, PackageLoader
@winhamwr Owner

I think we already talked about this, but since the ticket is marked ready for review, just reminding you to move this stuff to another module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test_requirements.txt
@@ -1,2 +1,3 @@
nose
mock
+Jinja==2.6
@winhamwr Owner

Does the requirement need to be pegged that specifically? Seems like say >=2.0 would probably be good enough?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@kylegibson
Owner

you can actually put these on separate lines, like this:

install:
   - python setup.py -q install
   - pip install -r test_requirements.txt

See https://github.com/PolicyStat/terrarium/blob/master/.travis.yml

@jlward jlward merged commit 25991f6 into master
@jlward jlward referenced this pull request from a commit
@jlward jlward refs #6: added jinja to the requirements f4ffa5e
@jlward jlward referenced this pull request from a commit
@jlward jlward refs #6: stopped using abbreviated names d6c6ff9
@jlward jlward referenced this pull request from a commit
@jlward jlward refs #6: moved req to test req only 514fcaa
@jlward jlward referenced this pull request from a commit
@jlward jlward refs #6: looser reqs for jinja 361799d
@jlward jlward referenced this pull request from a commit
@jlward jlward refs #6: lets tell travis how to install test reqs for real; install …
…the correct version of jinja(2)
bccc063
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 .travis.yml
@@ -3,7 +3,9 @@ python:
- "2.6"
- "2.7"
script: ./run_tests.sh
-install: "python setup.py -q install"
+install:
+ - python setup.py -q install
+ - pip install -r test_requirements.txt
notifications:
email:
- jason.louard.ward@gmail.com
View
214 docx2html/tests/__init__.py
@@ -1,223 +1,11 @@
-import re
from unittest import TestCase
+import re
from docx2html.core import (
MetaData,
create_html,
)
-DOCUMENT_XML_TEMPLATE = """
-<?xml version="1.0"?>
-<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
- %(body)s
-</w:document>
-""".strip()
-
-DOCUMENT_T_TEMPLATE = """
-<w:t>%(text)s</w:t>
-"""
-
-
-DOCUMENT_R_TEMPLATE = """
-<w:r>
- <w:rPr>
- %(bold)s
- </w:rPr>
- %(text)s
-</w:r>
-"""
-
-DOCUMENT_P_TEMPLATE = """
- <w:p>
- <w:pPr>
- <w:pStyle w:val="style0"/>
- </w:pPr>
- %(r_tags)s
- </w:p>
-""".strip()
-
-DOCUMENT_HYPERLINK_TEMPLATE = """
-<w:hyperlink r:id="%(r_id)s">
- %(r_tag)s
-</w:hyperlink>
-"""
-
-DOCUMENT_LI_TEMPLATE = """
- <w:p>
- <w:pPr>
- <w:pStyle w:val="style0"/>
- <w:numPr>
- <w:ilvl w:val="%(ilvl)d"/>
- <w:numId w:val="%(numId)d"/>
- </w:numPr>
- </w:pPr>
- <w:r>
- <w:rPr>
- %(bold)s
- </w:rPr>
- %(text)s
- </w:r>
- </w:p>
-""".strip()
-
-# Simple tc, no rowspan or colspans
-DOCUMENT_TC_TEMPLATE = """
- <w:tc>
- <w:tcPr>
- <w:tcW w:type="dxa" w:w="4986"/>
- <w:tcBorders>
- <w:top w:color="000000" w:space="0" w:sz="2" w:val="single"/>
- <w:left w:color="000000" w:space="0" w:sz="2" w:val="single"/>
- <w:bottom w:color="000000" w:space="0" w:sz="2" w:val="single"/>
- </w:tcBorders>
- <w:shd w:fill="auto" w:val="clear"/>
- <w:tcMar>
- <w:top w:type="dxa" w:w="55"/>
- <w:left w:type="dxa" w:w="55"/>
- <w:bottom w:type="dxa" w:w="55"/>
- <w:right w:type="dxa" w:w="55"/>
- </w:tcMar>
- </w:tcPr>
- %(p_tag)s
- </w:tc>
-""".strip()
-
-DOCUMENT_TR_TEMPLATE = """
- <w:tr>
- <w:trPr>
- <w:cantSplit w:val="false"/>
- </w:trPr>
- %(tcs)s
- </w:tr>
-""".strip()
-
-DOCUMENT_TBL_TEMPLATE = """
- <w:tbl>
- <w:tblPr>
- <w:tblW w:type="dxa" w:w="9972"/>
- <w:jc w:val="left"/>
- <w:tblBorders>
- <w:top w:color="000000" w:space="0" w:sz="2" w:val="single"/>
- <w:left w:color="000000" w:space="0" w:sz="2" w:val="single"/>
- <w:bottom w:color="000000" w:space="0" w:sz="2" w:val="single"/>
- </w:tblBorders>
- </w:tblPr>
- <w:tblGrid>
- <w:gridCol w:w="4986"/>
- <w:gridCol w:w="4986"/>
- </w:tblGrid>
- %(trs)s
- </w:tbl>
-""".strip()
-
-
-DOCUMENT_DRAWING_TEMPLATE = """
- <w:p>
- <w:pPr>
- <w:pStyle w:val="style0"/>
- </w:pPr>
- <w:r>
- <w:rPr/>
- <w:drawing>
- <wp:anchor allowOverlap="1" behindDoc="0" distB="0" distL="0" distR="0" distT="0" layoutInCell="1" locked="0" relativeHeight="0" simplePos="0">
- <wp:simplePos x="0" y="0"/>
- <wp:positionH relativeFrom="column">
- <wp:posOffset>2397125</wp:posOffset>
- </wp:positionH>
- <wp:positionV relativeFrom="paragraph">
- <wp:posOffset>0</wp:posOffset>
- </wp:positionV>
- <wp:extent cx="1537970" cy="354965"/>
- <wp:effectExtent b="0" l="0" r="0" t="0"/>
- <wp:wrapSquare wrapText="largest"/>
- <wp:docPr descr="A description..." id="1" name="Picture"/>
- <wp:cNvGraphicFramePr>
- <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
- </wp:cNvGraphicFramePr>
- <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
- <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
- <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
- <pic:nvPicPr>
- <pic:cNvPr descr="A description..." id="0" name="Picture"/>
- <pic:cNvPicPr>
- <a:picLocks noChangeArrowheads="1" noChangeAspect="1"/>
- </pic:cNvPicPr>
- </pic:nvPicPr>
- <pic:blipFill>
- <a:blip r:embed="%(r_id)s"/>
- <a:srcRect/>
- <a:stretch>
- <a:fillRect/>
- </a:stretch>
- </pic:blipFill>
- <pic:spPr bwMode="auto">
- <a:xfrm>
- <a:off x="0" y="0"/>
- <a:ext cx="1537970" cy="354965"/>
- </a:xfrm>
- <a:prstGeom prst="rect">
- <a:avLst/>
- </a:prstGeom>
- <a:noFill/>
- <a:ln w="9525">
- <a:noFill/>
- <a:miter lim="800000"/>
- <a:headEnd/>
- <a:tailEnd/>
- </a:ln>
- </pic:spPr>
- </pic:pic>
- </a:graphicData>
- </a:graphic>
- </wp:anchor>
- </w:drawing>
- </w:r>
- </w:p>
-""".strip()
-
-
-DOCUMENT_PICT_TEMPLATE = """
- <w:p w:rsidR="00E94BDC" w:rsidRPr="003638EA" w:rsidRDefault="00E94BDC" w:rsidP="00E94BDC">
- <w:pPr>
- <w:rPr>
- <w:color w:val="000000"/>
- </w:rPr>
- </w:pPr>
- <w:r w:rsidR="00360165">
- <w:rPr>
- <w:b/>
- <w:color w:val="000000"/>
- </w:rPr>
- <w:pict>
- <v:shape id="_x0000_i1027" type="#_x0000_t75" style="width:99.75pt;height:116.25pt">
- <v:imagedata r:id="%(r_id)s" o:title="New Picture"/>
- </v:shape>
- </w:pict>
- </w:r>
- </w:p>
-""".strip()
-
-
-DOCUMENT_PICT_NO_IMAGEID_TEMPLATE = """
- <w:p w:rsidR="00E94BDC" w:rsidRPr="003638EA" w:rsidRDefault="00E94BDC" w:rsidP="00E94BDC">
- <w:pPr>
- <w:rPr>
- <w:color w:val="000000"/>
- </w:rPr>
- </w:pPr>
- <w:r w:rsidR="00360165">
- <w:rPr>
- <w:b/>
- <w:color w:val="000000"/>
- </w:rPr>
- <w:pict>
- <v:shape id="_x0000_i1027" type="#_x0000_t75" style="width:99.75pt;height:116.25pt">
- </v:shape>
- </w:pict>
- </w:r>
- </w:p>
-""".strip()
-
def assert_html_equal(actual_html, expected_html):
assert collapse_html(
View
110 docx2html/tests/document_builder.py
@@ -0,0 +1,110 @@
+from jinja2 import Environment, PackageLoader
+
+templates = {
+ 'drawing': 'drawing.xml',
+ 'hyperlink': 'hyperlink.xml',
+ 'main': 'base.xml',
+ 'p': 'p.xml',
+ 'pict': 'pict.xml',
+ 'r': 'r.xml',
+ 'table': 'table.xml',
+ 'tc': 'tc.xml',
+ 'tr': 'tr.xml',
+}
+
+env = Environment(
+ loader=PackageLoader(
+ 'docx2html.tests',
+ 'templates',
+ ),
+)
+
+
+class DocxBuilder(object):
+ @classmethod
+ def xml(self, body):
+ template = env.get_template(templates['main'])
+ return template.render(body=body)
+
+ @classmethod
+ def p_tag(self, text, bold=False):
+ if isinstance(text, str):
+ # Use create a single r tag based on the text and the bold
+ run_tag = DocxBuilder.r_tag(text, bold)
+ run_tags = [run_tag]
+ elif isinstance(text, list):
+ run_tags = text
+ else:
+ raise AssertionError('text must be a string or a list')
+ template = env.get_template(templates['p'])
+
+ kwargs = {
+ 'run_tags': run_tags,
+ }
+ return template.render(**kwargs)
+
+ @classmethod
+ def r_tag(self, text, is_bold=False):
+ template = env.get_template(templates['r'])
+ kwargs = {
+ 'text': text,
+ 'is_bold': is_bold,
+ }
+ return template.render(**kwargs)
+
+ @classmethod
+ def hyperlink_tag(self, r_id, run_tags):
+ template = env.get_template(templates['hyperlink'])
+ kwargs = {
+ 'r_id': r_id,
+ 'run_tags': run_tags,
+ }
+ return template.render(**kwargs)
+
+ @classmethod
+ def li(self, text, ilvl, numId, bold=False):
+ if isinstance(text, str):
+ # Use create a single r tag based on the text and the bold
+ run_tag = DocxBuilder.r_tag(text, bold)
+ run_tags = [run_tag]
+ elif isinstance(text, list):
+ run_tags = []
+ for run_text, run_bold in text:
+ run_tags.append(DocxBuilder.r_tag(run_tags, run_bold))
+ else:
+ raise AssertionError('text must be a string or a list')
+ template = env.get_template(templates['p'])
+
+ kwargs = {
+ 'run_tags': run_tags,
+ 'is_list': True,
+ 'ilvl': ilvl,
+ 'numId': numId,
+ }
+ return template.render(**kwargs)
+
+ @classmethod
+ def table(self, num_rows, num_columns, text):
+
+ def _tc(cell_value):
+ template = env.get_template(templates['tc'])
+ return template.render(p_tag=cell_value)
+
+ def _tr(rows, text):
+ tcs = [_tc(text.next()) for _ in range(rows)]
+ template = env.get_template(templates['tr'])
+ return template.render(table_cells=tcs)
+
+ trs = [_tr(num_rows, text) for _ in range(num_rows)]
+ template = env.get_template(templates['table'])
+ return template.render(table_rows=trs)
+
+ @classmethod
+ def drawing(self, r_id):
+ template = env.get_template(templates['drawing'])
+ return template.render(r_id=r_id)
+
+ @classmethod
+ def pict(self, r_id=None):
+ template = env.get_template(templates['pict'])
+ return template.render(r_id=r_id)
View
4 docx2html/tests/templates/base.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
+ {{ body }}
+</w:document>
View
61 docx2html/tests/templates/drawing.xml
@@ -0,0 +1,61 @@
+<w:p>
+ <w:pPr>
+ <w:pStyle w:val="style0"/>
+ </w:pPr>
+ <w:r>
+ <w:rPr/>
+ <w:drawing>
+ <wp:anchor allowOverlap="1" behindDoc="0" distB="0" distL="0" distR="0" distT="0" layoutInCell="1" locked="0" relativeHeight="0" simplePos="0">
+ <wp:simplePos x="0" y="0"/>
+ <wp:positionH relativeFrom="column">
+ <wp:posOffset>2397125</wp:posOffset>
+ </wp:positionH>
+ <wp:positionV relativeFrom="paragraph">
+ <wp:posOffset>0</wp:posOffset>
+ </wp:positionV>
+ <wp:extent cx="1537970" cy="354965"/>
+ <wp:effectExtent b="0" l="0" r="0" t="0"/>
+ <wp:wrapSquare wrapText="largest"/>
+ <wp:docPr descr="A description..." id="1" name="Picture"/>
+ <wp:cNvGraphicFramePr>
+ <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
+ </wp:cNvGraphicFramePr>
+ <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
+ <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
+ <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
+ <pic:nvPicPr>
+ <pic:cNvPr descr="A description..." id="0" name="Picture"/>
+ <pic:cNvPicPr>
+ <a:picLocks noChangeArrowheads="1" noChangeAspect="1"/>
+ </pic:cNvPicPr>
+ </pic:nvPicPr>
+ <pic:blipFill>
+ <a:blip r:embed="{{ r_id }}"/>
+ <a:srcRect/>
+ <a:stretch>
+ <a:fillRect/>
+ </a:stretch>
+ </pic:blipFill>
+ <pic:spPr bwMode="auto">
+ <a:xfrm>
+ <a:off x="0" y="0"/>
+ <a:ext cx="1537970" cy="354965"/>
+ </a:xfrm>
+ <a:prstGeom prst="rect">
+ <a:avLst/>
+ </a:prstGeom>
+ <a:noFill/>
+ <a:ln w="9525">
+ <a:noFill/>
+ <a:miter lim="800000"/>
+ <a:headEnd/>
+ <a:tailEnd/>
+ </a:ln>
+ </pic:spPr>
+ </pic:pic>
+ </a:graphicData>
+ </a:graphic>
+ </wp:anchor>
+ </w:drawing>
+ </w:r>
+</w:p>
View
5 docx2html/tests/templates/hyperlink.xml
@@ -0,0 +1,5 @@
+<w:hyperlink r:id="{{ r_id }}">
+ {% for run_tag in run_tags %}
+ {{ run_tag }}
+ {% endfor %}
+</w:hyperlink>
View
14 docx2html/tests/templates/p.xml
@@ -0,0 +1,14 @@
+<w:p>
+ <w:pPr>
+ <w:pStyle w:val="style0"/>
+ {% if is_list %}
+ <w:numPr>
+ <w:ilvl w:val="{{ ilvl }}"/>
+ <w:numId w:val="{{ numId }}"/>
+ </w:numPr>
+ {% endif %}
+ </w:pPr>
+ {% for run_tag in run_tags %}
+ {{ run_tag }}
+ {% endfor %}
+</w:p>
View
17 docx2html/tests/templates/pict.xml
@@ -0,0 +1,17 @@
+<w:p w:rsidR="00E94BDC" w:rsidRPr="003638EA" w:rsidRDefault="00E94BDC" w:rsidP="00E94BDC">
+ <w:pPr>
+ <w:rPr>
+ <w:color w:val="000000"/>
+ </w:rPr>
+ </w:pPr>
+ <w:r w:rsidR="00360165">
+ <w:rPr>
+ <w:color w:val="000000"/>
+ </w:rPr>
+ <w:pict>
+ <v:shape id="_x0000_i1027" type="#_x0000_t75" style="width:99.75pt;height:116.25pt">
+ {% if r_id %}<v:imagedata r:id="{{ r_id }}" o:title="New Picture"/>{% endif %}
+ </v:shape>
+ </w:pict>
+ </w:r>
+</w:p>
View
8 docx2html/tests/templates/r.xml
@@ -0,0 +1,8 @@
+<w:r>
+ <w:rPr>
+ {% if is_bold %}
+ <w:b/>
+ {% endif %}
+ </w:rPr>
+ {% include 't.xml' %}
+</w:r>
View
1  docx2html/tests/templates/t.xml
@@ -0,0 +1 @@
+<w:t>{{ text }}</w:t>
View
18 docx2html/tests/templates/table.xml
@@ -0,0 +1,18 @@
+<w:tbl>
+ <w:tblPr>
+ <w:tblW w:type="dxa" w:w="9972"/>
+ <w:jc w:val="left"/>
+ <w:tblBorders>
+ <w:top w:color="000000" w:space="0" w:sz="2" w:val="single"/>
+ <w:left w:color="000000" w:space="0" w:sz="2" w:val="single"/>
+ <w:bottom w:color="000000" w:space="0" w:sz="2" w:val="single"/>
+ </w:tblBorders>
+ </w:tblPr>
+ <w:tblGrid>
+ <w:gridCol w:w="4986"/>
+ <w:gridCol w:w="4986"/>
+ </w:tblGrid>
+ {% for table_row in table_rows %}
+ {{ table_row }}
+ {% endfor %}
+</w:tbl>
View
18 docx2html/tests/templates/tc.xml
@@ -0,0 +1,18 @@
+<w:tc>
+ <w:tcPr>
+ <w:tcW w:type="dxa" w:w="4986"/>
+ <w:tcBorders>
+ <w:top w:color="000000" w:space="0" w:sz="2" w:val="single"/>
+ <w:left w:color="000000" w:space="0" w:sz="2" w:val="single"/>
+ <w:bottom w:color="000000" w:space="0" w:sz="2" w:val="single"/>
+ </w:tcBorders>
+ <w:shd w:fill="auto" w:val="clear"/>
+ <w:tcMar>
+ <w:top w:type="dxa" w:w="55"/>
+ <w:left w:type="dxa" w:w="55"/>
+ <w:bottom w:type="dxa" w:w="55"/>
+ <w:right w:type="dxa" w:w="55"/>
+ </w:tcMar>
+ </w:tcPr>
+ {{ p_tag }}
+</w:tc>
View
8 docx2html/tests/templates/tr.xml
@@ -0,0 +1,8 @@
+<w:tr>
+ <w:trPr>
+ <w:cantSplit w:val="false"/>
+ </w:trPr>
+ {% for table_cell in table_cells %}
+ {{ table_cell }}
+ {% endfor %}
+</w:tr>
View
215 docx2html/tests/test_xml.py
@@ -11,92 +11,13 @@
get_namespace,
is_last_li,
)
+from docx2html.tests.document_builder import DocxBuilder as DXB
from docx2html.tests import (
- DOCUMENT_DRAWING_TEMPLATE,
- DOCUMENT_HYPERLINK_TEMPLATE,
- DOCUMENT_LI_TEMPLATE,
- DOCUMENT_PICT_NO_IMAGEID_TEMPLATE,
- DOCUMENT_PICT_TEMPLATE,
- DOCUMENT_P_TEMPLATE,
- DOCUMENT_R_TEMPLATE,
- DOCUMENT_TBL_TEMPLATE,
- DOCUMENT_TC_TEMPLATE,
- DOCUMENT_TR_TEMPLATE,
- DOCUMENT_T_TEMPLATE,
- DOCUMENT_XML_TEMPLATE,
_TranslationTestCase,
assert_html_equal,
)
-def _create_t_tag(text):
- return DOCUMENT_T_TEMPLATE % {
- 'text': text,
- }
-
-
-def _bold(is_bold):
- if is_bold:
- return '<w:b/>'
- return ''
-
-
-def _create_p_tag(r_tags):
- """
- r_tags is a list of r tags or hyperlinks (or some combination)
- """
- return DOCUMENT_P_TEMPLATE % {
- 'r_tags': ''.join(r_tags)
- }
-
-
-def _create_r_tag(text, is_bold=False):
- t_tag = _create_t_tag(text)
- return DOCUMENT_R_TEMPLATE % {
- 'text': t_tag,
- 'bold': _bold(is_bold=is_bold),
- }
-
-
-def _create_hyperlink_tag(r_id, r_tag):
- return DOCUMENT_HYPERLINK_TEMPLATE % {
- 'r_id': r_id,
- 'r_tag': r_tag,
- }
-
-
-def _create_li(text, ilvl, numId, bold=False):
- text = _create_t_tag(text)
- return DOCUMENT_LI_TEMPLATE % {
- 'text': text,
- 'ilvl': ilvl,
- 'numId': numId,
- 'bold': _bold(is_bold=bold),
- }
-
-
-def _create_table(num_rows, num_columns, text):
- def _create_tc(cell_value):
- return DOCUMENT_TC_TEMPLATE % {
- 'p_tag': cell_value,
- }
-
- def _create_tr(rows, text):
- tcs = ''
- for _ in range(rows):
- tcs += _create_tc(text.next())
- return DOCUMENT_TR_TEMPLATE % {
- 'tcs': tcs,
- }
-
- trs = ''
- for _ in range(num_columns):
- trs += _create_tr(num_rows, text)
- return DOCUMENT_TBL_TEMPLATE % {
- 'trs': trs,
- }
-
-
class SimpleListTestCase(_TranslationTestCase):
expected_output = '''
<html>
@@ -116,11 +37,9 @@ def get_xml(self):
]
lis = ''
for text, ilvl, numId in li_text:
- lis += _create_li(text=text, ilvl=ilvl, numId=numId)
+ lis += DXB.li(text=text, ilvl=ilvl, numId=numId)
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': lis,
- }
+ xml = DXB.xml(lis)
return etree.fromstring(xml)
def test_get_li_nodes(self):
@@ -166,23 +85,22 @@ class TableInListTestCase(_TranslationTestCase):
'''
def get_xml(self):
- table = _create_table(num_rows=2, num_columns=2, text=chain(
- [_create_p_tag([_create_r_tag('BBB')])],
- [_create_p_tag([_create_r_tag('CCC')])],
- [_create_p_tag([_create_r_tag('DDD')])],
- [_create_p_tag([_create_r_tag('EEE')])],
+ table = DXB.table(num_rows=2, num_columns=2, text=chain(
+ [DXB.p_tag('BBB')],
+ [DXB.p_tag('CCC')],
+ [DXB.p_tag('DDD')],
+ [DXB.p_tag('EEE')],
))
# Nest that table in a list.
- first_li = _create_li(text='AAA', ilvl=0, numId=1)
- second = _create_li(text='FFF', ilvl=0, numId=1)
- p_tag = _create_p_tag([_create_r_tag('GGG')])
+ first_li = DXB.li(text='AAA', ilvl=0, numId=1)
+ second = DXB.li(text='FFF', ilvl=0, numId=1)
+ p_tag = DXB.p_tag('GGG')
+
body = ''
for el in [first_li, table, second, p_tag]:
body += el
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': body,
- }
+ xml = DXB.xml(body)
return etree.fromstring(xml)
def test_get_li_nodes_with_nested_table(self):
@@ -246,13 +164,11 @@ def get_xml(self):
('FFF', 1, 1),
('GGG', 2, 1),
]
- lis = ''
+ body = ''
for text, ilvl, numId in li_text:
- lis += _create_li(text=text, ilvl=ilvl, numId=numId)
+ body += DXB.li(text=text, ilvl=ilvl, numId=numId)
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': lis,
- }
+ xml = DXB.xml(body)
return etree.fromstring(xml)
def test_is_top_level_upper_roman(self):
@@ -299,13 +215,11 @@ def get_xml(self):
('BBB', 0, 1),
('CCC', 0, 1),
]
- lis = ''
+ body = ''
for text, ilvl, numId in li_text:
- lis += _create_li(text=text, ilvl=ilvl, numId=numId, bold=True)
+ body += DXB.li(text=text, ilvl=ilvl, numId=numId, bold=True)
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': lis,
- }
+ xml = DXB.xml(body)
return etree.fromstring(xml)
@@ -334,12 +248,8 @@ def image_handler(image_id, relationship_dict):
return relationship_dict.get(image_id)
def get_xml(self):
- drawing = DOCUMENT_DRAWING_TEMPLATE % {
- 'r_id': 'rId0',
- }
- pict = DOCUMENT_PICT_TEMPLATE % {
- 'r_id': 'rId1',
- }
+ drawing = DXB.drawing('rId0')
+ pict = DXB.pict('rId1')
tags = [
drawing,
pict,
@@ -348,9 +258,7 @@ def get_xml(self):
for el in tags:
body += el
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': body,
- }
+ xml = DXB.xml(body)
return etree.fromstring(xml)
def test_get_image_id(self):
@@ -420,26 +328,24 @@ class ListWithContinuationTestCase(_TranslationTestCase):
'''
def get_xml(self):
- table = _create_table(num_rows=2, num_columns=2, text=chain(
- [_create_p_tag([_create_r_tag('DDD')])],
- [_create_p_tag([_create_r_tag('EEE')])],
- [_create_p_tag([_create_r_tag('FFF')])],
- [_create_p_tag([_create_r_tag('GGG')])],
+ table = DXB.table(num_rows=2, num_columns=2, text=chain(
+ [DXB.p_tag('DDD')],
+ [DXB.p_tag('EEE')],
+ [DXB.p_tag('FFF')],
+ [DXB.p_tag('GGG')],
))
tags = [
- _create_li(text='AAA', ilvl=0, numId=1),
- _create_p_tag([_create_r_tag('BBB')]),
- _create_li(text='CCC', ilvl=0, numId=1),
+ DXB.li(text='AAA', ilvl=0, numId=1),
+ DXB.p_tag('BBB'),
+ DXB.li(text='CCC', ilvl=0, numId=1),
table,
- _create_li(text='HHH', ilvl=0, numId=1),
+ DXB.li(text='HHH', ilvl=0, numId=1),
]
body = ''
for el in tags:
body += el
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': body,
- }
+ xml = DXB.xml(body)
return etree.fromstring(xml)
@@ -463,9 +369,7 @@ def image_handler(image_id, relationship_dict):
return relationship_dict.get(image_id)
def get_xml(self):
- pict = DOCUMENT_PICT_TEMPLATE % {
- 'r_id': 'rId0',
- }
+ pict = DXB.pict('rId0')
tags = [
pict,
]
@@ -473,9 +377,7 @@ def get_xml(self):
for el in tags:
body += el
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': body,
- }
+ xml = DXB.xml(body)
return etree.fromstring(xml)
def test_image_id_for_pict(self):
@@ -491,13 +393,13 @@ def test_image_id_for_pict(self):
self.assertEqual(image_id, 'rId0')
-class PictImageTestCase(_TranslationTestCase):
+class PictImageMissingIdTestCase(_TranslationTestCase):
expected_output = '''
<html></html>
'''
def get_xml(self):
- pict = DOCUMENT_PICT_NO_IMAGEID_TEMPLATE
+ pict = DXB.pict(None)
tags = [
pict,
]
@@ -505,9 +407,7 @@ def get_xml(self):
for el in tags:
body += el
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': body,
- }
+ xml = DXB.xml(body)
return etree.fromstring(xml)
@@ -528,17 +428,16 @@ class TableWithInvalidTag(_TranslationTestCase):
'''
def get_xml(self):
- table = _create_table(num_rows=2, num_columns=2, text=chain(
- [_create_p_tag([_create_r_tag('AAA')])],
- [_create_p_tag([_create_r_tag('BBB')])],
+ table = DXB.table(num_rows=2, num_columns=2, text=chain(
+ [DXB.p_tag('AAA')],
+ [DXB.p_tag('BBB')],
# This tag may have CCC in it, however this tag has no meaning
# pertaining to content.
['<w:invalidTag>CCC</w:invalidTag>'],
- [_create_p_tag([_create_r_tag('DDD')])],
+ [DXB.p_tag('DDD')],
))
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': table,
- }
+ body = table
+ xml = DXB.xml(body)
return etree.fromstring(xml)
@@ -549,17 +448,17 @@ class HyperlinkStyledTestCase(_TranslationTestCase):
expected_output = '''
<html>
- <p><a href="www.google.com">link</a></p>
+ <p><a href="www.google.com">link</a>.</p>
</html>
'''
def get_xml(self):
- r_tag = _create_r_tag('link', is_bold=True)
- hyperlink = _create_hyperlink_tag(r_id='rId0', r_tag=r_tag)
- p_tag = _create_p_tag([hyperlink])
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': p_tag,
- }
+ run_tags = []
+ run_tags.append(DXB.r_tag('link', is_bold=True))
+ run_tags = [DXB.hyperlink_tag(r_id='rId0', run_tags=run_tags)]
+ run_tags.append(DXB.r_tag('.', is_bold=False))
+ body = DXB.p_tag(run_tags)
+ xml = DXB.xml(body)
return etree.fromstring(xml)
@@ -570,15 +469,15 @@ class HyperlinkVanillaTestCase(_TranslationTestCase):
expected_output = '''
<html>
- <p><a href="www.google.com">link</a></p>
+ <p><a href="www.google.com">link</a>.</p>
</html>
'''
def get_xml(self):
- r_tag = _create_r_tag('link', is_bold=False)
- hyperlink = _create_hyperlink_tag(r_id='rId0', r_tag=r_tag)
- p_tag = _create_p_tag([hyperlink])
- xml = DOCUMENT_XML_TEMPLATE % {
- 'body': p_tag,
- }
+ run_tags = []
+ run_tags.append(DXB.r_tag('link', is_bold=False))
+ run_tags = [DXB.hyperlink_tag(r_id='rId0', run_tags=run_tags)]
+ run_tags.append(DXB.r_tag('.', is_bold=False))
+ body = DXB.p_tag(run_tags)
+ xml = DXB.xml(body)
return etree.fromstring(xml)
View
1  test_requirements.txt
@@ -1,2 +1,3 @@
nose
mock
+Jinja2>=2.0
Something went wrong with that request. Please try again.