|
2 | 2 | import plotly.graph_objects as go
|
3 | 3 | import pandas as pd
|
4 | 4 | from plotly.express._core import build_dataframe
|
5 |
| -from pandas.util.testing import assert_frame_equal |
| 5 | +from pandas.testing import assert_frame_equal |
| 6 | +import pytest |
6 | 7 |
|
7 | 8 |
|
8 | 9 | def test_wide_mode_external():
|
@@ -76,63 +77,47 @@ def test_wide_mode_labels_external():
|
76 | 77 | assert fig.layout.legend.title.text == "my column"
|
77 | 78 |
|
78 | 79 |
|
79 |
| -def test_wide_mode_internal(): |
80 |
| - # here we do basic exhaustive testing of the various graph_object permutations |
81 |
| - # via build_dataframe directly, which leads to more compact test code: |
82 |
| - # we pass in args (which includes df) and look at how build_dataframe mutates |
83 |
| - # both args and the df, and assume that since the rest of the downstream PX |
84 |
| - # machinery has not wide-mode-specific code, and the tests above pass, that this is |
85 |
| - # enough to prove things work |
86 |
| - |
| 80 | +# here we do basic exhaustive testing of the various graph_object permutations |
| 81 | +# via build_dataframe directly, which leads to more compact test code: |
| 82 | +# we pass in args (which includes df) and look at how build_dataframe mutates |
| 83 | +# both args and the df, and assume that since the rest of the downstream PX |
| 84 | +# machinery has not wide-mode-specific code, and the tests above pass, that this is |
| 85 | +# enough to prove things work |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "trace_type,x,y,color", |
| 88 | + [ |
| 89 | + (go.Scatter, "index", "_value_", "_column_"), |
| 90 | + (go.Bar, "index", "_value_", "_column_"), |
| 91 | + (go.Box, "_column_", "_value_", None), |
| 92 | + (go.Violin, "_column_", "_value_", None), |
| 93 | + (go.Histogram, "_value_", None, "_column_"), |
| 94 | + ], |
| 95 | +) |
| 96 | +@pytest.mark.parametrize( |
| 97 | + "orientation", [None, "v", "h"], |
| 98 | +) |
| 99 | +def test_wide_mode_internal(trace_type, x, y, color, orientation): |
87 | 100 | df_in = pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, 6]), index=[11, 12, 13])
|
88 |
| - |
89 |
| - def extract_and_check_df(args_out): |
90 |
| - df_out = args_out.pop("data_frame") |
91 |
| - assert_frame_equal( |
92 |
| - df_out.sort_index(axis=1), |
93 |
| - pd.DataFrame( |
94 |
| - dict( |
95 |
| - index=[11, 12, 13, 11, 12, 13], |
96 |
| - _column_=["a", "a", "a", "b", "b", "b"], |
97 |
| - _value_=[1, 2, 3, 4, 5, 6], |
98 |
| - ) |
99 |
| - ).sort_index(axis=1), |
100 |
| - ) |
101 |
| - return args_out |
102 |
| - |
103 |
| - for trace_type in [go.Scatter, go.Bar]: |
104 |
| - args_in = dict(data_frame=df_in.copy(), color=None) |
105 |
| - args_out = extract_and_check_df(build_dataframe(args_in, trace_type)) |
106 |
| - assert args_out == dict( |
107 |
| - x="index", y="_value_", color="_column_", orientation="v" |
108 |
| - ) |
109 |
| - |
110 |
| - # now we check with orientation |
111 |
| - args_in = dict(data_frame=df_in.copy(), color=None, orientation="h") |
112 |
| - args_out = extract_and_check_df(build_dataframe(args_in, trace_type)) |
113 |
| - assert args_out == dict( |
114 |
| - y="index", x="_value_", color="_column_", orientation="h" |
115 |
| - ) |
116 |
| - |
117 |
| - for trace_type in [go.Violin, go.Box]: |
118 |
| - args_in = dict(data_frame=df_in.copy(), color=None) |
119 |
| - args_out = extract_and_check_df(build_dataframe(args_in, trace_type)) |
120 |
| - assert args_out == dict(x="_column_", y="_value_", color=None, orientation="v") |
121 |
| - |
122 |
| - # now we check with orientation |
123 |
| - args_in = dict(data_frame=df_in.copy(), color=None, orientation="h") |
124 |
| - args_out = extract_and_check_df(build_dataframe(args_in, trace_type)) |
125 |
| - assert args_out == dict(y="_column_", x="_value_", color=None, orientation="h") |
126 |
| - |
127 |
| - for trace_type in [go.Histogram]: |
128 |
| - args_in = dict(data_frame=df_in.copy(), color=None) |
129 |
| - args_out = extract_and_check_df(build_dataframe(args_in, trace_type)) |
130 |
| - assert args_out == dict(x="_value_", color="_column_", orientation="v") |
131 |
| - |
132 |
| - # now we check with orientation |
133 |
| - args_in = dict(data_frame=df_in.copy(), color=None, orientation="h") |
134 |
| - args_out = extract_and_check_df(build_dataframe(args_in, trace_type)) |
135 |
| - assert args_out == dict(y="_value_", color="_column_", orientation="h") |
| 101 | + args_in = dict(data_frame=df_in, color=None, orientation=orientation) |
| 102 | + args_out = build_dataframe(args_in, trace_type) |
| 103 | + df_out = args_out.pop("data_frame") |
| 104 | + assert_frame_equal( |
| 105 | + df_out.sort_index(axis=1), |
| 106 | + pd.DataFrame( |
| 107 | + dict( |
| 108 | + index=[11, 12, 13, 11, 12, 13], |
| 109 | + _column_=["a", "a", "a", "b", "b", "b"], |
| 110 | + _value_=[1, 2, 3, 4, 5, 6], |
| 111 | + ) |
| 112 | + ).sort_index(axis=1), |
| 113 | + ) |
| 114 | + for arg in ["x", "y"]: |
| 115 | + if arg not in args_out: |
| 116 | + args_out[arg] = None # so this doesn't fail for histogram |
| 117 | + if orientation is None or orientation == "v": |
| 118 | + assert args_out == dict(x=x, y=y, color=color, orientation="v") |
| 119 | + else: |
| 120 | + assert args_out == dict(x=y, y=x, color=color, orientation="h") |
136 | 121 |
|
137 | 122 |
|
138 | 123 | def test_wide_mode_internal_special_cases():
|
|
0 commit comments