Codebase list pyexcel / master tests / test_bug_fixes.py
master

Tree @master (Download .tar.gz)

test_bug_fixes.py @masterraw · history · blame

  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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from datetime import datetime
from textwrap import dedent

import pyexcel as p
from _compact import StringIO, OrderedDict

import psutil
from nose.tools import eq_


def test_bug_01():
    """
    if first row of csv is shorter than the rest of the rows,
    the csv will be truncated by first row. This is a bug

    "a,d,e,f" <- this will be 1
    '1',2,3,4 <- 4
    '2',3,4,5
    'b'       <- give '' for missing cells
    """
    r = p.Reader(os.path.join("tests", "fixtures", "bug_01.csv"))
    assert len(r.row[0]) == 4
    # test "" is append for empty cells
    assert r[0, 1] == ""
    assert r[3, 1] == ""


def test_issue_03():
    file_prefix = "issue_03_test"
    csv_file = "%s.csv" % file_prefix
    xls_file = "%s.xls" % file_prefix
    my_sheet_name = "mysheetname"
    data = [[1, 1]]
    sheet = p.Sheet(data, name=my_sheet_name)
    sheet.save_as(csv_file)
    assert os.path.exists(csv_file)
    sheet.save_as(xls_file)
    book = p.load_book(xls_file)
    assert book.sheet_names()[0] == my_sheet_name
    os.unlink(csv_file)
    os.unlink(xls_file)


def test_issue_06():
    import logging

    logger = logging.getLogger("test")
    logger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    logger.addHandler(ch)
    output = StringIO()
    book = p.Book({"hoja1": [["datos", "de", "prueba"], [1, 2, 3]]})
    book.save_to_memory("csv", output)
    logger.debug(output.getvalue())


def test_issue_09():
    p.book.LOCAL_UUID = 0
    merged = p.Book()
    sheet1 = p.Sheet(sheet=[[1, 2]])
    sheet2 = p.Sheet(sheet=[[1, 2]])
    merged += sheet1
    merged += sheet2
    eq_(merged[1].name, "pyexcel sheet_1")


def test_issue_10():
    thedict = OrderedDict()
    thedict.update({"Column 1": [1, 2, 3]})
    thedict.update({"Column 2": [1, 2, 3]})
    thedict.update({"Column 3": [1, 2, 3]})
    p.save_as(adict=thedict, dest_file_name="issue10.xls")
    newdict = p.get_dict(file_name="issue10.xls")
    assert isinstance(newdict, OrderedDict) is True
    eq_(thedict, newdict)
    os.unlink("issue10.xls")


def test_issue_29():
    a = [
        # error case
        ["2016-03-31 10:59", "0123", "XS360_EU", "04566651561653122"],
        #  python types
        [datetime(2016, 4, 15, 17, 52, 11), 123, False, 456193284757],
    ]
    s = p.get_sheet(array=a)
    content = dedent(
        """
    pyexcel_sheet1:
    +------------------+------+----------+-------------------+
    | 2016-03-31 10:59 | 0123 | XS360_EU | 04566651561653122 |
    +------------------+------+----------+-------------------+
    | 15/04/16         | 123  | false    | 456193284757      |
    +------------------+------+----------+-------------------+"""
    )
    eq_(str(s), content.strip("\n"))


def test_issue_29_nominablesheet():
    a = [
        ["date", "number", "misc", "long number"],
        # error case
        ["2016-03-31 10:59", "0123", "XS360_EU", "04566651561653122"],
        #  python types
        [datetime(2016, 4, 15, 17, 52, 11), 123, False, 456193284757],
    ]
    s = p.get_sheet(array=a)
    s.name_columns_by_row(0)
    content = dedent(
        """
    pyexcel_sheet1:
    +------------------+--------+----------+-------------------+
    |       date       | number |   misc   |    long number    |
    +==================+========+==========+===================+
    | 2016-03-31 10:59 | 0123   | XS360_EU | 04566651561653122 |
    +------------------+--------+----------+-------------------+
    | 15/04/16         | 123    | false    | 456193284757      |
    +------------------+--------+----------+-------------------+"""
    )
    eq_(str(s), content.strip("\n"))


def test_issue_51_orderred_dict_in_records():
    from pyexcel.plugins.sources.pydata.records import RecordsReader

    records = []
    orderred_dict = OrderedDict()
    orderred_dict.update({"Zebra": 10})
    orderred_dict.update({"Hippo": 9})
    orderred_dict.update({"Monkey": 8})
    records.append(orderred_dict)
    orderred_dict2 = OrderedDict()
    orderred_dict2.update({"Zebra": 1})
    orderred_dict2.update({"Hippo": 2})
    orderred_dict2.update({"Monkey": 3})
    records.append(orderred_dict2)
    records_reader = RecordsReader(records)
    array = list(records_reader.to_array())
    expected = [["Zebra", "Hippo", "Monkey"], [10, 9, 8], [1, 2, 3]]
    eq_(array, expected)


def test_issue_51_normal_dict_in_records():
    from pyexcel.plugins.sources.pydata.records import RecordsReader

    records = []
    orderred_dict = {}
    orderred_dict.update({"Zebra": 10})
    orderred_dict.update({"Hippo": 9})
    orderred_dict.update({"Monkey": 8})
    records.append(orderred_dict)
    orderred_dict2 = {}
    orderred_dict2.update({"Zebra": 1})
    orderred_dict2.update({"Hippo": 2})
    orderred_dict2.update({"Monkey": 3})
    records.append(orderred_dict2)
    records_reader = RecordsReader(records)
    array = list(records_reader.to_array())
    expected = [["Hippo", "Monkey", "Zebra"], [9, 8, 10], [2, 3, 1]]
    eq_(array, expected)


def test_issue_55_unicode_in_headers():
    headers = [u"Äkkilähdöt", u"Matkakirjoituksia", u"Matkatoimistot"]
    content = [headers, [1, 2, 3]]
    sheet = p.Sheet(content)
    sheet.name_columns_by_row(0)
    eq_(sheet.colnames, headers)


def test_issue_60_chinese_text_in_python_2_stdout():
    import sys

    data = [["这", "是", "中", "文"], ["这", "是", "中", "文"]]
    sheet = p.Sheet(data)
    sys.stdout.write(repr(sheet))


def test_issue_60_chinese_text_in_python_2_stdout_on_book():
    import sys

    adict = {"Sheet 1": [["这", "是", "中", "文"], ["这", "是", "中", "文"]]}
    book = p.Book()
    book.bookdict = adict
    sys.stdout.write(repr(book))


def test_issue_63_empty_array_crash_texttable_renderer():
    sheet = p.Sheet([])
    print(sheet)


def test_xls_issue_11():
    data = [[1, 2]]
    sheet = p.Sheet(data)
    sheet2 = p.get_sheet(file_content=sheet.xls, file_type="XLS")
    eq_(sheet.array, sheet2.array)
    test_file = "xls_issue_11.JSON"
    sheet2.save_as(test_file)
    os.unlink(test_file)


def test_issue_68():
    data = [[1]]
    sheet = p.Sheet(data)
    stream = sheet.save_to_memory("csv")
    eq_(stream.read(), "1\r\n")
    data = {"sheet": [[1]]}
    book = p.Book(data)
    stream = book.save_to_memory("csv")
    eq_(stream.read(), "1\r\n")


def test_issue_74():
    from decimal import Decimal

    data = [[Decimal("1.1")]]
    sheet = p.Sheet(data)
    table = sheet.texttable
    expected = "pyexcel sheet:\n+-----+\n| 1.1 |\n+-----+"
    eq_(table, expected)


def test_issue_76():
    from pyexcel._compact import StringIO

    tsv_stream = StringIO()
    tsv_stream.write("1\t2\t3\t4\n")
    tsv_stream.write("1\t2\t3\t4\n")
    tsv_stream.seek(0)
    sheet = p.get_sheet(
        file_stream=tsv_stream, file_type="csv", delimiter="\t"
    )
    data = [[1, 2, 3, 4], [1, 2, 3, 4]]
    eq_(sheet.array, data)


def test_issue_83_csv_file_handle():
    proc = psutil.Process()
    test_file = os.path.join("tests", "fixtures", "bug_01.csv")
    open_files_l1 = proc.open_files()

    # start with a csv file
    data = p.iget_array(file_name=test_file)
    open_files_l2 = proc.open_files()
    delta = len(open_files_l2) - len(open_files_l1)
    # interestingly, no open file handle yet
    assert delta == 0

    # now the file handle get opened when we run through
    # the generator
    list(data)
    open_files_l3 = proc.open_files()
    delta = len(open_files_l3) - len(open_files_l1)
    # caught an open file handle, the "fish" finally
    assert delta == 1

    # free the fish
    p.free_resources()
    open_files_l4 = proc.open_files()
    # this confirms that no more open file handle
    eq_(open_files_l1, open_files_l4)


def test_issue_83_file_handle_no_generator():
    proc = psutil.Process()
    test_files = [
        os.path.join("tests", "fixtures", "bug_01.csv"),
        os.path.join("tests", "fixtures", "test-single.csvz"),
        os.path.join("tests", "fixtures", "date_field.xls"),
    ]
    for test_file in test_files:
        open_files_l1 = proc.open_files()
        # start with a csv file
        p.get_array(file_name=test_file)
        open_files_l2 = proc.open_files()
        delta = len(open_files_l2) - len(open_files_l1)
        # no open file handle should be left
        assert delta == 0


def test_issue_83_csvz_file_handle():
    proc = psutil.Process()
    test_file = os.path.join("tests", "fixtures", "test-single.csvz")
    open_files_l1 = proc.open_files()

    # start with a csv file
    data = p.iget_array(file_name=test_file)
    open_files_l2 = proc.open_files()
    delta = len(open_files_l2) - len(open_files_l1)
    # interestingly, file is already open :)
    assert delta == 1

    # now the file handle get opened when we run through
    # the generator
    list(data)
    open_files_l3 = proc.open_files()
    delta = len(open_files_l3) - len(open_files_l1)
    # caught an open file handle, the "fish" finally
    assert delta == 1

    # free the fish
    p.free_resources()
    open_files_l4 = proc.open_files()
    # this confirms that no more open file handle
    eq_(open_files_l1, open_files_l4)


def test_issue_83_xls_file_handle():
    proc = psutil.Process()
    test_file = os.path.join("tests", "fixtures", "date_field.xls")
    open_files_l1 = proc.open_files()

    # start with a csv file
    data = p.iget_array(file_name=test_file)
    open_files_l2 = proc.open_files()
    delta = len(open_files_l2) - len(open_files_l1)
    # interestingly, no open file using xlrd
    assert delta == 0

    # now the file handle get opened when we run through
    # the generator
    list(data)
    open_files_l3 = proc.open_files()
    delta = len(open_files_l3) - len(open_files_l1)
    # still no open file
    assert delta == 0

    p.free_resources()
    open_files_l4 = proc.open_files()
    eq_(open_files_l1, open_files_l4)


def test_issue_92_non_uniform_records():
    records = [{"a": 1}, {"b": 2}, {"c": 3}]
    sheet = p.get_sheet(records=records, custom_headers=["a", "b", "c"])
    content = dedent(
        """
    +---+---+---+
    | a | b | c |
    +---+---+---+
    | 1 |   |   |
    +---+---+---+
    |   | 2 |   |
    +---+---+---+
    |   |   | 3 |
    +---+---+---+"""
    ).strip("\n")
    eq_(str(sheet.content), content)


def test_issue_92_incomplete_records():
    records = [{"a": 1, "b": 2, "c": 3}, {"b": 2}, {"c": 3}]
    sheet = p.get_sheet(records=records)
    content = dedent(
        """
    +---+---+---+
    | a | b | c |
    +---+---+---+
    | 1 | 2 | 3 |
    +---+---+---+
    |   | 2 |   |
    +---+---+---+
    |   |   | 3 |
    +---+---+---+"""
    ).strip("\n")
    eq_(str(sheet.content), content)


def test_issue_92_verify_save_as():
    records = [{"a": 1, "b": 2, "c": 3}, {"b": 2}, {"c": 3}]
    csv_io = p.save_as(records=records, dest_file_type="csv")
    content = "a,b,c\r\n1,2,3\r\n,2,\r\n,,3\r\n"
    eq_(csv_io.getvalue(), content)


def test_issue_95_preserve_order_in_iget_orders():
    test_data = [["a", "b", "c"], ["1", "2", "3"], ["4", "5", "6"]]

    records = p.iget_records(array=test_data)
    result = []
    for record in records:
        for key, value in record.items():
            result.append([key, value])

    expected = [
        ["a", "1"],
        ["b", "2"],
        ["c", "3"],
        ["a", "4"],
        ["b", "5"],
        ["c", "6"],
    ]
    eq_(result, expected)


def test_issue_95_preserve_custom_order_in_iget_orders():
    test_data = [["a", "b", "c"], ["1", "2", "3"], ["4", "5", "6"]]

    records = p.iget_records(array=test_data, custom_headers=["c", "a", "b"])
    result = []
    for record in records:
        for key, value in record.items():
            result.append([key, value])

    expected = [
        ["c", "3"],
        ["a", "1"],
        ["b", "2"],
        ["c", "6"],
        ["a", "4"],
        ["b", "5"],
    ]
    eq_(result, expected)


def test_issue_95_preserve_order_in_get_orders():
    test_data = [["a", "b", "c"], ["1", "2", "3"], ["4", "5", "6"]]

    records = p.get_records(array=test_data)
    result = []
    for record in records:
        for key, value in record.items():
            result.append([key, value])

    expected = [
        ["a", "1"],
        ["b", "2"],
        ["c", "3"],
        ["a", "4"],
        ["b", "5"],
        ["c", "6"],
    ]
    eq_(result, expected)


def test_issue_100():
    data = [["a", "b"]]
    sheet = p.Sheet(data)
    sheet.name_columns_by_row(0)
    eq_(sheet.to_dict(), {"a": [], "b": []})


def test_issue_125():
    book = p.Book()
    book += p.Sheet([[1]], "A")
    book += p.Sheet([[2]], "B")
    eq_(book.sheet_names(), ["A", "B"])
    book.sort_sheets(reverse=True)
    eq_(book.sheet_names(), ["B", "A"])


def test_issue_125_saving_the_order():
    test_file = "issue_125.xls"
    book = p.Book()
    book += p.Sheet([[1]], "A")
    book += p.Sheet([[2]], "B")
    eq_(book.sheet_names(), ["A", "B"])
    book.sort_sheets(reverse=True)
    book.save_as(test_file)
    book2 = p.get_book(file_name=test_file)
    eq_(book2.sheet_names(), ["B", "A"])
    os.unlink(test_file)


def test_issue_125_using_key():
    test_file = "issue_125.xls"
    book = p.Book()
    book += p.Sheet([[1]], "A")
    book += p.Sheet([[2]], "B")
    book += p.Sheet([[3]], "C")

    custom_order = {"A": 1, "B": 3, "C": 2}
    book.sort_sheets(key=lambda x: custom_order[x])
    book.save_as(test_file)
    book2 = p.get_book(file_name=test_file)
    eq_(book2.sheet_names(), ["A", "C", "B"])
    os.unlink(test_file)


def test_issue_126():
    data = [[1]]
    test_file = "issue_126.xls"
    test_name = "doyoufindme"
    p.save_as(array=data, dest_file_name=test_file, dest_sheet_name=test_name)
    sheet = p.get_sheet(file_name=test_file)
    eq_(sheet.name, test_name)
    os.unlink(test_file)


def test_issue_126_isave_as():
    data = [[1]]
    test_file = "issue_126.xls"
    test_name = "doyoufindme"
    p.isave_as(array=data, dest_file_name=test_file, dest_sheet_name=test_name)
    sheet = p.get_sheet(file_name=test_file)
    eq_(sheet.name, test_name)
    os.unlink(test_file)


def test_pyexcel_issue_138():
    sheet = p.Sheet()
    sheet.csv = "123_122,"
    eq_(sheet[0, 0], "123_122")


def test_pyexcel_issue_140():
    TestSheet1 = p.Sheet()
    TestSheet1[4, 4] = "4x4"
    TestSheet1[0, 0] = "0,0"
    expected = [
        ["0,0", "", "", "", ""],
        ["", "", "", "", ""],
        ["", "", "", "", ""],
        ["", "", "", "", ""],
        ["", "", "", "", "4x4"],
    ]
    eq_(expected, TestSheet1.to_array())


def test_pyexcel_issue_176():
    sheet = p.get_sheet(
        file_name=os.path.join("tests", "fixtures", "bug_176.xlsx")
    )
    eq_("<No data>", sheet.name)
    eq_([[]], sheet.array)


def test_pyexcel_issue_176_get_book():
    book = p.get_book(
        file_name=os.path.join("tests", "fixtures", "bug_176.xlsx")
    )
    eq_({}, book.bookdict)