forked from eyeseast/python-tablefu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
757 lines (638 loc) · 22.2 KB
/
test.py
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#! /usr/bin/env python
import csv
import unittest
import urllib2
from table_fu import TableFu
from table_fu.formatting import Formatter
class TableTest(unittest.TestCase):
def setUp(self):
self.csv_file = open('tests/test.csv')
self.table = [['Author', 'Best Book', 'Number of Pages', 'Style'],
['Samuel Beckett', 'Malone Muert', '120', 'Modernism'],
['James Joyce', 'Ulysses', '644', 'Modernism'],
['Nicholson Baker', 'Mezannine', '150', 'Minimalism'],
['Vladimir Sorokin', 'The Queue', '263', 'Satire'],
['Ayn Rand', 'Atlas Shrugged', '1088', 'Science fiction']]
def tearDown(self):
self.csv_file.close()
class BigTableTest(TableTest):
def test_table(self):
"Create a table from an open CSV file"
t = TableFu(self.csv_file)
self.table.pop(0)
self.assertEqual(t.table, self.table)
def test_table_from_list(self):
"Create a table from a two-dimensional list"
t = TableFu(self.table)
self.table.pop(0)
self.assertEqual(t.table, self.table)
def test_table_two_ways(self):
"Two ways to create the same table"
t1 = TableFu(self.csv_file)
t2 = TableFu(self.table)
self.assertEqual(t1.table, t2.table)
class ColumnTest(TableTest):
def test_get_columns(self):
"Get a table's (default) columns"
t = TableFu(self.csv_file)
self.assertEqual(t.columns, self.table[0])
def test_set_columns(self):
"Set new columns for a table"
t = TableFu(self.csv_file)
columns = ['Style', 'Author']
t.columns = columns
self.assertEqual(t.columns, columns)
class HeaderTest(TableTest):
def test_get_headers(self):
"Get the table's headers"
t = TableFu(self.csv_file)
self.assertEqual(t.headers, self.table[0])
class RowTest(TableTest):
def test_count_rows(self):
"Count rows, not including headings"
t = TableFu(self.csv_file)
self.table.pop(0)
self.assertEqual(len(list(t.rows)), len(self.table))
self.assertEqual(len(t), len(self.table))
def test_get_row(self):
"Get one row by slicing the table"
t = TableFu(self.csv_file)
self.assertEqual(t[1], list(t.rows)[1])
def test_check_row(self):
"Check that row numbers are assigned correctly"
t = TableFu(self.csv_file)
self.table.pop(0)
for i, row in enumerate(self.table):
self.assertEqual(
t[i].cells,
self.table[i]
)
class RowColumnTest(TableTest):
def test_limit_columns(self):
"Column definitions are passed to rows"
t = TableFu(self.csv_file)
t.columns = ['Author', 'Style']
self.assertEqual(
str(t[0]),
'Samuel Beckett, Modernism'
)
class DatumTest(TableTest):
def test_get_datum(self):
"Get one cell at a time"
t = TableFu(self.csv_file)
for row in t.rows:
for c in self.table[0]:
self.assertEqual(c, row[c].column_name)
def test_set_datum(self):
"Set a new value for one cell"
t = TableFu(self.csv_file)
modernism = t[0]
modernism['Author'] = "Someone new"
self.assertEqual(str(modernism['Author']), "Someone new")
def test_datum_values(self):
"Ensure every cell has the right value"
t = TableFu(self.csv_file)
columns = self.table.pop(0)
for i, row in enumerate(t.rows):
for index, column in enumerate(columns):
self.assertEqual(
self.table[i][index],
str(row[column])
)
def test_update_values(self):
"Update multiple cell values for a given row"
t = TableFu(self.csv_file)
modernism = t[0]
kerouac = {
'Author': 'Jack Kerouac',
'Best Book': 'On the Road',
'Number of Pages': '320',
'Style': 'Beat'
}
modernism.update(kerouac)
self.assertEqual(
set(kerouac.values()),
set(modernism.cells)
)
def test_datum_equality(self):
"Data are tested on their values"
t = TableFu(self.csv_file)
self.assertEqual(t[0]['Author'], 'Samuel Beckett')
def test_keys(self):
"Get keys for a row, which should match the table's columns"
t = TableFu(self.csv_file)
modernism = t[0]
self.assertEqual(modernism.keys(), t.columns)
def test_values(self):
"Get values for a row"
t = TableFu(self.csv_file)
modernism = t[0]
values = [d.value for d in modernism.data]
self.assertEqual(modernism.values(), values)
def test_items(self):
"Get key-value pairs for a row"
t = TableFu(self.csv_file)
modernism = t[0]
self.assertEqual(
modernism.items(),
zip(modernism.keys(), modernism.values())
)
def test_list_row(self):
"Convert a row back to a list"
t = TableFu(self.csv_file)
modernism = t[0]
self.assertEqual(
list(modernism),
modernism.values()
)
def test_virtual_column(self):
t = TableFu(self.csv_file,formatting={'Combo': { 'filter': 'join', 'args': ['Author', 'Best Book'], 'options': {'delimiter': ',' }}})
for test_row,row in zip(self.table[1:],t.rows):
self.assertEqual(','.join(test_row[:2]), str(row['Combo']))
def test_virtual_columns_cant_be_set(self):
t = TableFu(self.csv_file,columns=['Combo'], formatting={'Combo': { 'filter': 'join', 'args': ['Author', 'Best Book'], 'options': {'delimiter': ',' }}})
row = t.rows.next()
try:
row['Combo'] = "this should never work"
self.fail()
except ValueError:
pass
class ErrorTest(TableTest):
def test_bad_key(self):
"Non-existent columns raise a KeyError"
t = TableFu(self.csv_file)
for row in t.rows:
self.assertRaises(
KeyError,
row.__getitem__,
'not-a-key'
)
def test_bad_total(self):
"Only number-like fields can be totaled"
t = TableFu(self.csv_file)
self.assertRaises(ValueError, t.total, 'Author')
class SortTest(TableTest):
def test_sort(self):
"Sort a table in place"
t = TableFu(self.csv_file)
self.table.pop(0)
self.table.sort(key=lambda row: row[0])
t.sort('Author')
self.assertEqual(
t[0].cells,
self.table[0]
)
class ValuesTest(TableTest):
def test_values(self):
"Return one column's values for all rows"
t = TableFu(self.csv_file)
self.table.pop(0)
authors = [row[0] for row in self.table]
self.assertEqual(authors, t.values('Author'))
def test_unique_values(self):
"Adding unique=True returns a set"
t = TableFu(self.table)
self.table.pop(0)
styles = set([row[-1] for row in self.table])
self.assertEqual(t.values('Style', unique=True), styles)
def test_totals(self):
"Total values for a table across rows"
t = TableFu(self.csv_file)
self.table.pop(0)
pages = sum([float(row[2]) for row in self.table])
self.assertEqual(pages, t.total('Number of Pages'))
def test_formatting_takes_priority(self):
t = TableFu(self.csv_file,formatting={'Author': { 'filter': 'template', 'args': ['Author'], 'options': {'template': '*%s*'}}})
for v in t.values('Author'):
self.assertEqual('*',v[0])
self.assertEqual('*',v[-1])
class FacetTest(TableTest):
def test_facet(self):
"Facet tables based on shared column values"
t = TableFu(self.csv_file)
tables = t.facet_by('Style')
style_row = self.table[4]
self.assertEqual(
style_row,
tables[2][0].cells
)
class FilterTest(TableTest):
def test_count(self):
"Count is like len()"
t = TableFu(self.csv_file)
self.assertEqual(len(t), t.count())
def test_filter(self):
"Filtering returns a new TableFu instance"
t = TableFu(self.csv_file)
f = t.filter(Author='Samuel Beckett')
self.assertEqual(type(t), type(f))
self.assertEqual(t.columns, f.columns)
def test_simple_filter(self):
"Filter by keyword args"
t = TableFu(self.csv_file)
f = t.filter(Author='Samuel Beckett')
self.assertEqual(f[0].cells, self.table[1])
def test_multi_filter(self):
"Filter by multiple keywords"
t = TableFu(self.csv_file)
f = t.filter(Style='Modernism', Author='Samuel Beckett')
self.assertEqual(f[0].cells, self.table[1])
def test_big_filter(self):
arra = open('tests/arra.csv')
t = TableFu(arra)
f = t.filter(State='ALABAMA', County='COLBERT')
self.assertEqual(f.count(), 5)
class OptionsTest(TableTest):
def test_sort_option_str(self):
"Sort the table by a string field, Author"
t = TableFu(self.csv_file, sorted_by={"Author": {'reverse': True}})
self.table.pop(0)
self.table.sort(key=lambda row: row[0], reverse=True)
self.assertEqual(t[0].cells, self.table[0])
def test_sort_option_int(self):
"Sorting the table by an int field, Number of Pages"
t = TableFu(self.csv_file)
pages = t.values('Number of Pages')
pages = sorted(pages, reverse=True)
t.sort('Number of Pages', reverse=True)
self.assertEqual(t.values('Number of Pages'), pages)
class DatumFormatTest(TableTest):
def setUp(self):
self.csv_file = open('tests/sites.csv')
def test_cell_format(self):
"Format a cell"
t = TableFu(self.csv_file)
t.formatting = {'Name': {
'filter': 'link',
'args': ['URL']
}
}
self.assertEqual(
str(t[0]['Name']),
'<a href="http://www.chrisamico.com" title="ChrisAmico.com">ChrisAmico.com</a>'
)
class HTMLTest(TableTest):
def test_datum_td(self):
"Output a cell as a <td> element"
t = TableFu(self.csv_file)
beckett = t[0]['Author']
self.assertEqual(
beckett.as_td(),
'<td style="" class="datum">Samuel Beckett</td>'
)
def test_row_tr(self):
"Output a row as a <tr> element"
t = TableFu(self.csv_file)
row = t[0]
self.assertEqual(
row.as_tr(),
'<tr id="row0" class="row even"><td style="" class="datum">Samuel Beckett</td><td style="" class="datum">Malone Muert</td><td style="" class="datum">120</td><td style="" class="datum">Modernism</td></tr>'
)
def test_header_th(self):
t = TableFu(self.csv_file)
hed = t.headers[0]
self.assertEqual(hed.as_th(), '<th style="" class="header">Author</th>')
class StyleTest(TableTest):
def test_datum_style(self):
t = TableFu(self.csv_file, style={'Author': 'text-align:left;'})
beckett = t[0]['Author']
self.assertEqual(beckett.style, 'text-align:left;')
def test_datum_td_style(self):
t = TableFu(self.csv_file, style={'Author': 'text-align:left;'})
beckett = t[0]['Author']
self.assertEqual(
beckett.as_td(),
'<td style="text-align:left;" class="datum">Samuel Beckett</td>'
)
def test_header_style(self):
t = TableFu(self.csv_file, style={'Author': 'text-align:left;'})
hed = t.headers[0]
self.assertEqual(hed.style, 'text-align:left;')
def test_header_th_style(self):
t = TableFu(self.csv_file, style={'Author': 'text-align:left;'})
hed = t.headers[0]
self.assertEqual(
hed.as_th(),
'<th style="text-align:left;" class="header">Author</th>'
)
class OutputTest(TableTest):
def setUp(self):
self.csv_file = open('tests/arra.csv')
def tearDown(self):
self.csv_file.close()
def test_csv(self):
t = TableFu(self.csv_file)
self.csv_file.seek(0)
for test, control in zip(t.csv(), self.csv_file.readline()):
self.assertEqual(test.strip(), control.strip()) # controlling for newlines
def test_json(self):
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
return
t = TableFu(self.csv_file)
self.csv_file.seek(0)
reader = csv.DictReader(self.csv_file)
jsoned = json.dumps([row for row in reader])
self.assertEqual(t.json(), jsoned)
def test_python(self):
t = TableFu(self.csv_file)
self.csv_file.seek(0)
reader = csv.DictReader(self.csv_file)
jsoned = [row for row in reader]
self.assertEqual(list(t.dict()), jsoned)
class ManipulationTest(TableTest):
def test_transpose(self):
t = TableFu(self.table)
result = [
['Author', 'Samuel Beckett', 'James Joyce', 'Nicholson Baker', 'Vladimir Sorokin', 'Ayn Rand'],
['Best Book', 'Malone Muert', 'Ulysses', 'Mezannine', 'The Queue', 'Atlas Shrugged'],
['Number of Pages', '120', '644', '150', '263', '1088'],
['Style', 'Modernism', 'Modernism', 'Minimalism', 'Satire', 'Science fiction']
]
transposed = t.transpose()
self.assertEqual(transposed.table, result[1:])
self.assertEqual(transposed.columns, [
'Author',
'Samuel Beckett',
'James Joyce',
'Nicholson Baker',
'Vladimir Sorokin',
'Ayn Rand',
])
def test_row_map(self):
"""
Test map a function to rows, or a subset of fields
"""
t = TableFu(self.table)
result = [s.lower() for s in t.values('Style')]
self.assertEqual(result, t.map(lambda row: row['Style'].value.lower()))
def test_map_values(self):
"""
Test mapping a function to specific column values
"""
t = TableFu(self.table)
result = [s.lower() for s in t.values('Style')]
self.assertEqual(result, t.map(str.lower, 'Style'))
def test_map_many_values(self):
"""
Test mapping a function to multiple columns
"""
t = TableFu(self.table)
result = [
[s.lower() for s in t.values(value)]
for value in ['Best Book', 'Style']
]
self.assertEqual(result, t.map(str.lower, 'Best Book', 'Style'))
class FormatTest(unittest.TestCase):
def setUp(self):
self.format = Formatter()
class RegisterTest(FormatTest):
def test_register(self):
"Register a new format function"
def test(value, *args):
args = list(args)
args.insert(0, value)
return args
self.format.register(test)
self.assertEqual(test, self.format._filters['test'])
def test_intcomma(self):
"Use intcomma for nicer number formatting"
self.assertEqual(
self.format(1200, 'intcomma'),
'1,200'
)
def test_ap_state(self):
"Return AP state style of a state"
self.assertEqual(
self.format(6, 'ap_state'),
'Calif.'
)
self.assertEqual(
self.format('California', 'ap_state'),
'Calif.'
)
self.assertEqual(
self.format('CA', 'ap_state'),
'Calif.'
)
self.assertEqual(
self.format('California', 'ap_state'),
'Calif.'
)
self.assertEqual(
self.format('foo', 'ap_state'),
'foo'
)
self.assertEqual(
self.format('foo', 'ap_state', failure_string='bar'),
'bar'
)
def test_capfirst(self):
"Returns a string with only the first character capitalized"
self.assertEqual(
self.format('ALLCAPS', 'capfirst'),
'Allcaps'
)
self.assertEqual(
self.format('whisper', 'capfirst'),
'Whisper'
)
self.assertEqual(
self.format('CaMeLcAsE', 'capfirst'),
'Camelcase'
)
self.assertEqual(
self.format('', 'capfirst'),
'N/A'
)
self.assertEqual(
self.format(1, 'capfirst'),
'N/A'
)
self.assertEqual(
self.format(1, 'capfirst', failure_string='bar'),
'bar'
)
def test_dollar_signs(self):
"Converts an integer into the corresponding number of dollar sign symbols."
self.assertEqual(
self.format(1, 'dollar_signs'),
'$'
)
self.assertEqual(
self.format(5, 'dollar_signs'),
'$$$$$'
)
self.assertEqual(
self.format('foo', 'dollar_signs'),
'N/A'
)
self.assertEqual(
self.format('foo', 'dollar_signs', failure_string='bar'),
'bar'
)
def test_image(self):
"Returns an HTML image tag"
self.assertEqual(
self.format('http://lorempixel.com/400/200/', 'image'),
'<img src="http://lorempixel.com/400/200/" style="">'
)
def test_percentage(self):
"Converts a floating point value into a percentage value."
self.assertEqual(
self.format(0.02, 'percentage'),
'2.0%'
)
self.assertEqual(
self.format(0.10560, 'percentage'),
'10.6%'
)
self.assertEqual(
self.format(0.10560, 'percentage', multiply=False),
'0.1%'
)
self.assertEqual(
self.format(0.10560, 'percentage', decimal_places=3),
'10.560%'
)
self.assertEqual(
self.format('foo', 'percentage'),
'N/A'
)
self.assertEqual(
self.format('foo', 'percentage', failure_string='bar'),
'bar'
)
def test_percent_change(self):
"Converts a floating point value into a percentage change value."
self.assertEqual(
self.format(0.02, 'percent_change'),
'+2.0%'
)
self.assertEqual(
self.format(-0.10560, 'percent_change'),
'-10.6%'
)
self.assertEqual(
self.format(-0.10560, 'percent_change', multiply=False),
'-0.1%'
)
self.assertEqual(
self.format(-0.10560, 'percent_change', decimal_places=3),
'-10.560%'
)
self.assertEqual(
self.format('foo', 'percent_change'),
'N/A'
)
self.assertEqual(
self.format('foo', 'percent_change', failure_string='bar'),
'bar'
)
def test_ratio(self):
"Converts a floating point value a X:1 ratio."
self.assertEqual(
self.format(1, 'ratio'),
'1:1'
)
self.assertEqual(
self.format(2, 'ratio'),
'2:1'
)
self.assertEqual(
self.format(2.2, 'ratio'),
'2:1'
)
self.assertEqual(
self.format('foo', 'ratio'),
'N/A'
)
self.assertEqual(
self.format('foo', 'ratio', failure_string='bar'),
'bar'
)
def test_stateface(self):
"Returns ProPublica stateface"
self.assertEqual(
self.format('California', 'stateface'),
'E'
)
self.assertEqual(
self.format('Wyo.', 'stateface'),
'x'
)
def test_state_postal(self):
"Returns a state's postal code"
self.assertEqual(
self.format('California', 'state_postal'),
'CA'
)
self.assertEqual(
self.format('Wyo.', 'state_postal'),
'WY'
)
self.assertEqual(
self.format('foo', 'state_postal'),
'foo'
)
def test_title(self):
"Converts a string into titlecase."
self.assertEqual(
self.format('ALLCAPS YO', 'title'),
'Allcaps Yo'
)
self.assertEqual(
self.format('whisper ing', 'title'),
'Whisper Ing'
)
self.assertEqual(
self.format('CaMeLcAsE', 'title'),
'Camelcase'
)
self.assertEqual(
self.format('', 'title'),
'N/A'
)
self.assertEqual(
self.format(1, 'title'),
'N/A'
)
class OpenerTest(unittest.TestCase):
def test_from_file(self):
t1 = TableFu.from_file('tests/arra.csv')
t2 = TableFu(open('tests/arra.csv'))
self.assertEqual(t1.table, t2.table)
def test_from_url(self):
url = "http://spreadsheets.google.com/pub?key=thJa_BvqQuNdaFfFJMMII0Q&output=csv"
t1 = TableFu.from_url(url)
t2 = TableFu(urllib2.urlopen(url))
self.assertEqual(t1.table, t2.table)
class RemoteTest(unittest.TestCase):
def test_use_url(self):
"Use a response from urllib2.urlopen as our base file"
url = "http://spreadsheets.google.com/pub?key=thJa_BvqQuNdaFfFJMMII0Q&output=csv"
response1 = urllib2.urlopen(url)
response2 = urllib2.urlopen(url)
reader = csv.reader(response1)
columns = reader.next()
t = TableFu(response2)
self.assertEqual(columns, t.columns)
class UpdateTest(TableTest):
"""
Tests for things that update or otherwise transform data
"""
def test_transform_to_int(self):
"""
Convert the Number of Pages field to integers
"""
t = TableFu(self.csv_file)
pages = t.values('Number of Pages')
t.transform('Number of Pages', int)
for s, i in zip(pages, t.values('Number of Pages')):
self.assertEqual(int(s), i)
if __name__ == '__main__':
unittest.main()