Skip to content

Commit

Permalink
Global shared variable in test (#144) (#149)
Browse files Browse the repository at this point in the history
* using global shared variable in test (#144)

* modified fixture scope as session, resolved dependency test cases

* run black

Co-authored-by: Doris Lee <dorisjunglinlee@gmail.com>
  • Loading branch information
piyushg9794 and dorisjlee committed Nov 19, 2020
1 parent da79415 commit 21e20cf
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 275 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ static/
*.egg-info*
build/
.DS_Store
.idea/
tests/.coverage
tests/coverage.xml
1 change: 1 addition & 0 deletions lux/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def intent(self, intent_input: Union[List[Union[str, Clause]], Vis]):

def clear_intent(self):
self.intent = []
self.expire_recs()

def set_intent(self, intent: List[Union[str, Clause]]):
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest
import pandas as pd


@pytest.fixture(scope="session")
def global_var():
url = "https://github.com/lux-org/lux-datasets/blob/master/data/olympic.csv?raw=true"
pytest.olympic = pd.read_csv(url)
pytest.car_df = pd.read_csv("lux/data/car.csv")
pytest.college_df = pd.read_csv("lux/data/college.csv")
32 changes: 17 additions & 15 deletions tests/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
from lux.vis.Vis import Vis


def test_vary_filter_val():
df = pd.read_csv("https://github.com/lux-org/lux-datasets/blob/master/data/olympic.csv?raw=true")
def test_vary_filter_val(global_var):
df = pytest.olympic
vis = Vis(["Height", "SportType=Ball"], df)
df.set_intent_as_vis(vis)
df._repr_html_()
assert len(df.recommendation["Filter"]) == len(df["SportType"].unique()) - 1


def test_filter_inequality():
df = pd.read_csv("lux/data/car.csv")
def test_filter_inequality(global_var):
df = pytest.car_df
df["Year"] = pd.to_datetime(df["Year"], format="%Y")

df.set_intent(
Expand All @@ -48,9 +48,9 @@ def test_filter_inequality():
assert fltr_clause.value == 10


def test_generalize_action():
def test_generalize_action(global_var):
# test that generalize action creates all unique visualizations
df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
df["Year"] = pd.to_datetime(
df["Year"], format="%Y"
) # change pandas dtype for the column "Year" to datetype
Expand All @@ -72,9 +72,10 @@ def test_generalize_action():
assert check1 and check2 and check3


def test_row_column_group():
url = "https://github.com/lux-org/lux-datasets/blob/master/data/state_timeseries.csv?raw=true"
df = pd.read_csv(url)
def test_row_column_group(global_var):
df = pd.read_csv(
"https://github.com/lux-org/lux-datasets/blob/master/data/state_timeseries.csv?raw=true"
)
df["Date"] = pd.to_datetime(df["Date"])
tseries = df.pivot(index="State", columns="Date", values="Value")
# Interpolating missing values
Expand All @@ -85,8 +86,8 @@ def test_row_column_group():
assert list(tseries.recommendation.keys()) == ["Row Groups", "Column Groups"]


def test_groupby():
df = pd.read_csv("lux/data/college.csv")
def test_groupby(global_var):
df = pytest.college_df
groupbyResult = df.groupby("Region").sum()
groupbyResult._repr_html_()
assert list(groupbyResult.recommendation.keys()) == ["Column Groups"]
Expand Down Expand Up @@ -159,17 +160,17 @@ def test_crosstab():
assert list(result.recommendation.keys()) == ["Row Groups", "Column Groups"]


def test_custom_aggregation():
def test_custom_aggregation(global_var):
import numpy as np

df = pd.read_csv("lux/data/college.csv")
df = pytest.college_df
df.set_intent(["HighestDegree", lux.Clause("AverageCost", aggregation=np.ptp)])
df._repr_html_()
assert list(df.recommendation.keys()) == ["Enhance", "Filter", "Generalize"]


def test_year_filter_value():
df = pd.read_csv("lux/data/car.csv")
def test_year_filter_value(global_var):
df = pytest.car_df
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
df.set_intent(["Acceleration", "Horsepower"])
df._repr_html_()
Expand All @@ -191,3 +192,4 @@ def test_year_filter_value():
assert (
"T00:00:00.000000000" not in vis.to_Altair()
), "Year filter title contains extraneous string, not displayed as summarized string"
df.clear_intent()
67 changes: 34 additions & 33 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
from lux.vis.VisList import VisList


def test_underspecified_no_vis(test_recs):
def test_underspecified_no_vis(global_var, test_recs):
no_vis_actions = ["Correlation", "Distribution", "Occurrence", "Temporal"]
df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
test_recs(df, no_vis_actions)
assert len(df.current_vis) == 0
assert df.current_vis is None or len(df.current_vis) == 0

# test only one filter context case.
df.set_intent([lux.Clause(attribute="Origin", filter_op="=", value="USA")])
test_recs(df, no_vis_actions)
assert len(df.current_vis) == 0


def test_underspecified_single_vis(test_recs):
def test_underspecified_single_vis(global_var, test_recs):
one_vis_actions = ["Enhance", "Filter", "Generalize"]
df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
df.set_intent([lux.Clause(attribute="MilesPerGal"), lux.Clause(attribute="Weight")])
test_recs(df, one_vis_actions)
assert len(df.current_vis) == 1
Expand All @@ -42,6 +42,7 @@ def test_underspecified_single_vis(test_recs):
assert attr.data_model == "measure"
for attr in df.current_vis[0]._inferred_intent:
assert attr.data_type == "quantitative"
df.clear_intent()


# def test_underspecified_vis_collection(test_recs):
Expand Down Expand Up @@ -75,8 +76,8 @@ def test_underspecified_single_vis(test_recs):
# df.set_intent([lux.Clause(attribute ="?", data_model="measure"), lux.Clause(attribute ="?", data_model="measure")])
# assert len(df.current_vis) == len([vis.get_attr_by_data_model("measure") for vis in df.current_vis]) #should be 25
# test_recs(df, multiple_vis_actions)
def test_set_intent_as_vis(test_recs):
df = pd.read_csv("lux/data/car.csv")
def test_set_intent_as_vis(global_var, test_recs):
df = pytest.car_df
df._repr_html_()
vis = df.recommendation["Correlation"][0]
df.intent = vis
Expand All @@ -95,19 +96,19 @@ def test_recs_function(df, actions):
return test_recs_function


def test_parse():
df = pd.read_csv("lux/data/car.csv")
def test_parse(global_var):
df = pytest.car_df
vlst = VisList([lux.Clause("Origin=?"), lux.Clause(attribute="MilesPerGal")], df)
assert len(vlst) == 3

df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
vlst = VisList([lux.Clause("Origin=?"), lux.Clause("MilesPerGal")], df)
assert len(vlst) == 3


def test_underspecified_vis_collection_zval():
def test_underspecified_vis_collection_zval(global_var):
# check if the number of charts is correct
df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
vlst = VisList(
[
lux.Clause(attribute="Origin", filter_op="=", value="?"),
Expand All @@ -123,11 +124,11 @@ def test_underspecified_vis_collection_zval():
# assert len(vlst) == 8


def test_sort_bar():
def test_sort_bar(global_var):
from lux.processor.Compiler import Compiler
from lux.vis.Vis import Vis

df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
vis = Vis(
[
lux.Clause(attribute="Acceleration", data_model="measure", data_type="quantitative"),
Expand All @@ -138,7 +139,7 @@ def test_sort_bar():
assert vis.mark == "bar"
assert vis._inferred_intent[1].sort == ""

df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
vis = Vis(
[
lux.Clause(attribute="Acceleration", data_model="measure", data_type="quantitative"),
Expand All @@ -150,8 +151,8 @@ def test_sort_bar():
assert vis._inferred_intent[1].sort == "ascending"


def test_specified_vis_collection():
df = pd.read_csv("lux/data/car.csv")
def test_specified_vis_collection(global_var):
df = pytest.car_df
# change pandas dtype for the column "Year" to datetype
df["Year"] = pd.to_datetime(df["Year"], format="%Y")

Expand Down Expand Up @@ -181,8 +182,8 @@ def test_specified_vis_collection():
assert "Origin = Europe" not in chart_titles


def test_specified_channel_enforced_vis_collection():
df = pd.read_csv("lux/data/car.csv")
def test_specified_channel_enforced_vis_collection(global_var):
df = pytest.car_df
# change pandas dtype for the column "Year" to datetype
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
visList = VisList(
Expand All @@ -193,9 +194,9 @@ def test_specified_channel_enforced_vis_collection():
check_attribute_on_channel(vis, "MilesPerGal", "x")


def test_autoencoding_scatter():
def test_autoencoding_scatter(global_var):
# No channel specified
df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
# change pandas dtype for the column "Year" to datetype
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
vis = Vis([lux.Clause(attribute="MilesPerGal"), lux.Clause(attribute="Weight")], df)
Expand Down Expand Up @@ -234,9 +235,9 @@ def test_autoencoding_scatter():
)


def test_autoencoding_histogram():
def test_autoencoding_histogram(global_var):
# No channel specified
df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
# change pandas dtype for the column "Year" to datetype
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
vis = Vis([lux.Clause(attribute="MilesPerGal", channel="y")], df)
Expand All @@ -247,8 +248,8 @@ def test_autoencoding_histogram():
assert vis.get_attr_by_channel("y")[0].attribute == "Record"


def test_autoencoding_line_chart():
df = pd.read_csv("lux/data/car.csv")
def test_autoencoding_line_chart(global_var):
df = pytest.car_df
# change pandas dtype for the column "Year" to datetype
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
vis = Vis([lux.Clause(attribute="Year"), lux.Clause(attribute="Acceleration")], df)
Expand Down Expand Up @@ -287,8 +288,8 @@ def test_autoencoding_line_chart():
)


def test_autoencoding_color_line_chart():
df = pd.read_csv("lux/data/car.csv")
def test_autoencoding_color_line_chart(global_var):
df = pytest.car_df
# change pandas dtype for the column "Year" to datetype
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
intent = [
Expand All @@ -302,8 +303,8 @@ def test_autoencoding_color_line_chart():
check_attribute_on_channel(vis, "Origin", "color")


def test_autoencoding_color_scatter_chart():
df = pd.read_csv("lux/data/car.csv")
def test_autoencoding_color_scatter_chart(global_var):
df = pytest.car_df
# change pandas dtype for the column "Year" to datetype
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
vis = Vis(
Expand All @@ -327,10 +328,10 @@ def test_autoencoding_color_scatter_chart():
check_attribute_on_channel(vis, "Acceleration", "color")


def test_populate_options():
def test_populate_options(global_var):
from lux.processor.Compiler import Compiler

df = pd.read_csv("lux/data/car.csv")
df = pytest.car_df
df.set_intent([lux.Clause(attribute="?"), lux.Clause(attribute="MilesPerGal")])
col_set = set()
for specOptions in Compiler.populate_wildcard_options(df._intent, df)["attributes"]:
Expand All @@ -355,8 +356,8 @@ def test_populate_options():
)


def test_remove_all_invalid():
df = pd.read_csv("lux/data/car.csv")
def test_remove_all_invalid(global_var):
df = pytest.car_df
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
# with pytest.warns(UserWarning,match="duplicate attribute specified in the intent"):
df.set_intent(
Expand Down
20 changes: 10 additions & 10 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def contain_horsepower(df):
return df


def test_default_actions_registered():
df = pd.read_csv("lux/data/car.csv")
def test_default_actions_registered(global_var):
df = pytest.car_df
df._repr_html_()
assert "Distribution" in df.recommendation
assert len(df.recommendation["Distribution"]) > 0
Expand Down Expand Up @@ -91,13 +91,13 @@ def test_no_validator():
assert "bars" in df.recommendation


def test_invalid_function():
def test_invalid_function(global_var):
df = pd.read_csv("lux/data/car.csv")
with pytest.raises(ValueError, match="Value must be a callable"):
lux.register_action("bars", "not a Callable")


def test_invalid_validator():
def test_invalid_validator(global_var):
df = pd.read_csv("lux/data/car.csv")

def random_categorical(ldf):
Expand Down Expand Up @@ -136,14 +136,14 @@ def test_remove_action():
)


def test_remove_invalid_action():
df = pd.read_csv("lux/data/car.csv")
def test_remove_invalid_action(global_var):
df = pytest.car_df
with pytest.raises(ValueError, match="Option 'bars' has not been registered"):
lux.remove_action("bars")


def test_remove_default_actions():
df = pd.read_csv("lux/data/car.csv")
def test_remove_default_actions(global_var):
df = pytest.car_df
df._repr_html_()

lux.remove_action("Distribution")
Expand Down Expand Up @@ -178,8 +178,8 @@ def test_remove_default_actions():


# TODO: This test does not pass in pytest but is working in Jupyter notebook.
# def test_plot_setting():
# df = pd.read_csv("lux/data/car.csv")
# def test_plot_setting(global_var):
# df = pytest.car_df
# df["Year"] = pd.to_datetime(df["Year"], format='%Y')
# def change_color_add_title(chart):
# chart = chart.configure_mark(color="green") # change mark color to green
Expand Down
Loading

0 comments on commit 21e20cf

Please sign in to comment.