File: test_text.py

package info (click to toggle)
python-petl 1.7.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,224 kB
  • sloc: python: 22,617; makefile: 109; xml: 9
file content (191 lines) | stat: -rw-r--r-- 4,253 bytes parent folder | download | duplicates (2)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division


from tempfile import NamedTemporaryFile
import gzip
import os
import io


from petl.test.helpers import ieq, eq_
from petl.io.text import fromtext, totext


def test_fromtext():

    # initial data
    f = NamedTemporaryFile(delete=False, mode='wb')
    f.write(b'foo\tbar\n')
    f.write(b'a\t1\n')
    f.write(b'b\t2\n')
    f.write(b'c\t3\n')
    f.close()

    actual = fromtext(f.name, encoding='ascii')
    expect = (('lines',),
              ('foo\tbar',),
              ('a\t1',),
              ('b\t2',),
              ('c\t3',))
    ieq(expect, actual)
    ieq(expect, actual)  # verify can iterate twice


def test_fromtext_lineterminators():

    data = [b'foo,bar',
            b'a,1',
            b'b,2',
            b'c,2']

    expect = (('lines',),
              ('foo,bar',),
              ('a,1',),
              ('b,2',),
              ('c,2',))

    for lt in b'\r', b'\n', b'\r\n':
        f = NamedTemporaryFile(mode='wb', delete=False)
        f.write(lt.join(data))
        f.close()
        actual = fromtext(f.name, encoding='ascii')
        ieq(expect, actual)


def test_totext():

    # exercise function
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    f = NamedTemporaryFile(delete=False)
    f.close()
    prologue = (
        "{| class='wikitable'\n"
        "|-\n"
        "! foo\n"
        "! bar\n"
    )
    template = (
        "|-\n"
        "| {foo}\n"
        "| {bar}\n"
    )
    epilogue = "|}\n"
    totext(table, f.name, encoding='ascii', template=template,
           prologue=prologue, epilogue=epilogue)

    # check what it did
    with io.open(f.name, mode='rt', encoding='ascii', newline='') as o:
        actual = o.read()
        expect = (
            "{| class='wikitable'\n"
            "|-\n"
            "! foo\n"
            "! bar\n"
            "|-\n"
            "| a\n"
            "| 1\n"
            "|-\n"
            "| b\n"
            "| 2\n"
            "|-\n"
            "| c\n"
            "| 2\n"
            "|}\n"
        )
        eq_(expect, actual)


def test_fromtext_gz():

    # initial data
    f = NamedTemporaryFile(delete=False)
    f.close()
    fn = f.name + '.gz'
    os.rename(f.name, fn)
    f = gzip.open(fn, 'wb')
    try:
        f.write(b'foo\tbar\n')
        f.write(b'a\t1\n')
        f.write(b'b\t2\n')
        f.write(b'c\t3\n')
    finally:
        f.close()

    actual = fromtext(fn, encoding='ascii')
    expect = (('lines',),
              ('foo\tbar',),
              ('a\t1',),
              ('b\t2',),
              ('c\t3',))
    ieq(expect, actual)
    ieq(expect, actual)  # verify can iterate twice


def test_totext_gz():

    # exercise function
    table = (('foo', 'bar'),
             ('a', 1),
             ('b', 2),
             ('c', 2))
    f = NamedTemporaryFile(delete=False)
    f.close()
    fn = f.name + '.gz'
    os.rename(f.name, fn)
    prologue = (
        "{| class='wikitable'\n"
        "|-\n"
        "! foo\n"
        "! bar\n"
    )
    template = (
        "|-\n"
        "| {foo}\n"
        "| {bar}\n"
    )
    epilogue = "|}\n"
    totext(table, fn, encoding='ascii', template=template, prologue=prologue,
           epilogue=epilogue)

    # check what it did
    o = gzip.open(fn, 'rb')
    try:
        actual = o.read()
        expect = (
            b"{| class='wikitable'\n"
            b"|-\n"
            b"! foo\n"
            b"! bar\n"
            b"|-\n"
            b"| a\n"
            b"| 1\n"
            b"|-\n"
            b"| b\n"
            b"| 2\n"
            b"|-\n"
            b"| c\n"
            b"| 2\n"
            b"|}\n"
        )
        eq_(expect, actual)
    finally:
        o.close()


def test_totext_headerless():
    table = []
    f = NamedTemporaryFile(delete=False)
    prologue = "-- START\n"
    template = "+ {f1}\n"
    epilogue = "-- END\n"
    totext(table, f.name, encoding='ascii', template=template,
           prologue=prologue, epilogue=epilogue)

    with io.open(f.name, mode='rt', encoding='ascii', newline='') as o:
        actual = o.read()
        expect = prologue + epilogue
        eq_(expect, actual)