Skip to content

Commit

Permalink
Apply lint to perspective tests
Browse files Browse the repository at this point in the history
  • Loading branch information
no author authored and texodus committed Jul 23, 2023
1 parent f21b69b commit abfaa27
Show file tree
Hide file tree
Showing 56 changed files with 1,971 additions and 6,216 deletions.
1 change: 0 additions & 1 deletion python/perspective/perspective/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
# ┃ This file is part of the Perspective library, distributed under the terms ┃
# ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
# ┃ This file is part of the Perspective library, distributed under the terms ┃
# ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ def test_widget_client_np_date(self, rename_libraries):
import perspective

assert perspective.is_libpsp() is False
data = {
"a": np.array(
[date(2020, i, 1) for i in range(1, 13)], dtype="datetime64[D]"
)
}
data = {"a": np.array([date(2020, i, 1) for i in range(1, 13)], dtype="datetime64[D]")}
widget = perspective.PerspectiveWidget(data)
assert hasattr(widget, "table") is False
assert widget._data == {"a": ["2020-{:02d}-01".format(i) for i in range(1, 13)]}
Expand All @@ -163,9 +159,7 @@ def test_widget_client_df_date(self, rename_libraries):
import perspective

assert perspective.is_libpsp() is False
data = pd.DataFrame(
{"a": [date(2020, i, 1) for i in range(1, 13)]}, dtype="datetime64[ns]"
)
data = pd.DataFrame({"a": [date(2020, i, 1) for i in range(1, 13)]}, dtype="datetime64[ns]")
widget = perspective.PerspectiveWidget(data)
assert hasattr(widget, "table") is False
assert widget._data == {
Expand All @@ -177,9 +171,7 @@ def test_widget_client_df_date_object(self, rename_libraries):
import perspective

assert perspective.is_libpsp() is False
data = pd.DataFrame(
{"a": [date(2020, i, 1) for i in range(1, 13)]}, dtype="object"
)
data = pd.DataFrame({"a": [date(2020, i, 1) for i in range(1, 13)]}, dtype="object")
widget = perspective.PerspectiveWidget(data)
assert hasattr(widget, "table") is False
assert widget._data == {
Expand All @@ -194,9 +186,7 @@ def test_widget_client_datetime(self, rename_libraries):
data = {"a": [datetime(2020, i, 1, 12, 30, 45) for i in range(1, 13)]}
widget = perspective.PerspectiveWidget(data)
assert hasattr(widget, "table") is False
assert widget._data == {
"a": ["2020-{:02d}-01 12:30:45".format(i) for i in range(1, 13)]
}
assert widget._data == {"a": ["2020-{:02d}-01 12:30:45".format(i) for i in range(1, 13)]}

def test_widget_client_np_datetime(self, rename_libraries):
import perspective
Expand All @@ -210,24 +200,16 @@ def test_widget_client_np_datetime(self, rename_libraries):
}
widget = perspective.PerspectiveWidget(data)
assert hasattr(widget, "table") is False
assert widget._data == {
"a": ["2020-{:02d}-01 12:30:45".format(i) for i in range(1, 13)]
}
assert widget._data == {"a": ["2020-{:02d}-01 12:30:45".format(i) for i in range(1, 13)]}

def test_widget_client_np_datetime_object(self, rename_libraries):
import perspective

assert perspective.is_libpsp() is False
data = {
"a": np.array(
[datetime(2020, i, 1, 12, 30, 45) for i in range(1, 13)], dtype="object"
)
}
data = {"a": np.array([datetime(2020, i, 1, 12, 30, 45) for i in range(1, 13)], dtype="object")}
widget = perspective.PerspectiveWidget(data)
assert hasattr(widget, "table") is False
assert widget._data == {
"a": ["2020-{:02d}-01 12:30:45".format(i) for i in range(1, 13)]
}
assert widget._data == {"a": ["2020-{:02d}-01 12:30:45".format(i) for i in range(1, 13)]}

def test_widget_client_df_datetime(self, rename_libraries):
import perspective
Expand Down Expand Up @@ -272,9 +254,7 @@ def test_widget_client_np_recarray(self, rename_libraries):
import perspective

assert perspective.is_libpsp() is False
data = np.array([(1, 2), (3, 4)], dtype=[("a", "int64"), ("b", "int64")]).view(
np.recarray
)
data = np.array([(1, 2), (3, 4)], dtype=[("a", "int64"), ("b", "int64")]).view(np.recarray)
widget = perspective.PerspectiveWidget(data)
assert hasattr(widget, "table") is False
assert widget._data == {"a": [1, 3], "b": [2, 4]}
Expand Down Expand Up @@ -355,9 +335,7 @@ def test_widget_client_schema(self, rename_libraries):
import perspective

assert perspective.is_libpsp() is False
widget = perspective.PerspectiveWidget(
{"a": int, "b": float, "c": bool, "d": date, "e": datetime, "f": str}
)
widget = perspective.PerspectiveWidget({"a": int, "b": float, "c": bool, "d": date, "e": datetime, "f": str})
assert hasattr(widget, "table") is False
assert widget._data == {
"a": "integer",
Expand All @@ -375,9 +353,7 @@ def test_widget_client_update(self, rename_libraries):
data = {"a": np.arange(0, 50)}
comparison_data = {"a": [i for i in range(50)]}
widget = perspective.PerspectiveWidget(data)
mocked_post = partial(
mock_post, assert_msg={"cmd": "update", "data": comparison_data}
)
mocked_post = partial(mock_post, assert_msg={"cmd": "update", "data": comparison_data})
widget.post = MethodType(mocked_post, widget)
widget.update(data)
assert hasattr(widget, "table") is False
Expand All @@ -389,9 +365,7 @@ def test_widget_client_replace(self, rename_libraries):
data = {"a": np.arange(0, 50)}
new_data = {"a": [1]}
widget = perspective.PerspectiveWidget(data)
mocked_post = partial(
mock_post, assert_msg={"cmd": "replace", "data": new_data}
)
mocked_post = partial(mock_post, assert_msg={"cmd": "replace", "data": new_data})
widget.post = MethodType(mocked_post, widget)
widget.replace(new_data)
assert widget._data is new_data
Expand Down Expand Up @@ -475,9 +449,7 @@ def test_widget_load_split_by_client(self, rename_libraries):
]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second", "third"])
df_both = pd.DataFrame(
np.random.randn(3, 16), index=["A", "B", "C"], columns=index
)
df_both = pd.DataFrame(np.random.randn(3, 16), index=["A", "B", "C"], columns=index)
widget = perspective.PerspectiveWidget(df_both)
assert hasattr(widget, "table") is False
assert widget.columns == ["value"]
Expand Down
16 changes: 4 additions & 12 deletions python/perspective/perspective/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ def make_arrow(names, data, types=None, legacy=False):

batch = pa.RecordBatch.from_arrays(arrays, names)
table = pa.Table.from_batches([batch])
writer = pa.RecordBatchStreamWriter(
stream, table.schema, use_legacy_format=legacy
)
writer = pa.RecordBatchStreamWriter(stream, table.schema, use_legacy_format=legacy)

writer.write_table(table)
writer.close()
Expand All @@ -96,9 +94,7 @@ def make_arrow_from_pandas(df, schema=None, legacy=False):
stream = pa.BufferOutputStream()
table = pa.Table.from_pandas(df, schema=schema)

writer = pa.RecordBatchStreamWriter(
stream, table.schema, use_legacy_format=legacy
)
writer = pa.RecordBatchStreamWriter(stream, table.schema, use_legacy_format=legacy)

writer.write_table(table)
writer.close()
Expand Down Expand Up @@ -138,9 +134,7 @@ def make_dictionary_arrow(names, data, types=None, legacy=False):

batch = pa.RecordBatch.from_arrays(arrays, names)
table = pa.Table.from_batches([batch])
writer = pa.RecordBatchStreamWriter(
stream, table.schema, use_legacy_format=legacy
)
writer = pa.RecordBatchStreamWriter(stream, table.schema, use_legacy_format=legacy)

writer.write_table(table)
writer.close()
Expand Down Expand Up @@ -220,9 +214,7 @@ def superstore(count=100):
dat["Row ID"] = id
dat["Order ID"] = "{}-{}".format(fake.ein(), fake.zipcode())
dat["Order Date"] = fake.date_this_year()
dat["Ship Date"] = fake.date_between_dates(dat["Order Date"]).strftime(
"%Y-%m-%d"
)
dat["Ship Date"] = fake.date_between_dates(dat["Order Date"]).strftime("%Y-%m-%d")
dat["Order Date"] = dat["Order Date"].strftime("%Y-%m-%d")
dat["Ship Mode"] = choice(["First Class", "Standard Class", "Second Class"])
dat["Ship Mode"] = choice(["First Class", "Standard Class", "Second Class"])
Expand Down
1 change: 0 additions & 1 deletion python/perspective/perspective/tests/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
# ┃ This file is part of the Perspective library, distributed under the terms ┃
# ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

81 changes: 32 additions & 49 deletions python/perspective/perspective/tests/core/test_aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,27 @@
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

from pytest import raises
from perspective import PerspectiveError, PerspectiveViewer,\
PerspectiveWidget, Aggregate, Table
from perspective import PerspectiveError, PerspectiveViewer, PerspectiveWidget, Aggregate, Table


class TestAggregates:

def test_aggregates_widget_load(self):
aggs = {
"a": Aggregate.AVG,
"b": Aggregate.LAST
}
aggs = {"a": Aggregate.AVG, "b": Aggregate.LAST}
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data, aggregates=aggs)
assert widget.aggregates == aggs

def test_aggregates_widget_load_weighted_mean(self):
aggs = {
"a": Aggregate.AVG,
"b": ["weighted mean", "a"]
}
aggs = {"a": Aggregate.AVG, "b": ["weighted mean", "a"]}
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"
}
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"]}
Expand Down Expand Up @@ -92,45 +78,42 @@ def test_aggregates_viewer_set_all(self):
assert viewer.aggregates == {"a": agg.value}

def get_median(self, input_data):
table = Table(data=input_data)
view = table.view(
columns=['Price'],
aggregates={'Price':'median'},
group_by=['Item'])
table = Table(data=input_data)
view = table.view(columns=["Price"], aggregates={"Price": "median"}, group_by=["Item"])

return view.to_json()[0]['Price']
return view.to_json()[0]["Price"]

def test_aggregate_median(self):
numeric_data = [
{'Item':'Book','Price':2.0},
{'Item':'Book','Price':3.0},
{'Item':'Book','Price':5.0},
{'Item':'Book','Price':4.0},
{'Item':'Book','Price':8.0},
{'Item':'Book','Price':9.0},
{'Item':'Book','Price':6.0},
{"Item": "Book", "Price": 2.0},
{"Item": "Book", "Price": 3.0},
{"Item": "Book", "Price": 5.0},
{"Item": "Book", "Price": 4.0},
{"Item": "Book", "Price": 8.0},
{"Item": "Book", "Price": 9.0},
{"Item": "Book", "Price": 6.0},
]

non_numeric_data = [
{'Item':'Book','Price':'2'},
{'Item':'Book','Price':'3'},
{'Item':'Book','Price':'5'},
{'Item':'Book','Price':'4'},
{'Item':'Book','Price':'8'},
{'Item':'Book','Price':'9'},
{'Item':'Book','Price':'6'},
{"Item": "Book", "Price": "2"},
{"Item": "Book", "Price": "3"},
{"Item": "Book", "Price": "5"},
{"Item": "Book", "Price": "4"},
{"Item": "Book", "Price": "8"},
{"Item": "Book", "Price": "9"},
{"Item": "Book", "Price": "6"},
]

# Testing with numeric data
assert self.get_median(numeric_data) == 5.0 #List = [2.0,3.0,5.0,4.0,8.0,9.0,6.0], median = 5.0
assert self.get_median(numeric_data[:2]) == 2.5 #List = [2.0,3.0], median = 2.5
assert self.get_median(numeric_data[5:]) == 7.5 #List = [9.0,6.0], median = 7.5
assert self.get_median(numeric_data[1:]) == 5.5 #List = [3.0,5.0,4.0,8.0,9.0,6.0], median = 5.5
assert self.get_median(numeric_data[::2]) == 5.5 #List = [2.0,5.0,8.0,6.0], median = 5.5
assert self.get_median(numeric_data) == 5.0 # List = [2.0,3.0,5.0,4.0,8.0,9.0,6.0], median = 5.0
assert self.get_median(numeric_data[:2]) == 2.5 # List = [2.0,3.0], median = 2.5
assert self.get_median(numeric_data[5:]) == 7.5 # List = [9.0,6.0], median = 7.5
assert self.get_median(numeric_data[1:]) == 5.5 # List = [3.0,5.0,4.0,8.0,9.0,6.0], median = 5.5
assert self.get_median(numeric_data[::2]) == 5.5 # List = [2.0,5.0,8.0,6.0], median = 5.5

# Testing with non-numeric data
assert self.get_median(non_numeric_data) == '5' #List = ['2','3','5','4','8','9','6'], median = '5'
assert self.get_median(non_numeric_data[:2]) == '3' #List = ['2','3'], median = '5'
assert self.get_median(non_numeric_data[5:]) == '9' #List = ['9','6'], median = '9'
assert self.get_median(non_numeric_data[1:]) == '6' #List = ['3','5','4','8','9','6'], median = '6'
assert self.get_median(non_numeric_data[::2]) == '6' #List = ['2','5','8','6'], median = '6'
assert self.get_median(non_numeric_data) == "5" # List = ['2','3','5','4','8','9','6'], median = '5'
assert self.get_median(non_numeric_data[:2]) == "3" # List = ['2','3'], median = '5'
assert self.get_median(non_numeric_data[5:]) == "9" # List = ['9','6'], median = '9'
assert self.get_median(non_numeric_data[1:]) == "6" # List = ['3','5','4','8','9','6'], median = '6'
assert self.get_median(non_numeric_data[::2]) == "6" # List = ['2','5','8','6'], median = '6'
4 changes: 1 addition & 3 deletions python/perspective/perspective/tests/core/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ def _counter(key, f, *args, **kwargs):
return f(*args, **kwargs)

short_delay_queue_process = partial(_counter, "sync")
long_delay_queue_process = partial(
TestAsync.loop.add_timeout, 1, _counter, "async"
)
long_delay_queue_process = partial(TestAsync.loop.add_timeout, 1, _counter, "async")

tbl = Table({"a": int, "b": float, "c": str})
tbl2 = Table({"a": int, "b": float, "c": str})
Expand Down
15 changes: 7 additions & 8 deletions python/perspective/perspective/tests/core/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@


class TestLayout:

def test_layout_invalid_plugin(self):
with patch('IPython.display.display'):
df = pd.DataFrame([1, 2], columns=['1'])
with patch("IPython.display.display"):
df = pd.DataFrame([1, 2], columns=["1"])
PerspectiveWidget(df, plugin=Plugin.YBAR)
PerspectiveWidget(df, plugin='Y Line')
PerspectiveWidget(df, plugin="Y Line")
try:
PerspectiveWidget(df, plugin='test')
PerspectiveWidget(df, plugin="test")
assert False
except PerspectiveError:
pass
Expand All @@ -35,9 +34,9 @@ def test_layout_invalid_plugin(self):
pass

def test_layout_invalid_columns(self):
with patch('IPython.display.display'):
df = pd.DataFrame([1, 2], columns=['1'])
PerspectiveWidget(df, plugin=Plugin.YBAR, columns=['1'])
with patch("IPython.display.display"):
df = pd.DataFrame([1, 2], columns=["1"])
PerspectiveWidget(df, plugin=Plugin.YBAR, columns=["1"])
try:
PerspectiveWidget(df, plugin=Plugin.YBAR, columns=5)
assert False
Expand Down
6 changes: 2 additions & 4 deletions python/perspective/perspective/tests/core/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

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


class TestPlugin:

def test_plugin_widget_load_grid(self):
data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
widget = PerspectiveWidget(data, plugin=Plugin.GRID)
Expand All @@ -40,7 +38,7 @@ def test_plugin_widget_load_invalid(self):

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

Expand Down
4 changes: 1 addition & 3 deletions python/perspective/perspective/tests/core/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

from pytest import raises
from perspective import PerspectiveError, PerspectiveViewer,\
PerspectiveWidget, Sort
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]])
Expand Down
Loading

0 comments on commit abfaa27

Please sign in to comment.