@@ -179,3 +179,81 @@ def test_tabyl_with_percentages_all():
179
179
assert (
180
180
result_numeric .select_dtypes (include = ["float" , "int" ]).max ().max () <= 1
181
181
)
182
+
183
+
184
+ @pytest .mark .functions
185
+ def test_tabyl_missing_col1 ():
186
+ """
187
+ Test that tabyl raises an error if col1 is missing from the DataFrame.
188
+ """
189
+ data = {"Category" : ["A" , "B" ], "Subcategory" : ["X" , "Y" ]}
190
+ df = pd .DataFrame (data )
191
+
192
+ with pytest .raises (
193
+ ValueError , match = "Column 'Region' is not in the DataFrame."
194
+ ):
195
+ tabyl (df , "Region" )
196
+
197
+
198
+ @pytest .mark .functions
199
+ def test_tabyl_missing_col2 ():
200
+ """
201
+ Test that tabyl raises an error if col2 is missing from the DataFrame.
202
+ """
203
+ data = {"Category" : ["A" , "B" ], "Subcategory" : ["X" , "Y" ]}
204
+ df = pd .DataFrame (data )
205
+
206
+ with pytest .raises (
207
+ ValueError , match = "Column 'Value' is not in the DataFrame."
208
+ ):
209
+ tabyl (df , "Category" , "Value" )
210
+
211
+
212
+ @pytest .mark .functions
213
+ def test_tabyl_missing_col3 ():
214
+ """
215
+ Test that tabyl raises an error if col3 is missing from the DataFrame.
216
+ """
217
+ data = {"Category" : ["A" , "B" ], "Subcategory" : ["X" , "Y" ]}
218
+ df = pd .DataFrame (data )
219
+
220
+ with pytest .raises (
221
+ ValueError , match = "Column 'Region' is not in the DataFrame."
222
+ ):
223
+ tabyl (df , "Category" , "Subcategory" , "Region" )
224
+
225
+
226
+ @pytest .mark .functions
227
+ def test_tabyl_single_column ():
228
+ """
229
+ Test that tabyl works correctly with only col1 specified.
230
+ """
231
+ data = {"Category" : ["A" , "B" , "A" , "C" , "B" , "A" , "C" ]}
232
+ df = pd .DataFrame (data )
233
+
234
+ result = tabyl (df , "Category" )
235
+ assert result .shape [0 ] == 3 # Three unique values in 'Category'
236
+ assert (
237
+ result ["count" ].sum () == 7
238
+ ) # Total count should match the number of rows
239
+
240
+
241
+ @pytest .mark .functions
242
+ def test_tabyl_invalid_percentage_axis ():
243
+ """
244
+ Test that tabyl raises an error for invalid percentage_axis values.
245
+ """
246
+ data = {"Category" : ["A" , "B" ], "Subcategory" : ["X" , "Y" ]}
247
+ df = pd .DataFrame (data )
248
+
249
+ with pytest .raises (
250
+ ValueError ,
251
+ match = "`percentage_axis` must be one of 'row', 'col', or 'all'." ,
252
+ ):
253
+ tabyl (
254
+ df ,
255
+ "Category" ,
256
+ "Subcategory" ,
257
+ show_percentages = True ,
258
+ percentage_axis = "invalid" ,
259
+ )
0 commit comments