Skip to content

Commit

Permalink
using global shared variable in test (lux-org#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushg9794 committed Nov 19, 2020
1 parent da79415 commit cd44600
Show file tree
Hide file tree
Showing 18 changed files with 305 additions and 290 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
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
import pandas as pd


@pytest.fixture
def global_var():
pytest.olympics_df = pd.read_csv("https://github.com/lux-org/lux-datasets/blob/master/data/olympic.csv?raw=true")
pytest.car_df = pd.read_csv("lux/data/car.csv")
url = "https://github.com/lux-org/lux-datasets/blob/master/data/state_timeseries.csv?raw=true"
pytest.state_timeseries_df = pd.read_csv(url)
pytest.college_df = pd.read_csv("lux/data/college.csv")
pytest.census_df = pd.read_csv("https://github.com/lux-org/lux-datasets/blob/master/data/census.csv?raw=true")
29 changes: 14 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.olympics_df
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,8 @@ 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 = pytest.state_timeseries_df
df["Date"] = pd.to_datetime(df["Date"])
tseries = df.pivot(index="State", columns="Date", values="Value")
# Interpolating missing values
Expand All @@ -85,8 +84,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 +158,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 Down
64 changes: 32 additions & 32 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
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

Expand All @@ -31,9 +31,9 @@ def test_underspecified_no_vis(test_recs):
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 Down Expand Up @@ -75,8 +75,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 +95,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 +123,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 +138,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 +150,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 +181,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 +193,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 +234,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 +247,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 +287,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 +302,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 +327,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 +355,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
16 changes: 8 additions & 8 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from lux.executor.PandasExecutor import PandasExecutor


def test_dateformatter():
ldf = pd.read_csv("lux/data/car.csv")
def test_dateformatter(global_var):
ldf = pytest.car_df
# change pandas dtype for the column "Year" to datetype
ldf["Year"] = pd.to_datetime(ldf["Year"], format="%Y")
timestamp = np.datetime64("2019-08-26")
Expand All @@ -37,8 +37,8 @@ def test_dateformatter():
assert date_utils.date_formatter(timestamp, ldf) == "2019-8-26"


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

ldf["Year"] = pd.DatetimeIndex(ldf["Year"]).to_period(freq="A")
Expand All @@ -56,8 +56,8 @@ def test_period_selection():
assert all(ldf.current_vis[2].data.columns == ["Year", "Acceleration"])


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

ldf["Year"] = pd.DatetimeIndex(ldf["Year"]).to_period(freq="A")
Expand All @@ -70,9 +70,9 @@ def test_period_filter():
assert isinstance(ldf.recommendation["Filter"][2]._inferred_intent[2].value, pd.Period)


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

df["Year"] = pd.DatetimeIndex(df["Year"]).to_period(freq="A")
Expand Down
Loading

0 comments on commit cd44600

Please sign in to comment.