@@ -87,6 +87,69 @@ def test_chart_can_be_serialized(self):
8787 assert len (json_str ) > 0
8888
8989
90+ class TestRenderAltairChartTypeDetection :
91+ """Tests for correct Altair chart type detection based on spec structure."""
92+
93+ def test_simple_chart_returns_layer_chart (self ):
94+ """Simple DRAW specs produce LayerChart (ggsql always wraps in layer)."""
95+ df = pl .DataFrame ({"x" : [1 , 2 , 3 ], "y" : [10 , 20 , 30 ]})
96+ chart = ggsql .render_altair (df , "VISUALISE x, y DRAW point" )
97+ # ggsql wraps all charts in a layer
98+ assert isinstance (chart , altair .LayerChart )
99+
100+ def test_layered_chart_can_round_trip (self ):
101+ """LayerChart can be converted to dict and back."""
102+ df = pl .DataFrame ({"x" : [1 , 2 , 3 ], "y" : [10 , 20 , 30 ]})
103+ chart = ggsql .render_altair (df , "VISUALISE x, y DRAW point" )
104+
105+ # Convert to dict and back
106+ spec = chart .to_dict ()
107+ assert "layer" in spec
108+
109+ # Should be able to recreate from dict
110+ recreated = altair .LayerChart .from_dict (spec )
111+ assert isinstance (recreated , altair .LayerChart )
112+
113+ def test_faceted_chart_returns_facet_chart (self ):
114+ """FACET WRAP specs produce FacetChart."""
115+ df = pl .DataFrame ({
116+ "x" : [1 , 2 , 3 , 4 , 5 , 6 ],
117+ "y" : [10 , 20 , 30 , 40 , 50 , 60 ],
118+ "group" : ["A" , "A" , "A" , "B" , "B" , "B" ],
119+ })
120+ # Need validate=False because ggsql produces v6 specs
121+ chart = ggsql .render_altair (df , "VISUALISE x, y FACET WRAP group DRAW point" , validate = False )
122+ assert isinstance (chart , altair .FacetChart )
123+
124+ def test_faceted_chart_can_round_trip (self ):
125+ """FacetChart can be converted to dict and back."""
126+ df = pl .DataFrame ({
127+ "x" : [1 , 2 , 3 , 4 , 5 , 6 ],
128+ "y" : [10 , 20 , 30 , 40 , 50 , 60 ],
129+ "group" : ["A" , "A" , "A" , "B" , "B" , "B" ],
130+ })
131+ chart = ggsql .render_altair (df , "VISUALISE x, y FACET WRAP group DRAW point" , validate = False )
132+
133+ # Convert to dict (skip validation for ggsql specs)
134+ spec = chart .to_dict (validate = False )
135+ assert "facet" in spec or "spec" in spec
136+
137+ # Should be able to recreate from dict (with validation disabled)
138+ recreated = altair .FacetChart .from_dict (spec , validate = False )
139+ assert isinstance (recreated , altair .FacetChart )
140+
141+ def test_chart_with_color_encoding (self ):
142+ """Charts with color encoding still return correct type."""
143+ df = pl .DataFrame ({
144+ "x" : [1 , 2 , 3 , 4 ],
145+ "y" : [10 , 20 , 30 , 40 ],
146+ "category" : ["A" , "B" , "A" , "B" ],
147+ })
148+ chart = ggsql .render_altair (df , "VISUALISE x, y, category AS color DRAW point" )
149+ # Should still be a LayerChart (ggsql wraps in layer)
150+ assert isinstance (chart , altair .LayerChart )
151+
152+
90153class TestRenderAltairErrorHandling :
91154 """Tests for error handling in render_altair()."""
92155
0 commit comments