2
2
from unittest import TestCase
3
3
import inspect
4
4
import copy
5
+ import pytest
5
6
6
7
import plotly .graph_objs as go
7
8
from plotly .subplots import make_subplots
9
+ from functools import reduce
8
10
9
11
10
12
class TestSelectForEachUpdateTraces (TestCase ):
@@ -414,3 +416,60 @@ def test_update_traces_overwrite(self):
414
416
{"type" : "bar" , "marker" : {"line" : {"width" : 10 }}},
415
417
],
416
418
)
419
+
420
+
421
+ @pytest .fixture
422
+ def select_traces_fixture ():
423
+ fig = make_subplots (2 , 3 )
424
+ for n in range (3 ):
425
+ fig .add_trace (go .Scatter (x = [1 , 2 ], y = [3 , n ]), row = 2 , col = 3 )
426
+ for n , ty in zip (range (3 ), [go .Scatter , go .Bar , go .Bar ]):
427
+ fig .add_trace (ty (x = [1 , 2 ], y = [3 , 10 * n ]), row = 1 , col = 3 )
428
+ return fig
429
+
430
+
431
+ def test_select_traces_integer (select_traces_fixture ):
432
+ fig = select_traces_fixture
433
+ # check we can index last trace selected
434
+ tr = list (fig .select_traces (selector = - 1 ))[0 ]
435
+ assert tr .y [1 ] == 20
436
+ # check we can index last trace selected in a row and column
437
+ tr = list (fig .select_traces (selector = - 1 , row = 2 , col = 3 ))[0 ]
438
+ assert tr .y [1 ] == 2
439
+ # check that indexing out of bounds raises IndexError
440
+ with pytest .raises (IndexError ):
441
+ tr = list (fig .select_traces (selector = 6 ))[0 ]
442
+
443
+
444
+ def test_select_traces_string (select_traces_fixture ):
445
+ fig = select_traces_fixture
446
+ # check we can select traces by type simply by passing a string to selector
447
+ trs = list (fig .select_traces (selector = "bar" ))
448
+ assert len (trs ) == 2 and reduce (
449
+ lambda last , cur : last
450
+ and (cur [0 ]["type" ] == "bar" )
451
+ and (cur [0 ]["y" ][1 ] == cur [1 ]),
452
+ zip (trs , [10 , 20 ]),
453
+ True ,
454
+ )
455
+ # check we can select traces by type regardless of the subplots they are on
456
+ trs = list (fig .select_traces (selector = "scatter" ))
457
+ assert len (trs ) == 4 and reduce (
458
+ lambda last , cur : last
459
+ and (cur [0 ]["type" ] == "scatter" )
460
+ and (cur [0 ]["y" ][1 ] == cur [1 ]),
461
+ zip (trs , [0 , 1 , 2 , 0 ]),
462
+ True ,
463
+ )
464
+ # check that we can select traces by type but only on a specific subplot
465
+ trs = list (fig .select_traces (row = 2 , col = 3 , selector = "scatter" ))
466
+ assert len (trs ) == 3 and reduce (
467
+ lambda last , cur : last
468
+ and (cur [0 ]["type" ] == "scatter" )
469
+ and (cur [0 ]["y" ][1 ] == cur [1 ]),
470
+ zip (trs , [0 , 1 , 2 ]),
471
+ True ,
472
+ )
473
+ # check that if selector matches no trace types then no traces are returned
474
+ trs = list (fig .select_traces (selector = "bogus" ))
475
+ assert len (trs ) == 0
0 commit comments