Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Enums as values for Widget/Viewer, refactor test folder structure #834

Merged
merged 1 commit into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions python/perspective/perspective/tests/core/test_aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
# the Apache License 2.0. The full license can be found in the LICENSE file.
#

from perspective.core import Aggregate
from perspective import PerspectiveWidget
from pytest import raises
from perspective import PerspectiveError, PerspectiveViewer,\
PerspectiveWidget, Aggregate


class TestAggregates:
Expand All @@ -20,3 +21,59 @@ def test_aggregates_widget_load(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data, aggregates=aggs)
assert widget.aggregates == aggs

def test_aggregates_widget_setattr(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data)
widget.aggregates = {
"a": Aggregate.ANY,
"b": Aggregate.LAST
}
assert widget.aggregates == {
"a": "any",
"b": "last"
}

def test_aggregates_widget_load_invalid(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
with raises(PerspectiveError):
PerspectiveWidget(data, aggregates={"a": "?"})

def test_aggregates_widget_setattr_invalid(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data)
with raises(PerspectiveError):
widget.aggregates = {"a": "?"}

def test_aggregates_widget_init_all(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
for agg in Aggregate:
widget = PerspectiveWidget(data, aggregates={"a": agg})
assert widget.aggregates == {"a": agg.value}

def test_aggregates_widget_set_all(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data)
for agg in Aggregate:
widget.aggregates = {"a": agg}
assert widget.aggregates == {"a": agg.value}

def test_aggregates_viewer_load(self):
viewer = PerspectiveViewer(aggregates={"a": Aggregate.AVG})
assert viewer.aggregates == {"a": "avg"}

def test_aggregates_viewer_setattr(self):
viewer = PerspectiveViewer()
viewer.aggregates = {"a": Aggregate.AVG}
assert viewer.aggregates == {"a": "avg"}

def test_aggregates_viewer_init_all(self):
for agg in Aggregate:
viewer = PerspectiveViewer(aggregates={"a": agg})
assert viewer.aggregates == {"a": agg.value}

def test_aggregates_viewer_set_all(self):
viewer = PerspectiveViewer()
for agg in Aggregate:
viewer.aggregates = {"a": agg}
assert viewer.aggregates == {"a": agg.value}
55 changes: 53 additions & 2 deletions python/perspective/perspective/tests/core/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
# the Apache License 2.0. The full license can be found in the LICENSE file.
#

from perspective.core import Plugin
from perspective import PerspectiveWidget
from pytest import raises
from perspective import PerspectiveError, PerspectiveViewer,\
PerspectiveWidget, Plugin


class TestPlugin:
Expand All @@ -16,3 +17,53 @@ def test_plugin_widget_load(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data, plugin=Plugin.XBAR)
assert widget.plugin == "x_bar"

def test_plugin_widget_setattr(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data)
widget.plugin = Plugin.XBAR
assert widget.plugin == "x_bar"

def test_plugin_widget_load_invalid(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
with raises(PerspectiveError):
PerspectiveWidget(data, plugin="?")

def test_plugin_widget_setattr_invalid(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data)
with raises(PerspectiveError):
widget.plugin = "?"

def test_plugin_widget_init_all(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
for plugin in Plugin:
widget = PerspectiveWidget(data, plugin=plugin)
assert widget.plugin == plugin.value

def test_plugin_widget_set_all(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data)
for plugin in Plugin:
widget.plugin = plugin
assert widget.plugin == plugin.value

def test_plugin_viewer_load(self):
viewer = PerspectiveViewer(plugin=Plugin.XBAR)
assert viewer.plugin == "x_bar"

def test_plugin_viewer_setattr(self):
viewer = PerspectiveViewer()
viewer.plugin = Plugin.XBAR
assert viewer.plugin == "x_bar"

def test_plugin_viewer_init_all(self):
for plugin in Plugin:
viewer = PerspectiveViewer(plugin=plugin)
assert viewer.plugin == plugin.value

def test_plugin_viewer_set_all(self):
viewer = PerspectiveViewer()
for plugin in Plugin:
viewer.plugin = plugin
assert viewer.plugin == plugin.value
69 changes: 69 additions & 0 deletions python/perspective/perspective/tests/core/test_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
################################################################################
#
# Copyright (c) 2019, the Perspective Authors.
#
# This file is part of the Perspective library, distributed under the terms of
# the Apache License 2.0. The full license can be found in the LICENSE file.
#

from pytest import raises
from perspective import PerspectiveError, PerspectiveViewer,\
PerspectiveWidget, Sort


class TestSort(object):

def test_sort_widget_load(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data, sort=[["a", Sort.DESC]])
assert widget.sort == [["a", "desc"]]

def test_sort_widget_setattr(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data)
widget.sort = [["a", Sort.ASC_ABS]]
assert widget.sort == [["a", "asc abs"]]

def test_sort_widget_load_invalid(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
with raises(PerspectiveError):
PerspectiveWidget(data, sort=[["a", "?"]])

def test_sort_widget_setattr_invalid(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data)
with raises(PerspectiveError):
widget.sort = [["a", "?"]]

def test_sort_widget_init_all(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
for sort in Sort:
widget = PerspectiveWidget(data, sort=[["a", sort]])
assert widget.sort == [["a", sort.value]]

def test_sort_widget_set_all(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data)
for sort in Sort:
widget.sort = [["a", sort]]
assert widget.sort == [["a", sort.value]]

def test_sort_viewer_load(self):
viewer = PerspectiveViewer(sort=[["a", Sort.COL_ASC_ABS]])
assert viewer.sort == [["a", "col asc abs"]]

def test_sort_viewer_setattr(self):
viewer = PerspectiveViewer()
viewer.sort = [["a", Sort.COL_DESC_ABS]]
assert viewer.sort == [["a", "col desc abs"]]

def test_sort_viewer_init_all(self):
for sort in Sort:
viewer = PerspectiveViewer(sort=[["a", sort]])
assert viewer.sort == [["a", sort.value]]

def test_sort_viewer_set_all(self):
viewer = PerspectiveViewer()
for sort in Sort:
viewer.sort = [["a", sort]]
assert viewer.sort == [["a", sort.value]]
4 changes: 2 additions & 2 deletions python/perspective/perspective/viewer/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def validate_plugin(plugin):
return plugin.value
elif isinstance(plugin, string_types):
if plugin not in Plugin.options():
raise PerspectiveError('Unrecognized `plugin`: %s', plugin)
raise PerspectiveError("Unrecognized `plugin`: {0}".format(plugin))
return plugin
else:
raise PerspectiveError('Cannot parse `plugin` of type: %s', str(type(plugin)))
raise PerspectiveError("Cannot parse `plugin` of type: {0}".format(str(type(plugin))))


def validate_columns(columns):
Expand Down
17 changes: 17 additions & 0 deletions python/perspective/perspective/viewer/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class PerspectiveViewer(PerspectiveTraitlets, object):
around creating views, loading data, and updating data.
'''

# Keep track of attributes that can be set via Enum, and their
# validation methods.
ENUM_VALIDATORS = {
"plugin": validate_plugin,
"aggregates": validate_aggregates,
"sort": validate_sort
}

def __init__(self,
plugin='hypergrid',
columns=None,
Expand Down Expand Up @@ -231,3 +239,12 @@ def _new_view(self):

self.manager.host_view(name, view)
self.view_name = name

def __setattr__(self, name, value):
"""Override __setattr__ in order to allow Enums to be validated
through Traitlets."""
if name in PerspectiveViewer.ENUM_VALIDATORS:
# call the validator and set
validated = PerspectiveViewer.ENUM_VALIDATORS[name](value)
value = validated
super(PerspectiveViewer, self).__setattr__(name, value)