Skip to content

Commit

Permalink
Add missing tests for indexes (#692)
Browse files Browse the repository at this point in the history
 I found that there was not enough exception testing for the names property.

![image](https://user-images.githubusercontent.com/44108233/63647783-526ad900-c761-11e9-8140-61201cdf268e.png)


So I added tests for those exceptions,

And also added tests for some other code that isn't being tested yet.

It's very appreciate it if someone check it out when available. :)

Thanks.
  • Loading branch information
itholic authored and HyukjinKwon committed Aug 26, 2019
1 parent 34088a4 commit 8e0a2a8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions databricks/koalas/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import pyspark

import databricks.koalas as ks
from databricks.koalas.generic import max_display_count
from databricks.koalas.exceptions import PandasNotImplementedError
from databricks.koalas.missing.indexes import _MissingPandasLikeIndex, _MissingPandasLikeMultiIndex
from databricks.koalas.testing.utils import ReusedSQLTestCase, TestUtils
Expand Down Expand Up @@ -55,6 +56,26 @@ def test_index(self):
kdf = ks.from_pandas(pdf)
self.assert_eq(kdf.index, pdf.index)

def test_index_getattr(self):
kidx = self.kdf.index
item = 'databricks'

expected_error_message = ("'Index' object has no attribute '{}'".format(item))
with self.assertRaisesRegex(AttributeError, expected_error_message):
kidx.__getattr__(item)

def test_multi_index_getattr(self):
arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
idx = pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
pdf = pd.DataFrame(np.random.randn(4, 5), idx)
kdf = ks.from_pandas(pdf)
kidx = kdf.index
item = 'databricks'

expected_error_message = ("'MultiIndex' object has no attribute '{}'".format(item))
with self.assertRaisesRegex(AttributeError, expected_error_message):
kidx.__getattr__(item)

def test_to_series(self):
pidx = self.pdf.index
kidx = self.kdf.index
Expand Down Expand Up @@ -93,6 +114,14 @@ def test_index_names(self):
self.assertEqual(kidx.names, pidx.names)
self.assert_eq(kidx, pidx)

with self.assertRaisesRegex(ValueError, "Names must be a list-like"):
kidx.names = 'hi'

expected_error_message = ("Length of new names must be {}, got {}"
.format(len(kdf._internal.index_map), len(['0', '1'])))
with self.assertRaisesRegex(ValueError, expected_error_message):
kidx.names = ['0', '1']

def test_multi_index_names(self):
arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
idx = pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
Expand Down
10 changes: 10 additions & 0 deletions databricks/koalas/tests/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def test_repr_series(self):
kser = ks.range(max_display_count + 1).id
self.assertTrue("Showing only the first" in repr(kser))

def test_repr_indexes(self):
kdf = ks.range(max_display_count)
kidx = kdf.index
self.assertTrue("Showing only the first" not in repr(kidx))
self.assert_eq(repr(kidx), repr(kidx.to_pandas()))

kdf = ks.range(max_display_count + 1)
kidx = kdf.index
self.assertTrue("Showing only the first" in repr(kidx))

def test_html_repr(self):
kdf = ks.range(max_display_count)
self.assertTrue("Showing only the first" not in kdf._repr_html_())
Expand Down

0 comments on commit 8e0a2a8

Please sign in to comment.