Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 23, 2020
1 parent 13d5039 commit 2d79470
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions holoviews/tests/util/testtransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def setUp(self):

# Assertion helpers

def check_apply(self, expr, expected, skip_dask=False):
def check_apply(self, expr, expected, skip_dask=False, skip_no_index=False):
if np.isscalar(expected):
# Pandas input
self.assertEqual(
Expand All @@ -69,10 +69,11 @@ def check_apply(self, expr, expected, skip_dask=False):

# Check using dataset backed by pandas DataFrame
# keep_index=False
np.testing.assert_equal(
expr.apply(self.dataset),
expected.values
)
if not skip_no_index:
np.testing.assert_equal(
expr.apply(self.dataset),
expected.values
)
# keep_index=True
pd.testing.assert_series_equal(
expr.apply(self.dataset, keep_index=True),
Expand All @@ -87,20 +88,22 @@ def check_apply(self, expr, expected, skip_dask=False):
expected_dask = dd.from_pandas(expected, npartitions=2)

# keep_index=False, compute=False
da.assert_eq(
expr.apply(self.dataset_dask, compute=False), expected_dask.values
)
if not skip_no_index:
da.assert_eq(
expr.apply(self.dataset_dask, compute=False), expected_dask.values
)
# keep_index=True, compute=False
dd.assert_eq(
expr.apply(self.dataset_dask, keep_index=True, compute=False),
expected_dask,
check_names=False
)
# keep_index=False, compute=True
np.testing.assert_equal(
expr.apply(self.dataset_dask, compute=True),
expected_dask.values.compute()
)
if not skip_no_index:
np.testing.assert_equal(
expr.apply(self.dataset_dask, compute=True),
expected_dask.values.compute()
)
# keep_index=True, compute=True
pd.testing.assert_series_equal(
expr.apply(self.dataset_dask, keep_index=True, compute=True),
Expand Down Expand Up @@ -226,6 +229,10 @@ def test_log10_transform(self):

# Custom functions

def test_str_astype(self):
expr = dim('int').str()
self.check_apply(expr, self.linear_ints.astype(str))

def test_norm_transform(self):
expr = dim('int').norm()
self.check_apply(expr, (self.linear_ints-1)/9.)
Expand Down Expand Up @@ -256,6 +263,13 @@ def test_bin_transform_with_labels(self):
)
self.check_apply(expr, expected)

def test_bin_transform_with_labels(self):
expr = dim('int').bin([0, 5, 10], ['A', 'B'])
expected = pd.Series(
['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B']
)
self.check_apply(expr, expected)

def test_categorize_transform_list(self):
expr = dim('categories').categorize(['circle', 'square', 'triangle'])
expected = pd.Series(
Expand Down Expand Up @@ -284,6 +298,12 @@ def test_categorize_transform_dict_with_default(self):
# We don't skip dask because results are stable across partitions
self.check_apply(expr, expected)

# Check accesors

def test_str_pandas_accessor(self):
expr = dim('categories').str.lower()
self.check_apply(expr, self.repeating.str.lower(), skip_no_index=True)

# Numpy functions

def test_digitize(self):
Expand Down Expand Up @@ -332,6 +352,10 @@ def test_multi_operator_expression_repr(self):
self.assertEqual(repr(((dim('float')-2)*3)**2),
"((dim('float')-2)*3)**2")

def test_accessor_repr(self):
self.assertEqual(repr(dim('date').dt.year),
"dim('date').dt.year")

# Applies method

def test_multi_dim_expression_applies(self):
Expand Down

0 comments on commit 2d79470

Please sign in to comment.