7
7
import unittest .mock as mock
8
8
from plotly .express ._core import build_dataframe
9
9
from pandas .testing import assert_frame_equal
10
-
10
+ from plotly . tests . b64 import b64 , _b64
11
11
12
12
# Fixtures
13
13
# --------
@@ -26,8 +26,9 @@ def add_interchange_module_for_old_pandas():
26
26
27
27
def test_numpy ():
28
28
fig = px .scatter (x = [1 , 2 , 3 ], y = [2 , 3 , 4 ], color = [1 , 3 , 9 ])
29
- assert np .all (fig .data [0 ].x == np .array ([1 , 2 , 3 ]))
30
- assert np .all (fig .data [0 ].y == np .array ([2 , 3 , 4 ]))
29
+
30
+ assert np .all (fig .data [0 ].x == b64 (np .array ([1 , 2 , 3 ])))
31
+ assert np .all (fig .data [0 ].y == b64 (np .array ([2 , 3 , 4 ])))
31
32
assert np .all (fig .data [0 ].marker .color == np .array ([1 , 3 , 9 ]))
32
33
33
34
@@ -99,16 +100,16 @@ def test_several_dataframes():
99
100
df = pd .DataFrame (dict (x = [0 , 1 ], y = [3 , 4 ]))
100
101
df2 = pd .DataFrame (dict (x = [3 , 5 ], y = [23 , 24 ]))
101
102
fig = px .scatter (x = df .y , y = df2 .y )
102
- assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
103
- assert np .all (fig .data [0 ].y == np .array ([23 , 24 ]))
103
+ assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
104
+ assert np .all (fig .data [0 ].y == b64 ( np .array ([23 , 24 ]) ))
104
105
assert fig .data [0 ].hovertemplate == "x=%{x}<br>y=%{y}<extra></extra>"
105
106
106
107
df = pd .DataFrame (dict (x = [0 , 1 ], y = [3 , 4 ]))
107
108
df2 = pd .DataFrame (dict (x = [3 , 5 ], y = [23 , 24 ]))
108
109
df3 = pd .DataFrame (dict (y = [0.1 , 0.2 ]))
109
110
fig = px .scatter (x = df .y , y = df2 .y , size = df3 .y )
110
- assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
111
- assert np .all (fig .data [0 ].y == np .array ([23 , 24 ]))
111
+ assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
112
+ assert np .all (fig .data [0 ].y == b64 ( np .array ([23 , 24 ]) ))
112
113
assert (
113
114
fig .data [0 ].hovertemplate
114
115
== "x=%{x}<br>y=%{y}<br>size=%{marker.size}<extra></extra>"
@@ -118,8 +119,8 @@ def test_several_dataframes():
118
119
df2 = pd .DataFrame (dict (x = [3 , 5 ], y = [23 , 24 ]))
119
120
df3 = pd .DataFrame (dict (y = [0.1 , 0.2 ]))
120
121
fig = px .scatter (x = df .y , y = df2 .y , hover_data = [df3 .y ])
121
- assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
122
- assert np .all (fig .data [0 ].y == np .array ([23 , 24 ]))
122
+ assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
123
+ assert np .all (fig .data [0 ].y == b64 ( np .array ([23 , 24 ]) ))
123
124
assert (
124
125
fig .data [0 ].hovertemplate
125
126
== "x=%{x}<br>y=%{y}<br>hover_data_0=%{customdata[0]}<extra></extra>"
@@ -129,8 +130,8 @@ def test_several_dataframes():
129
130
def test_name_heuristics ():
130
131
df = pd .DataFrame (dict (x = [0 , 1 ], y = [3 , 4 ], z = [0.1 , 0.2 ]))
131
132
fig = px .scatter (df , x = df .y , y = df .x , size = df .y )
132
- assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
133
- assert np .all (fig .data [0 ].y == np .array ([0 , 1 ]))
133
+ assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
134
+ assert np .all (fig .data [0 ].y == b64 ( np .array ([0 , 1 ]) ))
134
135
assert fig .data [0 ].hovertemplate == "y=%{marker.size}<br>x=%{y}<extra></extra>"
135
136
136
137
@@ -401,27 +402,27 @@ def test_splom_case():
401
402
assert len (fig .data [0 ].dimensions ) == len (iris .columns )
402
403
dic = {"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [7 , 8 , 9 ]}
403
404
fig = px .scatter_matrix (dic )
404
- assert np .all (fig .data [0 ].dimensions [0 ].values == np .array (dic ["a" ]))
405
+ assert np .all (fig .data [0 ].dimensions [0 ].values == b64 ( np .array (dic ["a" ]) ))
405
406
ar = np .arange (9 ).reshape ((3 , 3 ))
406
407
fig = px .scatter_matrix (ar )
407
- assert np .all (fig .data [0 ].dimensions [0 ].values == ar [:, 0 ])
408
+ assert np .all (fig .data [0 ].dimensions [0 ].values == b64 ( ar [:, 0 ]) )
408
409
409
410
410
411
def test_int_col_names ():
411
412
# DataFrame with int column names
412
413
lengths = pd .DataFrame (np .random .random (100 ))
413
414
fig = px .histogram (lengths , x = 0 )
414
- assert np .all (np .array (lengths ).flatten () == fig .data [0 ].x )
415
+ assert np .all (b64 ( np .array (lengths ).flatten () ) == fig .data [0 ].x )
415
416
# Numpy array
416
417
ar = np .arange (100 ).reshape ((10 , 10 ))
417
418
fig = px .scatter (ar , x = 2 , y = 8 )
418
- assert np .all (fig .data [0 ].x == ar [:, 2 ])
419
+ assert np .all (fig .data [0 ].x == b64 ( ar [:, 2 ]) )
419
420
420
421
421
422
def test_data_frame_from_dict ():
422
423
fig = px .scatter ({"time" : [0 , 1 ], "money" : [1 , 2 ]}, x = "time" , y = "money" )
423
424
assert fig .data [0 ].hovertemplate == "time=%{x}<br>money=%{y}<extra></extra>"
424
- assert np .all (fig .data [0 ].x == [0 , 1 ])
425
+ assert np .all (fig .data [0 ].x == _b64 ( [0 , 1 ]) )
425
426
426
427
427
428
def test_arguments_not_modified ():
@@ -485,13 +486,11 @@ def test_identity_map():
485
486
486
487
def test_constants ():
487
488
fig = px .scatter (x = px .Constant (1 ), y = [1 , 2 ])
488
- assert fig .data [0 ].x [0 ] == 1
489
- assert fig .data [0 ].x [1 ] == 1
489
+ assert fig .data [0 ].x == _b64 ([1 , 1 ])
490
490
assert "x=" in fig .data [0 ].hovertemplate
491
491
492
492
fig = px .scatter (x = px .Constant (1 , label = "time" ), y = [1 , 2 ])
493
- assert fig .data [0 ].x [0 ] == 1
494
- assert fig .data [0 ].x [1 ] == 1
493
+ assert fig .data [0 ].x == _b64 ([1 , 1 ])
495
494
assert "x=" not in fig .data [0 ].hovertemplate
496
495
assert "time=" in fig .data [0 ].hovertemplate
497
496
@@ -515,15 +514,12 @@ def test_constants():
515
514
516
515
def test_ranges ():
517
516
fig = px .scatter (x = px .Range (), y = [1 , 2 ], hover_data = [px .Range ()])
518
- assert fig .data [0 ].x [0 ] == 0
519
- assert fig .data [0 ].x [1 ] == 1
520
- assert fig .data [0 ].customdata [0 ][0 ] == 0
521
- assert fig .data [0 ].customdata [1 ][0 ] == 1
517
+ assert fig .data [0 ].x == _b64 ([0 , 1 ])
518
+ assert fig .data [0 ].customdata == _b64 ([[0 ], [1 ]])
522
519
assert "x=" in fig .data [0 ].hovertemplate
523
520
524
521
fig = px .scatter (x = px .Range (label = "time" ), y = [1 , 2 ])
525
- assert fig .data [0 ].x [0 ] == 0
526
- assert fig .data [0 ].x [1 ] == 1
522
+ assert fig .data [0 ].x == _b64 ([0 , 1 ])
527
523
assert "x=" not in fig .data [0 ].hovertemplate
528
524
assert "time=" in fig .data [0 ].hovertemplate
529
525
@@ -613,54 +609,54 @@ def test_x_or_y(fn):
613
609
categorical_df = pd .DataFrame (dict (col = categorical ), index = index )
614
610
615
611
fig = fn (x = numerical )
616
- assert list ( fig .data [0 ].x ) == numerical
617
- assert list ( fig .data [0 ].y ) == range_4
612
+ assert fig .data [0 ].x == _b64 ( numerical )
613
+ assert fig .data [0 ].y == _b64 ( range_4 )
618
614
assert fig .data [0 ].orientation == "h"
619
615
fig = fn (y = numerical )
620
- assert list ( fig .data [0 ].x ) == range_4
621
- assert list ( fig .data [0 ].y ) == numerical
616
+ assert fig .data [0 ].x == _b64 ( range_4 )
617
+ assert fig .data [0 ].y == _b64 ( numerical )
622
618
assert fig .data [0 ].orientation == "v"
623
619
fig = fn (numerical_df , x = "col" )
624
- assert list ( fig .data [0 ].x ) == numerical
625
- assert list ( fig .data [0 ].y ) == index
620
+ assert fig .data [0 ].x == _b64 ( numerical )
621
+ assert fig .data [0 ].y == _b64 ( index )
626
622
assert fig .data [0 ].orientation == "h"
627
623
fig = fn (numerical_df , y = "col" )
628
- assert list ( fig .data [0 ].x ) == index
629
- assert list ( fig .data [0 ].y ) == numerical
624
+ assert fig .data [0 ].x == _b64 ( index )
625
+ assert fig .data [0 ].y == _b64 ( numerical )
630
626
assert fig .data [0 ].orientation == "v"
631
627
632
628
if fn != px .bar :
633
629
fig = fn (x = categorical )
634
630
assert list (fig .data [0 ].x ) == categorical
635
- assert list ( fig .data [0 ].y ) == range_4
631
+ assert fig .data [0 ].y == _b64 ( range_4 )
636
632
assert fig .data [0 ].orientation == "h"
637
633
fig = fn (y = categorical )
638
- assert list ( fig .data [0 ].x ) == range_4
634
+ assert fig .data [0 ].x == _b64 ( range_4 )
639
635
assert list (fig .data [0 ].y ) == categorical
640
636
assert fig .data [0 ].orientation == "v"
641
637
fig = fn (categorical_df , x = "col" )
642
638
assert list (fig .data [0 ].x ) == categorical
643
- assert list ( fig .data [0 ].y ) == index
639
+ assert fig .data [0 ].y == _b64 ( index )
644
640
assert fig .data [0 ].orientation == "h"
645
641
fig = fn (categorical_df , y = "col" )
646
- assert list ( fig .data [0 ].x ) == index
642
+ assert fig .data [0 ].x == _b64 ( index )
647
643
assert list (fig .data [0 ].y ) == categorical
648
644
assert fig .data [0 ].orientation == "v"
649
645
650
646
else :
651
647
fig = fn (x = categorical )
652
648
assert list (fig .data [0 ].x ) == categorical
653
- assert list ( fig .data [0 ].y ) == constant
649
+ assert fig .data [0 ].y == _b64 ( constant )
654
650
assert fig .data [0 ].orientation == "v"
655
651
fig = fn (y = categorical )
656
- assert list ( fig .data [0 ].x ) == constant
652
+ assert fig .data [0 ].x == _b64 ( constant )
657
653
assert list (fig .data [0 ].y ) == categorical
658
654
assert fig .data [0 ].orientation == "h"
659
655
fig = fn (categorical_df , x = "col" )
660
656
assert list (fig .data [0 ].x ) == categorical
661
- assert list ( fig .data [0 ].y ) == constant
657
+ assert fig .data [0 ].y == _b64 ( constant )
662
658
assert fig .data [0 ].orientation == "v"
663
659
fig = fn (categorical_df , y = "col" )
664
- assert list ( fig .data [0 ].x ) == constant
660
+ assert fig .data [0 ].x == _b64 ( constant )
665
661
assert list (fig .data [0 ].y ) == categorical
666
662
assert fig .data [0 ].orientation == "h"
0 commit comments