forked from plotly/dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_base_component.py
1042 lines (881 loc) · 36 KB
/
test_base_component.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
from collections import OrderedDict
import collections
import inspect
import json
import os
import shutil
import unittest
import plotly
from dash.development.base_component import (
generate_class,
generate_class_string,
generate_class_file,
Component,
_explicitize_args,
js_to_py_type,
create_docstring,
parse_events
)
Component._prop_names = ('id', 'a', 'children', 'style', )
Component._type = 'TestComponent'
Component._namespace = 'test_namespace'
Component._valid_wildcard_attributes = ['data-', 'aria-']
def nested_tree():
"""This tree has a few unique properties:
- children is mixed strings and components (as in c2)
- children is just components (as in c)
- children is just strings (as in c1)
- children is just a single component (as in c3, c4)
- children contains numbers (as in c2)
- children contains "None" items (as in c2)
"""
c1 = Component(
id='0.1.x.x.0',
children='string'
)
c2 = Component(
id='0.1.x.x',
children=[10, None, 'wrap string', c1, 'another string', 4.51]
)
c3 = Component(
id='0.1.x',
# children is just a component
children=c2
)
c4 = Component(
id='0.1',
children=c3
)
c5 = Component(id='0.0')
c = Component(id='0', children=[c5, c4])
return c, c1, c2, c3, c4, c5
class TestComponent(unittest.TestCase):
def test_init(self):
Component(a=3)
def test_get_item_with_children(self):
c1 = Component(id='1')
c2 = Component(children=[c1])
self.assertEqual(c2['1'], c1)
def test_get_item_with_children_as_component_instead_of_list(self):
c1 = Component(id='1')
c2 = Component(id='2', children=c1)
self.assertEqual(c2['1'], c1)
def test_get_item_with_nested_children_one_branch(self):
c1 = Component(id='1')
c2 = Component(id='2', children=[c1])
c3 = Component(children=[c2])
self.assertEqual(c2['1'], c1)
self.assertEqual(c3['2'], c2)
self.assertEqual(c3['1'], c1)
def test_get_item_with_nested_children_two_branches(self):
c1 = Component(id='1')
c2 = Component(id='2', children=[c1])
c3 = Component(id='3')
c4 = Component(id='4', children=[c3])
c5 = Component(children=[c2, c4])
self.assertEqual(c2['1'], c1)
self.assertEqual(c4['3'], c3)
self.assertEqual(c5['2'], c2)
self.assertEqual(c5['4'], c4)
self.assertEqual(c5['1'], c1)
self.assertEqual(c5['3'], c3)
def test_get_item_with_nested_children_with_mixed_strings_and_without_lists(self): # noqa: E501
c, c1, c2, c3, c4, c5 = nested_tree()
self.assertEqual(
list(c.keys()),
[
'0.0',
'0.1',
'0.1.x',
'0.1.x.x',
'0.1.x.x.0'
]
)
# Try to get each item
for comp in [c1, c2, c3, c4, c5]:
self.assertEqual(c[comp.id], comp)
# Get an item that doesn't exist
with self.assertRaises(KeyError):
c['x']
def test_len_with_nested_children_with_mixed_strings_and_without_lists(self): # noqa: E501
c = nested_tree()[0]
self.assertEqual(
len(c),
5 + # 5 components
5 + # c2 has 2 strings, 2 numbers, and a None
1 # c1 has 1 string
)
def test_set_item_with_nested_children_with_mixed_strings_and_without_lists(self): # noqa: E501
keys = [
'0.0',
'0.1',
'0.1.x',
'0.1.x.x',
'0.1.x.x.0'
]
c = nested_tree()[0]
# Test setting items starting from the innermost item
for key in reversed(keys):
new_id = 'new {}'.format(key)
new_component = Component(
id=new_id,
children='new string'
)
c[key] = new_component
self.assertEqual(c[new_id], new_component)
def test_del_item_with_nested_children_with_mixed_strings_and_without_lists(self): # noqa: E501
c = nested_tree()[0]
for key in reversed(list(c.keys())):
c[key]
del c[key]
with self.assertRaises(KeyError):
c[key]
def test_traverse_with_nested_children_with_mixed_strings_and_without_lists(self): # noqa: E501
c, c1, c2, c3, c4, c5 = nested_tree()
elements = [i for i in c.traverse()]
self.assertEqual(
elements,
c.children + [c3] + [c2] + c2.children
)
def test_iter_with_nested_children_with_mixed_strings_and_without_lists(self): # noqa: E501
c = nested_tree()[0]
keys = list(c.keys())
# get a list of ids that __iter__ provides
iter_keys = [i for i in c]
self.assertEqual(keys, iter_keys)
def test_to_plotly_json_with_nested_children_with_mixed_strings_and_without_lists(self): # noqa: E501
c = nested_tree()[0]
Component._namespace
Component._type
self.assertEqual(json.loads(json.dumps(
c.to_plotly_json(),
cls=plotly.utils.PlotlyJSONEncoder
)), {
'type': 'TestComponent',
'namespace': 'test_namespace',
'props': {
'children': [
{
'type': 'TestComponent',
'namespace': 'test_namespace',
'props': {
'id': '0.0'
}
},
{
'type': 'TestComponent',
'namespace': 'test_namespace',
'props': {
'children': {
'type': 'TestComponent',
'namespace': 'test_namespace',
'props': {
'children': {
'type': 'TestComponent',
'namespace': 'test_namespace',
'props': {
'children': [
10,
None,
'wrap string',
{
'type': 'TestComponent',
'namespace': 'test_namespace', # noqa: E501
'props': {
'children': 'string',
'id': '0.1.x.x.0'
}
},
'another string',
4.51
],
'id': '0.1.x.x'
}
},
'id': '0.1.x'
}
},
'id': '0.1'
}
}
],
'id': '0'
}
})
def test_get_item_raises_key_if_id_doesnt_exist(self):
c = Component()
with self.assertRaises(KeyError):
c['1']
c1 = Component(id='1')
with self.assertRaises(KeyError):
c1['1']
c2 = Component(id='2', children=[c1])
with self.assertRaises(KeyError):
c2['0']
c3 = Component(children='string with no id')
with self.assertRaises(KeyError):
c3['0']
def test_equality(self):
# TODO - Why is this the case? How is == being performed?
# __eq__ only needs __getitem__, __iter__, and __len__
self.assertTrue(Component() == Component())
self.assertTrue(Component() is not Component())
c1 = Component(id='1')
c2 = Component(id='2', children=[Component()])
self.assertTrue(c1 == c2)
self.assertTrue(c1 is not c2)
def test_set_item(self):
c1a = Component(id='1', children='Hello world')
c2 = Component(id='2', children=c1a)
self.assertEqual(c2['1'], c1a)
c1b = Component(id='1', children='Brave new world')
c2['1'] = c1b
self.assertEqual(c2['1'], c1b)
def test_set_item_with_children_as_list(self):
c1 = Component(id='1')
c2 = Component(id='2', children=[c1])
self.assertEqual(c2['1'], c1)
c3 = Component(id='3')
c2['1'] = c3
self.assertEqual(c2['3'], c3)
def test_set_item_with_nested_children(self):
c1 = Component(id='1')
c2 = Component(id='2', children=[c1])
c3 = Component(id='3')
c4 = Component(id='4', children=[c3])
c5 = Component(id='5', children=[c2, c4])
c3b = Component(id='3')
self.assertEqual(c5['3'], c3)
self.assertTrue(c5['3'] is not '3')
self.assertTrue(c5['3'] is not c3b)
c5['3'] = c3b
self.assertTrue(c5['3'] is c3b)
self.assertTrue(c5['3'] is not c3)
c2b = Component(id='2')
c5['2'] = c2b
self.assertTrue(c5['4'] is c4)
self.assertTrue(c5['2'] is not c2)
self.assertTrue(c5['2'] is c2b)
with self.assertRaises(KeyError):
c5['1']
def test_set_item_raises_key_error(self):
c1 = Component(id='1')
c2 = Component(id='2', children=[c1])
with self.assertRaises(KeyError):
c2['3'] = Component(id='3')
def test_del_item_from_list(self):
c1 = Component(id='1')
c2 = Component(id='2')
c3 = Component(id='3', children=[c1, c2])
self.assertEqual(c3['1'], c1)
self.assertEqual(c3['2'], c2)
del c3['2']
with self.assertRaises(KeyError):
c3['2']
self.assertEqual(c3.children, [c1])
del c3['1']
with self.assertRaises(KeyError):
c3['1']
self.assertEqual(c3.children, [])
def test_del_item_from_class(self):
c1 = Component(id='1')
c2 = Component(id='2', children=c1)
self.assertEqual(c2['1'], c1)
del c2['1']
with self.assertRaises(KeyError):
c2['1']
self.assertEqual(c2.children, None)
def test_to_plotly_json_without_children(self):
c = Component(id='a')
c._prop_names = ('id',)
c._type = 'MyComponent'
c._namespace = 'basic'
self.assertEqual(
c.to_plotly_json(),
{'namespace': 'basic', 'props': {'id': 'a'}, 'type': 'MyComponent'}
)
def test_to_plotly_json_with_null_arguments(self):
c = Component(id='a')
c._prop_names = ('id', 'style',)
c._type = 'MyComponent'
c._namespace = 'basic'
self.assertEqual(
c.to_plotly_json(),
{'namespace': 'basic', 'props': {'id': 'a'}, 'type': 'MyComponent'}
)
c = Component(id='a', style=None)
c._prop_names = ('id', 'style',)
c._type = 'MyComponent'
c._namespace = 'basic'
self.assertEqual(
c.to_plotly_json(),
{
'namespace': 'basic', 'props': {'id': 'a', 'style': None},
'type': 'MyComponent'
}
)
def test_to_plotly_json_with_children(self):
c = Component(id='a', children='Hello World')
c._prop_names = ('id', 'children',)
c._type = 'MyComponent'
c._namespace = 'basic'
self.assertEqual(
c.to_plotly_json(),
{
'namespace': 'basic',
'props': {
'id': 'a',
# TODO - Rename 'children' to 'children'
'children': 'Hello World'
},
'type': 'MyComponent'
}
)
def test_to_plotly_json_with_nested_children(self):
c1 = Component(id='1', children='Hello World')
c1._prop_names = ('id', 'children',)
c1._type = 'MyComponent'
c1._namespace = 'basic'
c2 = Component(id='2', children=c1)
c2._prop_names = ('id', 'children',)
c2._type = 'MyComponent'
c2._namespace = 'basic'
c3 = Component(id='3', children='Hello World')
c3._prop_names = ('id', 'children',)
c3._type = 'MyComponent'
c3._namespace = 'basic'
c4 = Component(id='4', children=[c2, c3])
c4._prop_names = ('id', 'children',)
c4._type = 'MyComponent'
c4._namespace = 'basic'
def to_dict(id, children):
return {
'namespace': 'basic',
'props': {
'id': id,
'children': children
},
'type': 'MyComponent'
}
"""
self.assertEqual(
json.dumps(c4.to_plotly_json(),
cls=plotly.utils.PlotlyJSONEncoder),
json.dumps(to_dict('4', [
to_dict('2', to_dict('1', 'Hello World')),
to_dict('3', 'Hello World')
]))
)
"""
def test_to_plotly_json_with_wildcards(self):
c = Component(id='a', **{'aria-expanded': 'true',
'data-toggle': 'toggled',
'data-none': None})
c._prop_names = ('id',)
c._type = 'MyComponent'
c._namespace = 'basic'
self.assertEqual(
c.to_plotly_json(),
{'namespace': 'basic',
'props': {
'aria-expanded': 'true',
'data-toggle': 'toggled',
'data-none': None,
'id': 'a',
},
'type': 'MyComponent'}
)
def test_len(self):
self.assertEqual(len(Component()), 0)
self.assertEqual(len(Component(children='Hello World')), 1)
self.assertEqual(len(Component(children=Component())), 1)
self.assertEqual(len(Component(children=[Component(), Component()])),
2)
self.assertEqual(len(Component(children=[
Component(children=Component()),
Component()
])), 3)
def test_iter(self):
# keys, __contains__, items, values, and more are all mixin methods
# that we get for free by inheriting from the MutableMapping
# and behave as according to our implementation of __iter__
c = Component(
id='1',
children=[
Component(id='2', children=[
Component(id='3', children=Component(id='4'))
]),
Component(id='5', children=[
Component(id='6', children='Hello World')
]),
Component(),
Component(children='Hello World'),
Component(children=Component(id='7')),
Component(children=[Component(id='8')]),
]
)
# test keys()
keys = [k for k in list(c.keys())]
self.assertEqual(keys, ['2', '3', '4', '5', '6', '7', '8'])
self.assertEqual([i for i in c], keys)
# test values()
components = [i for i in list(c.values())]
self.assertEqual(components, [c[k] for k in keys])
# test __iter__()
for k in keys:
# test __contains__()
self.assertTrue(k in c)
# test __items__
items = [i for i in list(c.items())]
self.assertEqual(list(zip(keys, components)), items)
def test_pop(self):
c2 = Component(id='2')
c = Component(id='1', children=c2)
c2_popped = c.pop('2')
self.assertTrue('2' not in c)
self.assertTrue(c2_popped is c2)
class TestGenerateClassFile(unittest.TestCase):
def setUp(self):
json_path = os.path.join('tests', 'development', 'metadata_test.json')
with open(json_path) as data_file:
json_string = data_file.read()
data = json\
.JSONDecoder(object_pairs_hook=collections.OrderedDict)\
.decode(json_string)
self.data = data
# Create a folder for the new component file
os.makedirs('TableComponents')
# Import string not included in generated class string
import_string =\
"# AUTO GENERATED FILE - DO NOT EDIT\n\n" + \
"from dash.development.base_component import" + \
" Component, _explicitize_args\n\n\n"
# Class string generated from generate_class_string
self.component_class_string = import_string + generate_class_string(
typename='Table',
props=data['props'],
description=data['description'],
namespace='TableComponents'
)
# Class string written to file
generate_class_file(
typename='Table',
props=data['props'],
description=data['description'],
namespace='TableComponents'
)
written_file_path = os.path.join(
'TableComponents', "Table.py"
)
with open(written_file_path, 'r') as f:
self.written_class_string = f.read()
# The expected result for both class string and class file generation
expected_string_path = os.path.join(
'tests', 'development', 'metadata_test.py'
)
with open(expected_string_path, 'r') as f:
self.expected_class_string = f.read()
def tearDown(self):
shutil.rmtree('TableComponents')
def test_class_string(self):
self.assertEqual(
self.expected_class_string,
self.component_class_string
)
def test_class_file(self):
self.assertEqual(
self.expected_class_string,
self.written_class_string
)
class TestGenerateClass(unittest.TestCase):
def setUp(self):
path = os.path.join('tests', 'development', 'metadata_test.json')
with open(path) as data_file:
json_string = data_file.read()
data = json\
.JSONDecoder(object_pairs_hook=collections.OrderedDict)\
.decode(json_string)
self.data = data
self.ComponentClass = generate_class(
typename='Table',
props=data['props'],
description=data['description'],
namespace='TableComponents'
)
path = os.path.join(
'tests', 'development', 'metadata_required_test.json'
)
with open(path) as data_file:
json_string = data_file.read()
required_data = json\
.JSONDecoder(object_pairs_hook=collections.OrderedDict)\
.decode(json_string)
self.required_data = required_data
self.ComponentClassRequired = generate_class(
typename='TableRequired',
props=required_data['props'],
description=required_data['description'],
namespace='TableComponents'
)
def test_to_plotly_json(self):
c = self.ComponentClass()
self.assertEqual(c.to_plotly_json(), {
'namespace': 'TableComponents',
'type': 'Table',
'props': {
'children': None
}
})
c = self.ComponentClass(id='my-id')
self.assertEqual(c.to_plotly_json(), {
'namespace': 'TableComponents',
'type': 'Table',
'props': {
'children': None,
'id': 'my-id'
}
})
c = self.ComponentClass(id='my-id', optionalArray=None)
self.assertEqual(c.to_plotly_json(), {
'namespace': 'TableComponents',
'type': 'Table',
'props': {
'children': None,
'id': 'my-id',
'optionalArray': None
}
})
def test_arguments_become_attributes(self):
kwargs = {
'id': 'my-id',
'children': 'text children',
'optionalArray': [[1, 2, 3]]
}
component_instance = self.ComponentClass(**kwargs)
for k, v in list(kwargs.items()):
self.assertEqual(getattr(component_instance, k), v)
def test_repr_single_default_argument(self):
c1 = self.ComponentClass('text children')
c2 = self.ComponentClass(children='text children')
self.assertEqual(
repr(c1),
"Table('text children')"
)
self.assertEqual(
repr(c2),
"Table('text children')"
)
def test_repr_single_non_default_argument(self):
c = self.ComponentClass(id='my-id')
self.assertEqual(
repr(c),
"Table(id='my-id')"
)
def test_repr_multiple_arguments(self):
# Note how the order in which keyword arguments are supplied is
# not always equal to the order in the repr of the component
c = self.ComponentClass(id='my id', optionalArray=[1, 2, 3])
self.assertEqual(
repr(c),
"Table(optionalArray=[1, 2, 3], id='my id')"
)
def test_repr_nested_arguments(self):
c1 = self.ComponentClass(id='1')
c2 = self.ComponentClass(id='2', children=c1)
c3 = self.ComponentClass(children=c2)
self.assertEqual(
repr(c3),
"Table(Table(children=Table(id='1'), id='2'))"
)
def test_repr_with_wildcards(self):
c = self.ComponentClass(id='1', **{"data-one": "one",
"aria-two": "two"})
data_first = "Table(id='1', data-one='one', aria-two='two')"
aria_first = "Table(id='1', aria-two='two', data-one='one')"
repr_string = repr(c)
if not (repr_string == data_first or repr_string == aria_first):
raise Exception("%s\nDoes not equal\n%s\nor\n%s" %
(repr_string, data_first, aria_first))
def test_docstring(self):
assert_docstring(self.assertEqual, self.ComponentClass.__doc__)
def test_events(self):
self.assertEqual(
self.ComponentClass().available_events,
['restyle', 'relayout', 'click']
)
# This one is kind of pointless now
def test_call_signature(self):
__init__func = self.ComponentClass.__init__
# TODO: Will break in Python 3
# http://stackoverflow.com/questions/2677185/
self.assertEqual(
inspect.getargspec(__init__func).args,
['self',
'children',
'optionalArray',
'optionalBool',
'optionalFunc',
'optionalNumber',
'optionalObject',
'optionalString',
'optionalSymbol',
'optionalNode',
'optionalElement',
'optionalMessage',
'optionalEnum',
'optionalUnion',
'optionalArrayOf',
'optionalObjectOf',
'optionalObjectWithShapeAndNestedDescription',
'optionalAny',
'customProp',
'customArrayProp',
'id'] if hasattr(inspect, 'signature') else []
)
self.assertEqual(
inspect.getargspec(__init__func).varargs,
None if hasattr(inspect, 'signature') else 'args'
)
self.assertEqual(
inspect.getargspec(__init__func).keywords,
'kwargs'
)
if hasattr(inspect, 'signature'):
self.assertEqual(
[str(x) for x in inspect.getargspec(__init__func).defaults],
['None'] + ['undefined'] * 19
)
def test_required_props(self):
with self.assertRaises(Exception):
self.ComponentClassRequired()
self.ComponentClassRequired(id='test')
with self.assertRaises(Exception):
self.ComponentClassRequired(id='test', lahlah='test')
with self.assertRaises(Exception):
self.ComponentClassRequired(children='test')
class TestMetaDataConversions(unittest.TestCase):
def setUp(self):
path = os.path.join('tests', 'development', 'metadata_test.json')
with open(path) as data_file:
json_string = data_file.read()
data = json\
.JSONDecoder(object_pairs_hook=collections.OrderedDict)\
.decode(json_string)
self.data = data
self.expected_arg_strings = OrderedDict([
['children',
'a list of or a singular dash component, string or number'],
['optionalArray', 'list'],
['optionalBool', 'boolean'],
['optionalFunc', ''],
['optionalNumber', 'number'],
['optionalObject', 'dict'],
['optionalString', 'string'],
['optionalSymbol', ''],
['optionalElement', 'dash component'],
['optionalNode',
'a list of or a singular dash component, string or number'],
['optionalMessage', ''],
['optionalEnum', 'a value equal to: \'News\', \'Photos\''],
['optionalUnion', 'string | number'],
['optionalArrayOf', 'list'],
['optionalObjectOf',
'dict with strings as keys and values of type number'],
['optionalObjectWithShapeAndNestedDescription', '\n'.join([
"dict containing keys 'color', 'fontSize', 'figure'.",
"Those keys have the following types: ",
" - color (string; optional)",
" - fontSize (number; optional)",
" - figure (optional): Figure is a plotly graph object. figure has the following type: dict containing keys 'data', 'layout'.", # noqa: E501
"Those keys have the following types: ",
" - data (list; optional): data is a collection of traces",
" - layout (dict; optional): layout describes the rest of the figure" # noqa: E501
])],
['optionalAny', 'boolean | number | string | dict | list'],
['customProp', ''],
['customArrayProp', 'list'],
['data-*', 'string'],
['aria-*', 'string'],
['in', 'string'],
['id', 'string'],
['dashEvents', "a value equal to: 'restyle', 'relayout', 'click'"]
])
def test_docstring(self):
docstring = create_docstring(
'Table',
self.data['props'],
parse_events(self.data['props']),
self.data['description'],
)
assert_docstring(self.assertEqual, docstring)
def test_docgen_to_python_args(self):
props = self.data['props']
for prop_name, prop in list(props.items()):
self.assertEqual(
js_to_py_type(prop['type']),
self.expected_arg_strings[prop_name]
)
def assert_docstring(assertEqual, docstring):
for i, line in enumerate(docstring.split('\n')):
assertEqual(line, ([
"A Table component.",
"This is a description of the component.",
"It's multiple lines long.",
'',
"Keyword arguments:",
"- children (a list of or a singular dash component, string or number; optional)", # noqa: E501
"- optionalArray (list; optional): Description of optionalArray",
"- optionalBool (boolean; optional)",
"- optionalNumber (number; optional)",
"- optionalObject (dict; optional)",
"- optionalString (string; optional)",
"- optionalNode (a list of or a singular dash component, "
"string or number; optional)",
"- optionalElement (dash component; optional)",
"- optionalEnum (a value equal to: 'News', 'Photos'; optional)",
"- optionalUnion (string | number; optional)",
"- optionalArrayOf (list; optional)",
"- optionalObjectOf (dict with strings as keys and values "
"of type number; optional)",
"- optionalObjectWithShapeAndNestedDescription (optional): . "
"optionalObjectWithShapeAndNestedDescription has the "
"following type: dict containing keys "
"'color', 'fontSize', 'figure'.",
"Those keys have the following types: ",
" - color (string; optional)",
" - fontSize (number; optional)",
" - figure (optional): Figure is a plotly graph object. "
"figure has the following type: dict containing "
"keys 'data', 'layout'.",
"Those keys have the following types: ",
" - data (list; optional): data is a collection of traces",
" - layout (dict; optional): layout describes "
"the rest of the figure",
"- optionalAny (boolean | number | string | dict | "
"list; optional)",
"- customProp (optional)",
"- customArrayProp (list; optional)",
'- data-* (string; optional)',
'- aria-* (string; optional)',
'- in (string; optional)',
'- id (string; optional)',
'',
"Available events: 'restyle', 'relayout', 'click'",
' '
])[i]
)
class TestFlowMetaDataConversions(unittest.TestCase):
def setUp(self):
path = os.path.join('tests', 'development', 'flow_metadata_test.json')
with open(path) as data_file:
json_string = data_file.read()
data = json\
.JSONDecoder(object_pairs_hook=collections.OrderedDict)\
.decode(json_string)
self.data = data
self.expected_arg_strings = OrderedDict([
['children', 'a list of or a singular dash component, string or number'],
['requiredString', 'string'],
['optionalString', 'string'],
['optionalBoolean', 'boolean'],
['optionalFunc', ''],
['optionalNode', 'a list of or a singular dash component, string or number'],
['optionalArray', 'list'],
['requiredUnion', 'string | number'],
['optionalSignature(shape)', '\n'.join([
"dict containing keys 'checked', 'children', 'customData', 'disabled', 'label', 'primaryText', 'secondaryText', 'style', 'value'.",
"Those keys have the following types: ",
"- checked (boolean; optional)",
"- children (a list of or a singular dash component, string or number; optional)",
"- customData (bool | number | str | dict | list; required): A test description",
"- disabled (boolean; optional)",
"- label (string; optional)",
"- primaryText (string; required): Another test description",
"- secondaryText (string; optional)",
"- style (dict; optional)",
"- value (bool | number | str | dict | list; required)"
])],
['requiredNested', '\n'.join([
"dict containing keys 'customData', 'value'.",
"Those keys have the following types: ",
"- customData (required): . customData has the following type: dict containing keys 'checked', 'children', 'customData', 'disabled', 'label', 'primaryText', 'secondaryText', 'style', 'value'.",
" Those keys have the following types: ",
" - checked (boolean; optional)",
" - children (a list of or a singular dash component, string or number; optional)",
" - customData (bool | number | str | dict | list; required)",
" - disabled (boolean; optional)",
" - label (string; optional)",
" - primaryText (string; required)",
" - secondaryText (string; optional)",
" - style (dict; optional)",
" - value (bool | number | str | dict | list; required)",
"- value (bool | number | str | dict | list; required)",
])],
])
def test_docstring(self):
docstring = create_docstring(
'Flow_component',
self.data['props'],
parse_events(self.data['props']),
self.data['description'],
)
assert_flow_docstring(self.assertEqual, docstring)
def test_docgen_to_python_args(self):
props = self.data['props']
for prop_name, prop in list(props.items()):
self.assertEqual(
js_to_py_type(prop['flowType'], is_flow_type=True),
self.expected_arg_strings[prop_name]
)
def assert_flow_docstring(assertEqual, docstring):
for i, line in enumerate(docstring.split('\n')):
assertEqual(line, ([
"A Flow_component component.",
"This is a test description of the component.",
"It's multiple lines long.",
"",
"Keyword arguments:",
"- requiredString (string; required): A required string",
"- optionalString (string; optional): A string that isn't required.",
"- optionalBoolean (boolean; optional): A boolean test",
"- optionalNode (a list of or a singular dash component, string or number; optional): "
"A node test",
"- optionalArray (list; optional): An array test with a particularly ",
"long description that covers several lines. It includes the newline character ",
"and should span 3 lines in total.",