def test_add_operations_to_the_data(graphic_json_fixture, addendum_dict): graphic_json_fixture = graphic_json_fixture graphic_0_dict = graphic_json_fixture["graphic_0"] graphic_class = PlotlyPlot( graphic_0_dict, addendum_dict["graphic_0"], ) graphic_class.add_operations_to_the_data_from_addendum() operations_list = graphic_class.graphic_dict[DATA_FILTERS] assert len(operations_list) == 3 assert operations_list[0] == { "type": "filter", "column": "penguin_size:sex", "selected": ["MALE"], FILTERED_SELECTOR: False, } assert operations_list[1] == { "type": "filter", "column": "penguin_size:island", "selected": ["Torgersen", "Dream"], FILTERED_SELECTOR: False, } assert operations_list[2] == { "type": "numerical_filter", "column": "penguin_size:culmen_length_mm", "operation": "<=", "value": 4.0, }
def test_get_unique_set_of_columns_needed(): culmen = "penguin_size:culmen_length_mm" flipper = "penguin_size:flipper_length_mm" flipper2 = "penguin_size:flipper_length_mm2" island = "penguin_size:island" sex = "penguin_size:sex" ploty_test = PlotlyPlot() test_cols_list = get_unique_set_of_columns_needed( ploty_test.get_data_columns({ DATA: [ { "x": culmen, "y": flipper }, { "x": culmen, "y": flipper2 }, ] }), { "hover_data": { "column": [sex, culmen] }, "groupby": { "column": [island] }, }, ) assert culmen in test_cols_list assert flipper in test_cols_list assert flipper2 in test_cols_list assert island in test_cols_list assert sex in test_cols_list assert len(test_cols_list) == 5
def test_add_active_selectors_to_selectable_data_list_without_addendum( graphic_json_fixture, ): graphic_0_dict = graphic_json_fixture["graphic_0"] graphic_0_dict[SELECTABLE_DATA_DICT][FILTER][1][DEFAULT_SELECTED] = [ "Dream" ] graphic_class = PlotlyPlot(graphic_0_dict, ) graphic_class.add_active_selectors_to_selectable_data_list() filter_list = graphic_class.graphic_dict[SELECTABLE_DATA_DICT][FILTER] assert len(filter_list[0][ACTIVE_SELECTORS]) == 1 assert SHOW_ALL_ROW in filter_list[0][ACTIVE_SELECTORS] assert len(filter_list[1][ACTIVE_SELECTORS]) == 1 assert "Dream" in filter_list[1][ACTIVE_SELECTORS] numerical_filter_list = graphic_class.graphic_dict[SELECTABLE_DATA_DICT][ NUMERICAL_FILTER] assert numerical_filter_list[0][ACTIVE_SELECTORS][MAX][VALUE] == "" assert numerical_filter_list[0][ACTIVE_SELECTORS][MIN][VALUE] == "" graphic_1_dict = graphic_json_fixture["graphic_1"] graphic_class = PlotlyPlot(graphic_1_dict, ) graphic_class.add_active_selectors_to_selectable_data_list() groupby_list = graphic_class.graphic_dict[SELECTABLE_DATA_DICT][GROUPBY] assert groupby_list[ACTIVE_SELECTORS] == ["penguin_size:sex"]
def test_plotly_draw_scatter(make_data): plot_options = { DATA: [{"type": "scatter", "x": TITLE1, "y": TITLE2, "mode": "markers"}] } ploty_test = PlotlyPlot() graph_json = ploty_test.make_dict_for_html_plot(make_data, plot_options) graph_dict = json.loads(graph_json) assert (graph_dict[DATA][0]["x"] == make_data[TITLE1]).all() assert (graph_dict[DATA][0]["y"] == make_data[TITLE2]).all() assert graph_dict[LAYOUT][PLOT_AXIS.format("x")][TITLE] == TITLE1 assert graph_dict[LAYOUT][PLOT_AXIS.format("y")][TITLE] == TITLE2 # assert graph_dict[LAYOUT][PLOT_AXIS.format("x")][AUTOMARGIN] # assert graph_dict[LAYOUT][PLOT_AXIS.format("y")][AUTOMARGIN] assert len(graph_dict[DATA][0][TRANSFORMS]) == 0
def test_add_operations_to_the_data_from_defaults(graphic_json_fixture): selectable_data_dict = graphic_json_fixture["graphic_0"][ SELECTABLE_DATA_DICT] selectable_data_dict[NUMERICAL_FILTER][0][MIN] = 2 graphic_class = PlotlyPlot(graphic_json_fixture["graphic_0"]) graphic_class.add_operations_to_the_data_from_defaults() operations_list = graphic_class.graphic_dict[DATA_FILTERS] assert len(operations_list) == 1 assert operations_list[0] == { "type": "numerical_filter", "column": "penguin_size:culmen_length_mm", "operation": ">=", "value": 2.0, }
def test_plotly_draw_scatter(make_data): graphic_dict = { PLOT_SPECIFIC_INFO: { DATA: [{"type": "scatter", "x": TITLE1, "y": TITLE2, "mode": "markers"}] } } ploty_test = PlotlyPlot(graphic_dict) ploty_test.data = make_data ploty_test.make_dict_for_html_plot() graph_dict = json.loads(ploty_test.graph_json_str) assert (graph_dict[DATA][0]["x"] == make_data[TITLE1]).all() assert (graph_dict[DATA][0]["y"] == make_data[TITLE2]).all() assert graph_dict[LAYOUT][PLOT_AXIS.format("x")][TITLE] == TITLE1 assert graph_dict[LAYOUT][PLOT_AXIS.format("y")][TITLE] == TITLE2 # assert graph_dict[LAYOUT][PLOT_AXIS.format("x")][AUTOMARGIN] # assert graph_dict[LAYOUT][PLOT_AXIS.format("y")][AUTOMARGIN] assert TRANSFORMS not in graph_dict[DATA][0]
def test_add_active_selectors_to_selectable_data_list_with_SHOW_ALL_ROWS_chosen( graphic_json_fixture, ): addendum_dict = { "filter_0": ["MALE"], "filter_1": [SHOW_ALL_ROW], "numerical_filter_0_max_value": ["4"], "numerical_filter_0_min_value": [""], } graphic_0_dict = graphic_json_fixture["graphic_0"] graphic_class = PlotlyPlot( graphic_0_dict, addendum_dict, ) graphic_class.add_active_selectors_to_selectable_data_list() graphic_dict = graphic_class.graphic_dict assert len( graphic_dict[SELECTABLE_DATA_DICT][FILTER][1][ACTIVE_SELECTORS]) == 1 assert (SHOW_ALL_ROW in graphic_dict[SELECTABLE_DATA_DICT][FILTER][1][ACTIVE_SELECTORS])
def test_add_active_selectors_to_selectable_data_list_with_addendum( graphic_json_fixture, addendum_dict): graphic_0_dict = graphic_json_fixture["graphic_0"] graphic_class = PlotlyPlot( graphic_0_dict, addendum_dict["graphic_0"], ) graphic_class.add_active_selectors_to_selectable_data_list() filter_list = graphic_class.graphic_dict[SELECTABLE_DATA_DICT][FILTER] assert len(filter_list[0][ACTIVE_SELECTORS]) == 1 assert "MALE" in filter_list[0][ACTIVE_SELECTORS] assert len(filter_list[1][ACTIVE_SELECTORS]) == 2 assert "Torgersen" in filter_list[1][ACTIVE_SELECTORS] assert "Dream" in filter_list[1][ACTIVE_SELECTORS] numerical_filter_list = graphic_class.graphic_dict[SELECTABLE_DATA_DICT][ NUMERICAL_FILTER] assert numerical_filter_list[0][ACTIVE_SELECTORS][MAX][VALUE] == "4" assert numerical_filter_list[0][ACTIVE_SELECTORS][MIN][VALUE] == ""
def test_plotly_visualization_options(make_data, test_app_client_sql_backed): plot_options = { DATA: [{"type": "scatter", "x": TITLE1, "y": TITLE2, "mode": "markers"}] } ploty_test = PlotlyPlot() visualization_options = { "hover_data": {OPTION_COL: [TITLE1],}, # need a flask app to run AGGREGATE: {OPTION_COL: [TITLE2], AGGREGATIONS: {"x": "avg", "y": "avg"},}, } graph_json = ploty_test.make_dict_for_html_plot( make_data, plot_options, visualization_options ) graph_dict = json.loads(graph_json) transform_dict = graph_dict[DATA][0][TRANSFORMS] assert len(transform_dict) == 1 assert transform_dict[0][VISUALIZATION_TYPE] == AGGREGATE assert transform_dict[0][AGGREGATIONS][0]["target"] == "x" assert transform_dict[0][AGGREGATIONS][0]["func"] == "avg" assert transform_dict[0][AGGREGATIONS][1]["target"] == "y" assert transform_dict[0][AGGREGATIONS][1]["func"] == "avg"
def test_plotly_visualization_options(make_data, test_app_client_sql_backed): graphic_dict = { PLOT_SPECIFIC_INFO: { DATA: [ { "type": "scatter", "x": TITLE1, "y": TITLE2, "mode": "markers", HOVERTEXT: [TITLE1], TRANSFORMS: { AGGREGATE: [ { GROUPS: [TITLE2], AGGREGATIONS: [ {TARGET: "x", "func": "avg"}, {TARGET: "y", "func": "avg"}, ], } ] }, } ] } } ploty_test = PlotlyPlot(graphic_dict) ploty_test.data = make_data ploty_test.make_dict_for_html_plot() graph_dict = json.loads(ploty_test.graph_json_str) transform_dict = graph_dict[DATA][0][TRANSFORMS] assert len(transform_dict) == 1 assert transform_dict[0][VISUALIZATION_TYPE] == AGGREGATE assert transform_dict[0][AGGREGATIONS][0]["target"] == "x" assert transform_dict[0][AGGREGATIONS][0]["func"] == "avg" assert transform_dict[0][AGGREGATIONS][1]["target"] == "y" assert transform_dict[0][AGGREGATIONS][1]["func"] == "avg"
def test_create_data_subselect_info_for_plot_with_defaults( sql_handler_fixture, graphic_json_fixture): graphic_0_dict = graphic_json_fixture["graphic_0"] # add_active_selectors_to_selectable_data_list adds default SHOW_ALL_ROWS to selectors for selector in graphic_0_dict[SELECTABLE_DATA_DICT][FILTER]: selector[ACTIVE_SELECTORS] = [SHOW_ALL_ROW] numerical_filter_example_dict = {MAX: {VALUE: "3"}, MIN: {VALUE: ""}} graphic_0_dict[SELECTABLE_DATA_DICT][NUMERICAL_FILTER][0][ ACTIVE_SELECTORS] = numerical_filter_example_dict graphic_class = PlotlyPlot(graphic_0_dict, ) data_filters = graphic_class.graphic_dict.get(DATA_FILTERS, []) unique_entry_dict = sql_handler_fixture.get_column_unique_entries( graphic_class.get_columns_that_need_unique_entries(), filters=data_filters) graphic_class.unique_entry_dict = unique_entry_dict graphic_class.create_data_subselect_info_for_plot() select_info = graphic_class.select_info expected_select_info = [ { "select_html_file": "selector.html", "type": "", "name": "filter_0", "active_selector": [SHOW_ALL_ROW], "entries": [SHOW_ALL_ROW, ".", "FEMALE", "MALE"], "multiple": False, TEXT: "Filter by penguin_size:sex", }, { "select_html_file": "selector.html", "type": "", "name": "filter_1", "active_selector": [SHOW_ALL_ROW], "entries": [SHOW_ALL_ROW, "Biscoe", "Dream", "Torgersen"], "multiple": True, TEXT: "Filter by penguin_size:island", }, { "select_html_file": "numerical_filter.html", "type": "number", "name": "numerical_filter_0", "active_selector": numerical_filter_example_dict, "entries": None, "multiple": False, TEXT: "Filter by penguin_size:culmen_length_mm", }, ] assert select_info[0] == expected_select_info[0] assert select_info[1] == expected_select_info[1] assert select_info[2] == expected_select_info[2]
def test_modify_config_based_on_json(graphic_json_fixture): graphic_1_dict = graphic_json_fixture["graphic_1"] addendum_dict = { GROUPBY: ["penguin_size:island"], } assert TRANSFORMS not in graphic_1_dict[PLOT_SPECIFIC_INFO][DATA][0] graphic_class = PlotlyPlot( graphic_1_dict, addendum_dict, ) graphic_class.modify_graphic_dict_based_on_addendum_dict() assert graphic_class.graphic_dict[PLOT_SPECIFIC_INFO][DATA][0][TRANSFORMS][ GROUPBY][GROUPS] == ["penguin_size:island"] # test defualt selected graphic_class = PlotlyPlot(graphic_1_dict, ) graphic_class.modify_graphic_dict_based_on_addendum_dict() assert graphic_class.graphic_dict[PLOT_SPECIFIC_INFO][DATA][0][TRANSFORMS][ GROUPBY][GROUPS] == ["penguin_size:sex"]
def test_create_data_subselect_info(sql_handler_fixture, graphic_json_fixture): select_dict = { FILTER: [ { "column": "penguin_size:sex", "multiple": False, ACTIVE_SELECTORS: ["MALE"], }, { "column": "penguin_size:island", "multiple": True, ACTIVE_SELECTORS: [SHOW_ALL_ROW], }, ] } graphic_0_dict = graphic_json_fixture["graphic_0"] graphic_0_dict[SELECTABLE_DATA_DICT] = select_dict graphic_class = PlotlyPlot(graphic_0_dict, ) data_filters = graphic_class.graphic_dict.get(DATA_FILTERS, []) unique_entry_dict = sql_handler_fixture.get_column_unique_entries( graphic_class.get_columns_that_need_unique_entries(), filters=data_filters) graphic_class.unique_entry_dict = unique_entry_dict graphic_class.create_data_subselect_info_for_plot() select_info = graphic_class.select_info assert select_info[1][JINJA_SELECT_HTML_FILE] == "selector.html" assert "MALE" in select_info[0][ACTIVE_SELECTORS] assert "MALE" in select_info[0][ENTRIES] assert "FEMALE" in select_info[0][ENTRIES] assert "." in select_info[0][ ENTRIES] # yes this is a unique entry in the data set assert SHOW_ALL_ROW in select_info[1][ACTIVE_SELECTORS] assert "Biscoe" in select_info[1][ENTRIES] assert select_info[1][MULTIPLE] assert not select_info[0][MULTIPLE]