Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xinrong-meng committed Mar 31, 2021
1 parent 1a10666 commit 8279d16
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
29 changes: 29 additions & 0 deletions databricks/koalas/tests/indexes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,32 @@ def test_indexer_at_time(self):
NotImplementedError,
lambda: ks.DatetimeIndex([0]).indexer_at_time("00:00:00", asof=True),
)

def test_arithmetic_op_exceptions(self):
for kidx, pidx in self.idx_pairs:
py_datetime = pidx.to_pydatetime()
for other in [1, 0.1, kidx, kidx.to_series().reset_index(drop=True), py_datetime]:
expected_err_msg = "addition can not be applied to date times."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kidx + other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other + kidx)

expected_err_msg = "multiplication can not be applied to date times."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kidx * other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other * kidx)

expected_err_msg = "division can not be applied to date times."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kidx / other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other / kidx)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kidx // other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other // kidx)

expected_err_msg = "modulo can not be applied to date times."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kidx % other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other % kidx)

for other in [1, 0.1, py_datetime]:
expected_err_msg = "datetime subtraction can only be applied to datetime series."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kidx - other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other - kidx)

self.assertRaises(NotImplementedError, lambda: py_datetime - kidx)
31 changes: 31 additions & 0 deletions databricks/koalas/tests/test_series_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,37 @@ def test_timestamp_subtraction(self):
with self.assertRaisesRegex(TypeError, expected_error_message):
1 - kdf["a"]

def test_arithmetic_op_exceptions(self):
kser = self.ks_start_date
py_datetime = self.pd_start_date.dt.to_pydatetime()
datetime_index = ks.Index(self.pd_start_date)

for other in [1, 0.1, kser, datetime_index, py_datetime]:
expected_err_msg = "addition can not be applied to date times."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kser + other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other + kser)

expected_err_msg = "multiplication can not be applied to date times."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kser * other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other * kser)

expected_err_msg = "division can not be applied to date times."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kser / other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other / kser)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kser // other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other // kser)

expected_err_msg = "modulo can not be applied to date times."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kser % other)
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: other % kser)

ks.set_option("compute.ops_on_diff_frames", True)
for other in [1, 0.1, py_datetime]:
expected_err_msg = "datetime subtraction can only be applied to datetime series."
self.assertRaisesRegex(TypeError, expected_err_msg, lambda: kser - other)

self.assertRaises(NotImplementedError, lambda: py_datetime - kser)

def test_date_subtraction(self):
pdf = self.pdf1
kdf = ks.from_pandas(pdf)
Expand Down

0 comments on commit 8279d16

Please sign in to comment.