| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
|
| 5 |
import gzip |
| 6 |
import bz2 |
| 7 |
import zipfile |
| 8 |
from tempfile import NamedTemporaryFile |
| 9 |
from petl.compat import PY2 |
| 10 |
|
| 11 |
|
| 12 |
from petl.test.helpers import ieq, eq_ |
| 13 |
import petl as etl |
| 14 |
from petl.io.sources import MemorySource, PopenSource, ZipSource, \ |
| 15 |
StdoutSource, GzipSource, BZ2Source |
| 16 |
|
| 17 |
|
| 18 |
def test_memorysource(): |
| 19 |
tbl1 = (('foo', 'bar'), |
| 20 |
('a', '1'), |
| 21 |
('b', '2'), |
| 22 |
('c', '2')) |
| 23 |
|
| 24 |
# test writing to a string buffer |
| 25 |
ss = MemorySource() |
| 26 |
etl.tocsv(tbl1, ss) |
| 27 |
expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\n" |
| 28 |
if not PY2: |
| 29 |
expect = expect.encode('ascii') |
| 30 |
actual = ss.getvalue() |
| 31 |
eq_(expect, actual) |
| 32 |
|
| 33 |
# test reading from a string buffer |
| 34 |
tbl2 = etl.fromcsv(MemorySource(actual)) |
| 35 |
ieq(tbl1, tbl2) |
| 36 |
ieq(tbl1, tbl2) |
| 37 |
|
| 38 |
# test appending |
| 39 |
etl.appendcsv(tbl1, ss) |
| 40 |
actual = ss.getvalue() |
| 41 |
expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\na,1\r\nb,2\r\nc,2\r\n" |
| 42 |
if not PY2: |
| 43 |
expect = expect.encode('ascii') |
| 44 |
eq_(expect, actual) |
| 45 |
|
| 46 |
|
| 47 |
def test_memorysource_2(): |
| 48 |
|
| 49 |
data = 'foo,bar\r\na,1\r\nb,2\r\nc,2\r\n' |
| 50 |
if not PY2: |
| 51 |
data = data.encode('ascii') |
| 52 |
actual = etl.fromcsv(MemorySource(data)) |
| 53 |
expect = (('foo', 'bar'), |
| 54 |
('a', '1'), |
| 55 |
('b', '2'), |
| 56 |
('c', '2')) |
| 57 |
ieq(expect, actual) |
| 58 |
ieq(expect, actual) |
| 59 |
|
| 60 |
|
| 61 |
def test_popensource(): |
| 62 |
|
| 63 |
expect = (('foo', 'bar'),) |
| 64 |
delimiter = ' ' |
| 65 |
actual = etl.fromcsv(PopenSource(r'echo foo bar', |
| 66 |
shell=True), |
| 67 |
delimiter=delimiter) |
| 68 |
ieq(expect, actual) |
| 69 |
|
| 70 |
|
| 71 |
def test_zipsource(): |
| 72 |
|
| 73 |
# setup |
| 74 |
tbl = [('foo', 'bar'), ('a', '1'), ('b', '2')] |
| 75 |
fn_tsv = NamedTemporaryFile().name |
| 76 |
etl.totsv(tbl, fn_tsv) |
| 77 |
fn_zip = NamedTemporaryFile().name |
| 78 |
z = zipfile.ZipFile(fn_zip, mode='w') |
| 79 |
z.write(fn_tsv, 'data.tsv') |
| 80 |
z.close() |
| 81 |
|
| 82 |
# test |
| 83 |
actual = etl.fromtsv(ZipSource(fn_zip, 'data.tsv')) |
| 84 |
ieq(tbl, actual) |
| 85 |
|
| 86 |
|
| 87 |
def test_stdoutsource(): |
| 88 |
|
| 89 |
tbl = [('foo', 'bar'), ('a', 1), ('b', 2)] |
| 90 |
etl.tocsv(tbl, StdoutSource(), encoding='ascii') |
| 91 |
etl.tohtml(tbl, StdoutSource(), encoding='ascii') |
| 92 |
etl.topickle(tbl, StdoutSource()) |
| 93 |
|
| 94 |
|
| 95 |
def test_stdoutsource_none(capfd): |
| 96 |
|
| 97 |
tbl = [('foo', 'bar'), ('a', 1), ('b', 2)] |
| 98 |
etl.tocsv(tbl, encoding='ascii') |
| 99 |
captured = capfd.readouterr() |
| 100 |
outp = captured.out |
| 101 |
# TODO: capfd works on vscode but not in console/tox |
| 102 |
if outp: |
| 103 |
assert outp in ( 'foo,bar\r\na,1\r\nb,2\r\n' , 'foo,bar\na,1\nb,2\n' ) |
| 104 |
|
| 105 |
|
| 106 |
def test_stdoutsource_unicode(): |
| 107 |
|
| 108 |
tbl = [('foo', 'bar'), |
| 109 |
(u'Արամ Խաչատրյան', 1), |
| 110 |
(u'Johann Strauß', 2)] |
| 111 |
etl.tocsv(tbl, StdoutSource(), encoding='utf-8') |
| 112 |
etl.tohtml(tbl, StdoutSource(), encoding='utf-8') |
| 113 |
etl.topickle(tbl, StdoutSource()) |
| 114 |
|
| 115 |
|
| 116 |
def test_gzipsource(): |
| 117 |
|
| 118 |
# setup |
| 119 |
tbl = [('foo', 'bar'), ('a', '1'), ('b', '2')] |
| 120 |
fn = NamedTemporaryFile().name + '.gz' |
| 121 |
expect = b"foo,bar\na,1\nb,2\n" |
| 122 |
|
| 123 |
# write explicit |
| 124 |
etl.tocsv(tbl, GzipSource(fn), lineterminator='\n') |
| 125 |
actual = gzip.open(fn).read() |
| 126 |
eq_(expect, actual) |
| 127 |
# write implicit |
| 128 |
etl.tocsv(tbl, fn, lineterminator='\n') |
| 129 |
actual = gzip.open(fn).read() |
| 130 |
eq_(expect, actual) |
| 131 |
|
| 132 |
# read explicit |
| 133 |
tbl2 = etl.fromcsv(GzipSource(fn)) |
| 134 |
ieq(tbl, tbl2) |
| 135 |
# read implicit |
| 136 |
tbl2 = etl.fromcsv(fn) |
| 137 |
ieq(tbl, tbl2) |
| 138 |
|
| 139 |
|
| 140 |
def test_bzip2source(): |
| 141 |
|
| 142 |
# setup |
| 143 |
tbl = [('foo', 'bar'), ('a', '1'), ('b', '2')] |
| 144 |
fn = NamedTemporaryFile().name + '.bz2' |
| 145 |
expect = b"foo,bar\na,1\nb,2\n" |
| 146 |
|
| 147 |
# write explicit |
| 148 |
etl.tocsv(tbl, BZ2Source(fn), lineterminator='\n') |
| 149 |
actual = bz2.BZ2File(fn).read() |
| 150 |
eq_(expect, actual) |
| 151 |
# write implicit |
| 152 |
etl.tocsv(tbl, fn, lineterminator='\n') |
| 153 |
actual = bz2.BZ2File(fn).read() |
| 154 |
eq_(expect, actual) |
| 155 |
|
| 156 |
# read explicit |
| 157 |
tbl2 = etl.fromcsv(BZ2Source(fn)) |
| 158 |
ieq(tbl, tbl2) |
| 159 |
# read implicit |
| 160 |
tbl2 = etl.fromcsv(fn) |
| 161 |
ieq(tbl, tbl2) |