handle test · tm9k1/pandas@a4a2933 (original) (raw)

Original file line number Diff line number Diff line change
@@ -278,17 +278,32 @@ def test_compare_array(self, data, all_compare_operators):
278 278 self._compare_other(s, data, op_name, other)
279 279
280 280
281 +class DecimalArrayWithoutFromSequence(DecimalArray):
282 +"""Helper class for testing error handling in _from_sequence."""
283 +def _from_sequence(cls, scalars, dtype=None, copy=False):
284 +raise KeyError("For the test")
285 +
286 +
281 287 def test_combine_from_sequence_raises():
282 288 # https://github.com/pandas-dev/pandas/issues/22850
283 -class BadDecimalArray(DecimalArray):
284 -def _from_sequence(cls, scalars, dtype=None, copy=False):
285 -raise KeyError("For the test")
286 -
287 -ser = pd.Series(BadDecimalArray([decimal.Decimal("1.0"),
288 -decimal.Decimal("2.0")]))
289 +ser = pd.Series(DecimalArrayWithoutFromSequence([
290 +decimal.Decimal("1.0"),
291 +decimal.Decimal("2.0")
292 + ]))
289 293 result = ser.combine(ser, operator.add)
290 294
291 295 # note: object dtype
292 296 expected = pd.Series([decimal.Decimal("2.0"),
293 297 decimal.Decimal("4.0")], dtype="object")
294 298 tm.assert_series_equal(result, expected)
299 +
300 +
301 +def test_scalar_ops_from_sequence_raises():
302 +arr = DecimalArrayWithoutFromSequence([
303 +decimal.Decimal("1.0"),
304 +decimal.Decimal("2.0")
305 + ])
306 +result = arr + arr
307 +expected = np.array([decimal.Decimal("2.0"), decimal.Decimal("4.0")],
308 +dtype="object")
309 +tm.assert_numpy_array_equal(result, expected)