import unittest
import tempfile
from os import path
from lxml import etree

from mailmerge import MailMerge
from tests.utils import EtreeMixin, get_document_body_part


class MergeListTest(EtreeMixin, unittest.TestCase):

    def test_break_page(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_templates_simple.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_templates([
					{'fieldname': "Test with page_break"},
					{'fieldname': "abc"},
					{'fieldname': "2b v ~2b"},
				], 'page_break')

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>Test with page_break</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:r><w:br w:type="page"/></w:r></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>abc</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:r><w:br w:type="page"/></w:r></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>2b v ~2b</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:type w:val="continuous"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())

    def test_break_col(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_templates_simple.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_templates([
					{'fieldname': "Test with column_break"},
					{'fieldname': "abc"},
					{'fieldname': "2b v ~2b"},
				], 'column_break')

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>Test with column_break</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:r><w:br w:type="column"/></w:r></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>abc</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:r><w:br w:type="column"/></w:r></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>2b v ~2b</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:type w:val="continuous"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())

    def test_break_tW(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_templates_simple.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_templates([
					{'fieldname': "Test with textWrapping_break"},
					{'fieldname': "abc"},
					{'fieldname': "2b v ~2b"},
				], 'textWrapping_break')

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>Test with textWrapping_break</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:r><w:br w:type="textWrapping"/></w:r></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>abc</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:r><w:br w:type="textWrapping"/></w:r></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>2b v ~2b</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:type w:val="continuous"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())

    def test_sect_page(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_templates_simple.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_templates([
					{'fieldname': "Test with nextPage_section"},
					{'fieldname': "abc"},
					{'fieldname': "2b v ~2b"},
				], 'nextPage_section')

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>Test with nextPage_section</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="nextPage"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>abc</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="nextPage"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>2b v ~2b</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="nextPage"/></w:sectPr></w:body></w:document>')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())

    def test_sect_cont(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_templates_simple.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_templates([
					{'fieldname': "Test with continuous_section"},
					{'fieldname': "abc"},
					{'fieldname': "2b v ~2b"},
				], 'continuous_section')

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>Test with continuous_section</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="continuous"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>abc</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="continuous"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>2b v ~2b</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="continuous"/></w:sectPr></w:body></w:document>')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())

    def test_sect_eP(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_templates_simple.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_templates([
					{'fieldname': "Test with evenPage_section"},
					{'fieldname': "abc"},
					{'fieldname': "2b v ~2b"},
				], 'evenPage_section')

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>Test with evenPage_section</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="evenPage"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>abc</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="evenPage"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>2b v ~2b</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="evenPage"/></w:sectPr></w:body></w:document>')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())

    def test_sect_oP(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_templates_simple.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_templates([
					{'fieldname': "Test with oddPage_section"},
					{'fieldname': "abc"},
					{'fieldname': "2b v ~2b"},
				], 'oddPage_section')

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>Test with oddPage_section</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="oddPage"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>abc</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="oddPage"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>2b v ~2b</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="oddPage"/></w:sectPr></w:body></w:document>')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())

    def test_sect_col(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_templates_simple.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_templates([
					{'fieldname': "Test with nextColumn_section"},
					{'fieldname': "abc"},
					{'fieldname': "2b v ~2b"},
				], 'nextColumn_section')

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>Test with nextColumn_section</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="nextColumn"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>abc</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p><w:pPr><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="nextColumn"/></w:sectPr></w:pPr></w:p><w:p w:rsidR="005D67CE" w:rsidRDefault="00C83F78" w:rsidP="007B2B98"><w:pPr><w:rPr><w:noProof/></w:rPr></w:pPr><w:proofErr w:type="gramStart"/><w:r><w:t>Merge :</w:t></w:r><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>2b v ~2b</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:sectPr w:rsidR="005D67CE" w:rsidSect="00431D2A"><w:headerReference w:type="default" r:id="rId6"/><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="709"/><w:docGrid w:linePitch="360"/><w:type w:val="nextColumn"/></w:sectPr></w:body></w:document>')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())

