-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathtest_tables.py
1963 lines (1682 loc) · 54.6 KB
/
test_tables.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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import doctest
import re
import pytest
import warnings
import numpy as np
from numpy.testing import assert_array_equal
from datascience import *
import pandas as pd
from io import BytesIO
#########
# Utils #
#########
@pytest.fixture(scope='function')
def table():
"""Setup Scrabble table"""
return Table().with_columns([
'letter', ['a', 'b', 'c', 'z'],
'count', [9, 3, 3, 1],
'points', [1, 2, 2, 10],
])
@pytest.fixture(scope='function')
def table2():
"""Setup second table"""
return Table().with_columns([
['points', (1, 2, 3)],
['names', ('one', 'two', 'three')],
])
@pytest.fixture(scope='function')
def table3():
"""Setup third table; same labels as first but in a different order."""
return Table().with_columns([
'count', [0, 54, 5],
'points', [3, 10, 24],
'letter', ['x', 'y', 'z'],
])
@pytest.fixture(scope='function')
def table4():
"""Setup fourth table; three overlapping columns with table."""
return Table().with_columns([
'letter', ['a', 'b', 'c', '8', 'a'],
'count', [9, 3, 2, 0, 9],
'different label', [1, 4, 2, 1, 1],
'name', ['Gamma', 'Delta', 'Epsilon', 'Alpha', 'Beta']
])
@pytest.fixture(scope='function')
def table5():
"""Setup fifth table; has NaNs in it"""
return Table().with_columns([
'letter', ['a', 'b', 'c', 'd', 'y', 'z'],
'count', [9, 3, 3, 4, 2, 1],
'points', [1, 2, 2, 2, 4, 10],
'person_id', [np.float64('nan'), np.float64('nan'), np.float64('nan'), 1, 2, 3]
])
@pytest.fixture(scope='function')
def numbers_table():
"""Setup table containing only numbers"""
return Table().with_columns([
'count', [9, 3, 3, 1],
'points', [1, 2, 2, 10],
])
@pytest.fixture(scope='function')
def categories_table():
"""Setup a table with a column to serve as pivot keys and
a columns of values to bin for each key."""
return Table(['key', 'val']).with_rows([
['a', 1],
['a', 1],
['a', 2],
['b', 1],
['b', 2],
['b', 2]])
@pytest.fixture(scope='module')
def t():
"""Create one table for entire module"""
return table()
@pytest.fixture(scope='module')
def u():
"""Setup second alphanumeric table"""
return table2()
@pytest.fixture(scope='function')
def scrabble_table2():
"""Setup Scrabble table"""
return Table().with_columns([
'letter', ['a', 'b', 'c', 'z'],
'count', [9, 3, 3, 1],
'count_2', [9, 3, 3, 1],
'pointsplus1', [2, 3, 3, 11],
])
def assert_equal(string1, string2):
string1, string2 = str(string1), str(string2)
whitespace = re.compile(r'\s')
purify = lambda s: whitespace.sub('', s)
assert purify(string1) == purify(string2), "\n%s\n!=\n%s" % (string1, string2)
############
# Doctests #
############
def test_doctests():
results = doctest.testmod(tables, optionflags=doctest.NORMALIZE_WHITESPACE)
assert results.failed == 0
############
# Overview #
############
def test_basic(table):
"""Tests that t works"""
t = table
assert_equal(t, """
letter | count | points
a | 9 | 1
b | 3 | 2
c | 3 | 2
z | 1 | 10
""")
def test_column(table):
"""Test table.values()"""
t = table
assert_array_equal(t.column('letter'), np.array(['a', 'b', 'c', 'z']))
assert_array_equal(t.column(1), np.array([9, 3, 3, 1]))
with pytest.raises(ValueError):
t.column(-1)
with pytest.raises(ValueError):
t.column('abc')
def test_values():
t1 = Table().with_columns({
'row1': ['a', 'b', 'c'],
'row2': ['d', 'e', 'f'],
})
assert_array_equal(t1.values, np.array(t1.columns, None).T)
t2 = Table().with_columns({
'row1': ['x', 'y', 'z'],
'row2': [1, 2, 3],
})
assert_array_equal(t2.values, np.array(t2.columns, object).T)
def test_basic_points(table):
t = table
assert_array_equal(t['points'], np.array([1, 2, 2, 10]))
def test_basic_rows(table):
t = table
assert_equal(
t.rows[2],
"Row(letter='c', count=3, points=2)")
def test_row_conversion_to_np_array(table):
t = table
t_subset = t.select("count", "points")
assert_array_equal(np.array(t_subset.row(0)), np.array([9, 1]))
def test_select(table):
t = table
test = t.select('points', 1)
assert_equal(test, """
points | count
1 | 9
2 | 3
2 | 3
10 | 1
""")
def test_drop(table):
t = table
test = t.drop(['points', 1])
assert_equal(test, """
letter
a
b
c
z
""")
def test_take(table):
t = table
test = t.take([1, 2])
assert_equal(test, """
letter | count | points
b | 3 | 2
c | 3 | 2
""")
def test_take_slice(table):
t = table
test = t.take[1:3]
assert_equal(test, """
letter | count | points
b | 3 | 2
c | 3 | 2
""")
def test_take_slice_single(table):
t = table
test = t.take[1]
assert_equal(test, """
letter | count | points
b | 3 | 2
""")
def test_take_iterable(table):
t = table
test = t.take[0, 2]
assert_equal(test, """
letter | count | points
a | 9 | 1
c | 3 | 2
""")
def test_take_floating_args(table):
t = table
test = t.take(0, 2)
assert_equal(test, """
letter | count | points
a | 9 | 1
c | 3 | 2
""")
def test_exclude(table):
t = table
test = t.exclude([1, 3])
assert_equal(test, """
letter | count | points
a | 9 | 1
c | 3 | 2
""")
def test_exclude_slice(table):
t = table
test = t.exclude[1:3]
assert_equal(test, """
letter | count | points
a | 9 | 1
z | 1 | 10
""")
def test_exclude_slice_single(table):
t = table
test = t.exclude[1]
assert_equal(test, """
letter | count | points
a | 9 | 1
c | 3 | 2
z | 1 | 10
""")
def test_exclude_iterable(table):
t = table
test = t.exclude[0, 2]
assert_equal(test, """
letter | count | points
b | 3 | 2
z | 1 | 10
""")
def test_exclude_floating_args(table):
t = table
test = t.exclude(1, 3)
assert_equal(test, """
letter | count | points
a | 9 | 1
c | 3 | 2
""")
def test_stats(table):
t = table
test = t.stats()
assert_equal(test, """
statistic | letter | count | points
min | a | 1 | 1
max | z | 9 | 10
median | | 3 | 2
sum | | 16 | 15
""")
def test_stats_with_numpy(table):
t = table
test = t.stats([np.mean, np.std, np.var])
assert_equal(test, """
statistic | letter | count | points
mean | | 4 | 3.75
std | | 3 | 3.63146
var | | 9 | 13.1875""")
def test_where(table):
t = table
test = t.where('points', 2)
assert_equal(test, """
letter | count | points
b | 3 | 2
c | 3 | 2
""")
test = t.where(2, 2)
assert_equal(test, """
letter | count | points
b | 3 | 2
c | 3 | 2
""")
def test_where_conditions(table):
t = table
t['totals'] = t['points'] * t['count']
test = t.where(t['totals'] > 8)
assert_equal(test, """
letter | count | points | totals
a | 9 | 1 | 9
z | 1 | 10 | 10
""")
def test_where_predicates(table):
t = table
t['totals'] = t['points'] * t['count']
test = t.where('totals', are.between(9, 11))
assert_equal(test, """
letter | count | points | totals
a | 9 | 1 | 9
z | 1 | 10 | 10
""")
@pytest.mark.filterwarnings("error")
def test_where_predicates_nowarning_on_str(table):
t = table
test = t.where('letter', are.equal_to('a'))
assert_equal(test, """
letter | count | points
a | 9 | 1
""")
def test_where_predicates_warning(table, capsys):
t1 = table.copy()
count1 = t1['count'] - 1
count1[0] += 1
t1['count1'] = count1
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
with (pytest.raises(ValueError)):
test = t1.where('count', are.equal_to(t1.column("count1")))
assert len(w) == 1
assert "Do not pass an array or list to a predicate." in str(w[-1].message)
test = t1.where('count', are.equal_to, t1.column('count1'))
assert_equal(test, """
letter | count | points | count1
a | 9 | 1 | 9
""")
def test_sort(table):
t = table
t['totals'] = t['points'] * t['count']
test = t.sort('points')
assert_equal(test, """
letter | count | points | totals
a | 9 | 1 | 9
b | 3 | 2 | 6
c | 3 | 2 | 6
z | 1 | 10 | 10
""")
test = t.sort(3)
assert_equal(test, """
letter | count | points | totals
b | 3 | 2 | 6
c | 3 | 2 | 6
a | 9 | 1 | 9
z | 1 | 10 | 10
""")
def test_sort_args(table):
t = table
t['totals'] = t['points'] * t['count']
test = t.sort('points', descending=True, distinct=True)
assert_equal(test, """
letter | count | points | totals
z | 1 | 10 | 10
b | 3 | 2 | 6
a | 9 | 1 | 9
""")
def test_sort_descending(table):
sorted_table = table.sort('points', descending=True)
assert_equal(sorted_table, """
letter | count | points
z | 1 | 10
b | 3 | 2
c | 3 | 2
a | 9 | 1
""")
def test_sort_syntax(table):
t = table
t['totals'] = t['points'] * t['count']
test = t.sort(-t['totals'])
assert_equal(test, """
letter | count | points | totals
z | 1 | 10 | 10
a | 9 | 1 | 9
b | 3 | 2 | 6
c | 3 | 2 | 6
""")
def test_group(table, table5):
t = table
test = t.group('points')
assert_equal(test, """
points | count
1 | 1
2 | 2
10 | 1
""")
test = t.group(2)
assert_equal(test, """
points | count
1 | 1
2 | 2
10 | 1
""")
def test_group_nans(table5):
t = table5
test = t.group('person_id')
assert_equal(test, """
person_id | count
nan | 3
1 | 1
2 | 1
3 | 1
""")
def test_group_with_func(table):
t = table
t['totals'] = t['points'] * t['count']
test = t.group('points', sum)
assert_equal(test, """
points | letter sum | count sum | totals sum
1 | | 9 | 9
2 | | 6 | 12
10 | | 1 | 10
""")
def test_groups(table):
t = table.copy()
t['totals'] = t['points'] * t['count']
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
test = t.groups(['points', 'early'])
assert_equal(test, """
points | early | count
1 | False | 1
1 | True | 1
2 | True | 2
10 | False | 1
""")
def test_groups_using_group(table):
t = table.copy()
t['totals'] = t['points'] * t['count']
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
test = t.group(['points', 'early'])
assert_equal(test, """
points | early | count
1 | False | 1
1 | True | 1
2 | True | 2
10 | False | 1
""")
def test_groups_list(table):
t = table.copy()
t['totals'] = t['points'] * t['count']
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
test = t.groups(['points', 'early'], lambda s: s)
assert_equal(test, """
points | early | letter | count | totals
1 | False | ['e'] | [12] | [12]
1 | True | ['a'] | [9] | [9]
2 | True | ['b' 'c'] | [3 3] | [6 6]
10 | False | ['z'] | [1] | [10]
""")
def test_groups_collect(table):
t = table.copy()
t['totals'] = t['points'] * t['count']
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
test = t.select(['points', 'early', 'count']).groups(['points', 'early'], sum)
assert_equal(test, """
points | early | count sum
1 | False | 12
1 | True | 9
2 | True | 6
10 | False | 1
""")
def test_groups_nans(table5):
t = table5
test = t.group(['person_id', 'points'])
assert_equal(test, """
person_id | points |count
nan | 1 | 1
nan | 2 | 2
1 | 2 | 1
2 | 4 | 1
3 | 10 | 1
""")
def test_groups_with_nonexistent_label(table):
t = table.copy()
with (pytest.raises(ValueError)):
t.groups(['bad_label', 'points'])
def test_join(table, table2):
"""Tests that join works, not destructive"""
t = table
u = table2
t['totals'] = t['points'] * t['count']
assert_equal(t.join('points', u), """
points | letter | count | totals | names
1 | a | 9 | 9 | one
2 | b | 3 | 6 | two
2 | c | 3 | 6 | two
""")
assert_equal(u, """
points | names
1 | one
2 | two
3 | three
""")
assert_equal(t, """
letter | count | points | totals
a | 9 | 1 | 9
b | 3 | 2 | 6
c | 3 | 2 | 6
z | 1 | 10 | 10
""")
def test_set_format_with_no_format_column(mocker, table):
MockNumberFormatter = mocker.NonCallableMock(spec=NumberFormatter)
del MockNumberFormatter.format_column
with pytest.raises(Exception):
table.set_format('count', MockNumberFormatter)
def test_join_html(table, table2):
"""Test that join doesn't crash with formatting."""
t = table
u = table2
t = t.set_format('count', NumberFormatter)
t.as_html()
u.join('points', t, 'points').as_html()
def test_pivot_counts(table, table2):
t = table.copy()
u = table2
t['totals'] = t['points'] * t['count']
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
test = t.pivot('points', 'early')
assert_equal(test, """
early | 1 | 2 | 10
False | 1 | 0 | 1
True | 1 | 2 | 0
""")
def test_pivot_counts_with_indices(table):
t = table.copy()
t['totals'] = t['points'] * t['count']
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
test = t.pivot(2, 4)
assert_equal(test, """
early | 1 | 2 | 10
False | 1 | 0 | 1
True | 1 | 2 | 0
""")
def test_pivot_values(table):
t = table.copy()
t['totals'] = t['points'] * t['count']
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
t['exists'] = 2
summed = t.pivot('points', 'early', 'exists', sum)
assert_equal(summed, """
early | 1 | 2 | 10
False | 2 | 0 | 2
True | 2 | 4 | 0
""")
maxed = t.pivot('points', 'early', 'exists', max, -1)
assert_equal(maxed, """
early | 1 | 2 | 10
False | 2 | -1 | 2
True | 2 | 2 | -1
""")
def test_pivot_multiple_rows(table):
t = table.copy()
t['totals'] = t['points'] * t['count']
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
t['late'] = t['letter'] > 'c'
t['exists'] = 1
test = t.pivot('points', ['early', 'late'], 'exists', sum)
assert_equal(test, """
early | late | 1 | 2 | 10
False | True | 1 | 0 | 1
True | False | 1 | 2 | 0
""")
def test_pivot_sum(table):
t = table.copy()
t['totals'] = t['points'] * t['count']
t.append(('e', 12, 1, 12))
t['early'] = t['letter'] < 'd'
t['exists'] = 1
test = t.pivot('points', 'early', 'exists', sum)
assert_equal(test, """
early | 1 | 2 | 10
False | 1 | 0 | 1
True | 1 | 2 | 0
""")
def test_apply(table):
t = table.copy()
assert_array_equal(t.apply(lambda x, y: x * y, 'count', 'points'),
np.array([9, 6, 6, 10]))
assert_array_equal(t.apply(lambda x: x * x, 'points'),
np.array([1, 4, 4, 100]))
assert_array_equal(t.apply(lambda row: row.item('count') * 2),
np.array([18, 6, 6, 2]))
with(pytest.raises(ValueError)):
t.apply(lambda x, y: x + y, 'count', 'score')
# Deprecated behavior
assert_array_equal(t.apply(lambda x, y: x * y, 'count', 'points'),
np.array([9, 6, 6, 10]))
def test_first(table):
t = table
t['totals'] = t['points'] * t['count']
assert_equal(t, """
letter | count | points | totals
a | 9 | 1 | 9
b | 3 | 2 | 6
c | 3 | 2 | 6
z | 1 | 10 | 10
""")
assert_equal(t.first(1), 9)
assert_equal(t.first("points"), 1)
def test_last(table):
t = table
t['totals'] = t['points'] * t['count']
assert_equal(t, """
letter | count | points | totals
a | 9 | 1 | 9
b | 3 | 2 | 6
c | 3 | 2 | 6
z | 1 | 10 | 10
""")
assert_equal(t.last(1), 1)
assert_equal(t.last("points"), 10)
########
# Init #
########
def test_tuples(table, table2):
"""Tests that different-sized tuples are allowed."""
t = table
u = table2
different = [((5, 1), (1, 2, 2, 10)), ('short', 'long')]
t = Table().with_columns('tuple', different[0], 'size', different[1])
assert_equal(t, """
tuple | size
(5, 1) | short
(1, 2, 2, 10) | long
""")
same = [((5, 4, 3, 1), (1, 2, 2, 10)), ('long', 'long')]
u = Table().with_columns('tuple', same[0], 'size', same[1])
assert_equal(u, """
tuple | size
[5 4 3 1] | long
[ 1 2 2 10] | long
""")
def test_keys_and_values():
"""Tests that a table can be constructed from keys and values."""
d = {1: 2, 3: 4}
t = Table().with_columns('keys', d.keys(), 'values', d.values())
assert_equal(t, """
keys | values
1 | 2
3 | 4
""")
##########
# Modify #
##########
def test_move_to_start(table):
assert table.labels == ('letter', 'count', 'points')
table.move_to_start('points')
assert table.labels == ('points', 'letter', 'count')
def test_move_to_end(table):
assert table.labels == ('letter', 'count', 'points')
table.move_to_end('letter')
assert table.labels == ('count', 'points', 'letter')
def test_move_to_end_start_int_labels(table):
assert table.labels == ('letter', 'count', 'points')
table.move_to_start(2)
assert table.labels == ('points', 'letter', 'count')
table.move_to_end(1)
assert table.labels == ('points', 'count', 'letter')
def test_append_row(table):
row = ['g', 2, 2]
table.append(row)
assert_equal(table, """
letter | count | points
a | 9 | 1
b | 3 | 2
c | 3 | 2
z | 1 | 10
g | 2 | 2
""")
def test_append_none(table):
row = [[], None, "", 0]
for i in row:
table.append(i)
assert_equal(table, """
letter | count | points
a | 9 | 1
b | 3 | 2
c | 3 | 2
z | 1 | 10
""")
def test_append_row_by_array(table):
row = np.array(['g', 2, 2])
table.append(row)
assert_equal(table, """
letter | count | points
a | 9 | 1
b | 3 | 2
c | 3 | 2
z | 1 | 10
g | 2 | 2
""")
def test_append_row_different_num_cols(table):
"""Makes sure that any incoming row must have the same amount of columns as the table."""
row = "abcd"
with(pytest.raises(Exception)):
table.append(row)
row = ["e", 2, 4, 6]
with(pytest.raises(Exception)):
table.append(row)
def test_append_column(table):
column_1 = [10, 20, 30, 40]
column_2 = 'hello'
table.append_column('new_col1', column_1)
assert_equal(table, """
letter | count | points | new_col1
a | 9 | 1 | 10
b | 3 | 2 | 20
c | 3 | 2 | 30
z | 1 | 10 | 40
""")
new_table = table.append_column('new_col2', column_2)
assert_equal(table, """
letter | count | points | new_col1 | new_col2
a | 9 | 1 | 10 | hello
b | 3 | 2 | 20 | hello
c | 3 | 2 | 30 | hello
z | 1 | 10 | 40 | hello
""")
assert_equal(new_table, """
letter | count | points | new_col1 | new_col2
a | 9 | 1 | 10 | hello
b | 3 | 2 | 20 | hello
c | 3 | 2 | 30 | hello
z | 1 | 10 | 40 | hello
""")
with(pytest.raises(ValueError)):
table.append_column('bad_col', [1, 2])
with(pytest.raises(ValueError)):
table.append_column(0, [1, 2, 3, 4])
def test_append_column_with_formatter(table):
column_1 = [10, 20, 30, 40]
column_2 = 'hello'
table.append_column('new_col1', column_1, CurrencyFormatter)
assert_equal(table, """
letter | count | points | new_col1
a | 9 | 1 | $10
b | 3 | 2 | $20
c | 3 | 2 | $30
z | 1 | 10 | $40
""")
table.append_column('new_col2', column_2)
assert_equal(table, """
letter | count | points | new_col1 | new_col2
a | 9 | 1 | $10 | hello
b | 3 | 2 | $20 | hello
c | 3 | 2 | $30 | hello
z | 1 | 10 | $40 | hello
""")
def test_with_column(table):
column_1 = [10, 20, 30, 40]
column_2 = 'hello'
table2 = table.with_column('new_col1', column_1)
table3 = table2.with_column('new_col2', column_2)
assert_equal(table, """
letter | count | points
a | 9 | 1
b | 3 | 2
c | 3 | 2
z | 1 | 10
""")
assert_equal(table2, """
letter | count | points | new_col1
a | 9 | 1 | 10
b | 3 | 2 | 20
c | 3 | 2 | 30
z | 1 | 10 | 40
""")
assert_equal(table3, """
letter | count | points | new_col1 | new_col2
a | 9 | 1 | 10 | hello
b | 3 | 2 | 20 | hello
c | 3 | 2 | 30 | hello
z | 1 | 10 | 40 | hello
""")
with(pytest.raises(ValueError)):
table.append_column('bad_col', [1, 2])
with(pytest.raises(ValueError)):
table.append_column(0, [1, 2, 3, 4])
def test_with_column_with_formatter(table):
column_1 = [10, 20, 30, 40]
column_2 = 'hello'
table2 = table.with_column('new_col1', column_1, CurrencyFormatter)
table3 = table2.with_column('new_col2', column_2)
assert_equal(table, """
letter | count | points
a | 9 | 1
b | 3 | 2
c | 3 | 2
z | 1 | 10
""")
assert_equal(table2, """
letter | count | points | new_col1
a | 9 | 1 | $10
b | 3 | 2 | $20
c | 3 | 2 | $30
z | 1 | 10 | $40
""")
assert_equal(table3, """
letter | count | points | new_col1 | new_col2
a | 9 | 1 | $10 | hello
b | 3 | 2 | $20 | hello
c | 3 | 2 | $30 | hello
z | 1 | 10 | $40 | hello
""")
def test_with_columns():
players = Table().with_columns('player_id', make_array(110234, 110235), 'wOBA', make_array(.354, .236))
assert_equal(players, """
player_id | wOBA
110234 | 0.354
110235 | 0.236
""")
players = players.with_columns('salaries', 'N/A', 'season', 2016)
assert_equal(players, """
player_id | wOBA | salaries | season
110234 | 0.354 | N/A | 2016
110235 | 0.236 | N/A | 2016
""")
salaries = Table().with_column('salary', make_array('$500,000', '$15,500,000'))
players = players.with_columns('salaries', salaries.column('salary'), 'years', make_array(6, 1))
assert_equal(players, """
player_id | wOBA | salaries | season | years
110234 | 0.354 | $500,000 | 2016 | 6
110235 | 0.236 | $15,500,000 | 2016 | 1
""")
def test_with_columns_exception_for_incorrect_usage():
with(pytest.raises(TypeError)):
Table.with_columns('player_id', make_array(110234, 110235), 'wOBA', make_array(.354, .236))
def test_with_columns_with_formats():
players = Table().with_columns('player_id', make_array(110234, 110235), 'wOBA', make_array(.354, .236))
assert_equal(players, """
player_id | wOBA
110234 | 0.354
110235 | 0.236
""")
players = players.with_columns('salaries', 'N/A', 'season', 2016)
assert_equal(players, """
player_id | wOBA | salaries | season
110234 | 0.354 | N/A | 2016
110235 | 0.236 | N/A | 2016
""")
salaries = Table().with_column('salary', make_array(500000, 15500000))
players2 = players.with_columns('salaries', salaries.column('salary'), 'years', make_array(6, 1), formatter=CurrencyFormatter)
assert_equal(players2, """
player_id | wOBA | salaries | season | years
110234 | 0.354 | $500,000 | 2016 | $6
110235 | 0.236 | $15,500,000 | 2016 | $1
""")
with(pytest.raises(Exception)):
players3 = players.with_columns('salaries', salaries.column('salary'), make_array(7, 2), 'years', make_array(6, 1))
def test_with_columns(table):
column_1 = [10, 20, 30, 40]
column_2 = 'hello'
table2 = table.with_columns(
'new_col1', column_1,
'new_col2', column_2)
assert_equal(table2, """
letter | count | points | new_col1 | new_col2
a | 9 | 1 | 10 | hello
b | 3 | 2 | 20 | hello
c | 3 | 2 | 30 | hello
z | 1 | 10 | 40 | hello
""")
def test_append_table(table):
table.append(table)
assert_equal(table, """
letter | count | points
a | 9 | 1
b | 3 | 2
c | 3 | 2
z | 1 | 10
a | 9 | 1