3131from datafusion import functions as f
3232from datafusion .expr import Window
3333from pyarrow .csv import write_csv
34+ from datafusion .context import DataframeDisplayConfig
3435
3536
3637@pytest .fixture
@@ -51,6 +52,102 @@ def df():
5152 return ctx .from_arrow (batch )
5253
5354
55+ def test_display_config ():
56+ # Test display_config initialization
57+ config = DataframeDisplayConfig (
58+ max_table_bytes = 1024 ,
59+ min_table_rows = 10 ,
60+ max_cell_length = 15 ,
61+ max_table_rows_in_repr = 5 ,
62+ )
63+
64+ assert config .max_table_bytes == 1024
65+ assert config .min_table_rows == 10
66+ assert config .max_cell_length == 15
67+ assert config .max_table_rows_in_repr == 5
68+
69+ # Test property setters
70+ config .max_table_bytes = 2048
71+ config .min_table_rows = 20
72+ config .max_cell_length = 30
73+ config .max_table_rows_in_repr = 10
74+
75+ assert config .max_table_bytes == 2048
76+ assert config .min_table_rows == 20
77+ assert config .max_cell_length == 30
78+ assert config .max_table_rows_in_repr == 10
79+
80+ # Test property setter validation
81+ with pytest .raises (ValueError , match = "max_table_bytes must be greater than 0" ):
82+ config .max_table_bytes = 0
83+
84+ with pytest .raises (ValueError , match = "min_table_rows must be greater than 0" ):
85+ config .min_table_rows = - 1
86+
87+ with pytest .raises (ValueError , match = "max_cell_length must be greater than 0" ):
88+ config .max_cell_length = 0
89+
90+ with pytest .raises (
91+ ValueError , match = "max_table_rows_in_repr must be greater than 0"
92+ ):
93+ config .max_table_rows_in_repr = - 5
94+
95+
96+ def test_session_with_display_config ():
97+ # Test with_display_config returns a new context with updated config
98+ ctx = SessionContext ()
99+
100+ # Verify the default values are used initially
101+ df = ctx .from_pylist ([{"a" : 1 , "b" : "x" * 50 , "c" : 3 }] * 100 )
102+ html_repr = df ._repr_html_ ()
103+
104+ # Create a new context with custom display config
105+ ctx2 = ctx .with_display_config (
106+ max_table_bytes = 1024 ,
107+ min_table_rows = 5 ,
108+ max_cell_length = 10 ,
109+ max_table_rows_in_repr = 3 ,
110+ )
111+
112+ # Create a dataframe with the same data but using the new context
113+ df2 = ctx2 .from_pylist ([{"a" : 1 , "b" : "x" * 50 , "c" : 3 }] * 100 )
114+ html_repr2 = df2 ._repr_html_ ()
115+
116+ # The HTML representation should be different with different display configs
117+ assert html_repr != html_repr2
118+
119+ # Check that the second representation has the short cell data based on the configured length
120+ assert f'<span class="expandable" id="' in html_repr2
121+ assert f'>{ ("x" * 10 )} </span>' in html_repr2
122+
123+
124+ def test_display_config_in_init ():
125+ # Test providing display config directly in SessionContext constructor
126+ display_config = DataframeDisplayConfig (
127+ max_table_bytes = 1024 ,
128+ min_table_rows = 5 ,
129+ max_cell_length = 10 ,
130+ max_table_rows_in_repr = 3 ,
131+ )
132+
133+ ctx = SessionContext ()
134+ df1 = ctx .from_pylist ([{"a" : 1 , "b" : "x" * 50 , "c" : 3 }] * 100 )
135+ html_repr1 = df1 ._repr_html_ ()
136+
137+ # Create a context with custom display config through the with_display_config method
138+ ctx2 = ctx .with_display_config (
139+ max_table_bytes = 1024 ,
140+ min_table_rows = 5 ,
141+ max_cell_length = 10 ,
142+ max_table_rows_in_repr = 3 ,
143+ )
144+ df2 = ctx2 .from_pylist ([{"a" : 1 , "b" : "x" * 50 , "c" : 3 }] * 100 )
145+ html_repr2 = df2 ._repr_html_ ()
146+
147+ # Both methods should result in equivalent display configuration
148+ assert html_repr1 != html_repr2
149+
150+
54151@pytest .fixture
55152def struct_df ():
56153 ctx = SessionContext ()
0 commit comments