def test_upgrade_does_convert_bar_chart_with_second_dimension(): # GIVEN from tests.test_data.chart_convert import bar, bar_source, bar_grouped, bar_grouped_source # WHEN upgraded_simple = ChartObjectDataBuilder.upgrade_v1_to_v2(bar, bar_source) upgraded_grouped = ChartObjectDataBuilder.upgrade_v1_to_v2( bar_grouped, bar_grouped_source) # THEN assert upgraded_simple["type"] == "bar_chart" assert upgraded_grouped["type"] == "grouped_bar_chart"
def test_upgrade_does_copy_chart_type(): # GIVEN value = "blah" # WHEN upgraded = ChartObjectDataBuilder.get_v2_chart_type({"type": value}) # THEN assert upgraded["type"] == value
def test_bar_chart_takes_data_points_from_chart_object(): # GIVEN # from tests.test_data.chart_convert import bar, bar_source # WHEN # upgraded_bar = ChartObjectDataBuilder.upgrade_v1_to_v2(bar, bar_source) # THEN # assert upgraded_bar["data"][1] == ["White", 71.12375533]
def test_line_graph_takes_data_points_from_chart(): # GIVEN # from tests.test_data.chart_convert import line, line_source # WHEN # upgraded_line = ChartObjectDataBuilder.upgrade_v1_to_v2(line, line_source) # THEN # assert upgraded_line["data"][1] == ["All", "2005/06", 70]
def test_panel_line_graph_takes_data_points_from_chart(): # GIVEN # from tests.test_data.chart_convert import panel_line_chart, panel_line_chart_source # WHEN # upgraded_line = ChartObjectDataBuilder.upgrade_v1_to_v2( panel_line_chart, panel_line_chart_source) # THEN # assert upgraded_line["data"][1] == ["Asian", "2006/07", 6]
def test_bar_chart_has_data_points_ethnicity_value(): # GIVEN # from tests.test_data.chart_convert import bar, bar_source # WHEN # upgraded_bar = ChartObjectDataBuilder.upgrade_v1_to_v2(bar, bar_source) # THEN # assert "data" in upgraded_bar assert upgraded_bar["data"][0] == ["Ethnicity", "Value"]
def test_grouped_bar_chart_has_ethnicity_as_bars_if_series_are_ethnicity(): # GIVEN # from tests.test_data.chart_convert import bar_grouped, bar_grouped_source # WHEN # upgraded_bar = ChartObjectDataBuilder.upgrade_v1_to_v2( bar_grouped, bar_grouped_source) # THEN # assert upgraded_bar["chartOptions"]["data_style"] == "ethnicity_as_bar"
def test_line_graph_has_data_points_ethnicity_x_axis_value(): # GIVEN # from tests.test_data.chart_convert import line, line_source # WHEN # upgraded_line = ChartObjectDataBuilder.upgrade_v1_to_v2(line, line_source) # THEN # assert "data" in upgraded_line assert upgraded_line["data"][0] == ["Ethnicity", "Time", "Value"]
def test_grouped_bar_chart_takes_data_points_from_chart_if_categories_are_ethnicity( ): # GIVEN # from tests.test_data.chart_convert import bar_grouped_2, bar_grouped_source_2 # WHEN # upgraded_bar = ChartObjectDataBuilder.upgrade_v1_to_v2( bar_grouped_2, bar_grouped_source_2) # THEN # assert upgraded_bar["data"][1] == ["Asian", "Boys", 73]
def test_grouped_bar_chart_has_data_type_set(): # GIVEN # from tests.test_data.chart_convert import bar_grouped, bar_grouped_source # WHEN # upgraded_bar = ChartObjectDataBuilder.upgrade_v1_to_v2( bar_grouped, bar_grouped_source) # THEN # assert "chartOptions" in upgraded_bar assert "data_style" in upgraded_bar["chartOptions"]
def test_component_bar_chart_has_data_type_set(): # GIVEN # from tests.test_data.chart_convert import component_ethnicity_bars, component_ethnicity_bars_source # WHEN # upgraded_component = ChartObjectDataBuilder.upgrade_v1_to_v2( component_ethnicity_bars, component_ethnicity_bars_source) # THEN # assert "chartOptions" in upgraded_component assert "data_style" in upgraded_component["chartOptions"]
def test_panel_line_graph_has_data_points_with_header_line(): # GIVEN # from tests.test_data.chart_convert import panel_line_chart, panel_line_chart_source # WHEN # upgraded_line = ChartObjectDataBuilder.upgrade_v1_to_v2( panel_line_chart, panel_line_chart_source) # THEN # assert "data" in upgraded_line assert upgraded_line["data"][0] == ["Ethnicity", "Time", "Value"]
def test_component_chart_has_ethnicity_as_bars_if_series_are_ethnicity(): # GIVEN # from tests.test_data.chart_convert import component_ethnicity_components, component_ethnicity_components_source # WHEN # upgraded_component = ChartObjectDataBuilder.upgrade_v1_to_v2( component_ethnicity_components, component_ethnicity_components_source) # THEN # assert upgraded_component["chartOptions"][ "data_style"] == "ethnicity_as_sections"
def test_line_graph_has_x_axis_set(): # GIVEN # from tests.test_data.chart_convert import line, line_source # WHEN # upgraded_line = ChartObjectDataBuilder.upgrade_v1_to_v2(line, line_source) # THEN # assert "chartOptions" in upgraded_line assert "x_axis_column" in upgraded_line["chartOptions"] assert upgraded_line["chartOptions"]["x_axis_column"] == "Time"
def test_grouped_bar_chart_takes_data_points_from_chart_if_series_are_ethnicity( ): # GIVEN # from tests.test_data.chart_convert import bar_grouped, bar_grouped_source # WHEN # upgraded_bar = ChartObjectDataBuilder.upgrade_v1_to_v2( bar_grouped, bar_grouped_source) # THEN # assert upgraded_bar["data"][1] == [ "White British", "Higher managerial", 82 ]
def test_component_bar_chart_takes_data_points_from_chart_if_bars_are_ethnicity( ): # GIVEN # from tests.test_data.chart_convert import component_ethnicity_bars, component_ethnicity_bars_source # WHEN # upgraded_component = ChartObjectDataBuilder.upgrade_v1_to_v2( component_ethnicity_bars, component_ethnicity_bars_source) # THEN # data values for the first point are correct (excludes order) first_data_point = upgraded_component["data"][1] assert "All" == first_data_point[0] assert "1,000 or more" == first_data_point[1] assert 24 == first_data_point[3]
def test_panel_bar_chart_has_ethnicity_as_panels_if_panels_are_ethnicity(): # GIVEN # from tests.test_data.chart_convert import ( panel_bar_chart_ethnicity_for_panels, panel_bar_chart_ethnicity_for_panels_source, ) # WHEN # upgraded_panel_bar = ChartObjectDataBuilder.upgrade_v1_to_v2( panel_bar_chart_ethnicity_for_panels, panel_bar_chart_ethnicity_for_panels_source) # THEN # assert upgraded_panel_bar["chartOptions"][ "data_style"] == "ethnicity_as_panels"
def test_panel_bar_bar_chart_has_data_type_set(): # GIVEN # from tests.test_data.chart_convert import ( panel_bar_chart_ethnicity_for_bars, panel_bar_chart_ethnicity_for_bars_source, ) # WHEN # upgraded_panel_bar = ChartObjectDataBuilder.upgrade_v1_to_v2( panel_bar_chart_ethnicity_for_bars, panel_bar_chart_ethnicity_for_bars_source) # THEN # assert "chartOptions" in upgraded_panel_bar assert "data_style" in upgraded_panel_bar["chartOptions"]
def build(dimension): dimension_object = { "context": DimensionObjectBuilder.get_context(dimension) } if dimension.table: dimension_object["table"] = TableObjectDataBuilder.build( dimension.table) if dimension.chart: dimension_object["chart"] = ChartObjectDataBuilder.build( dimension.chart) if dimension.table: dimension_object["tabular"] = TableObjectTableBuilder.build( dimension.table) return dimension_object
def build(dimension): dimension_object = { "context": DimensionObjectBuilder.get_context(dimension) } if dimension.dimension_table and dimension.dimension_table.table_object: dimension_object["table"] = TableObjectDataBuilder.build( dimension.dimension_table.table_object) if dimension.dimension_chart and dimension.dimension_chart.chart_object: dimension_object["chart"] = ChartObjectDataBuilder.build( dimension.dimension_chart.chart_object) if dimension.dimension_table and dimension.dimension_table.table_object: dimension_object["tabular"] = TableObjectTableBuilder.build( dimension.dimension_table.table_object) return dimension_object
def test_panel_bar_bar_chart_takes_data_points_from_chart_if_bars_are_ethnicity( ): # GIVEN # from tests.test_data.chart_convert import ( panel_bar_chart_ethnicity_for_bars, panel_bar_chart_ethnicity_for_bars_source, ) # WHEN # upgraded_panel_bar = ChartObjectDataBuilder.upgrade_v1_to_v2( panel_bar_chart_ethnicity_for_bars, panel_bar_chart_ethnicity_for_bars_source) # THEN # assert "All" == upgraded_panel_bar["data"][1][0] assert "Any type of state support" == upgraded_panel_bar["data"][1][1] assert 57 == upgraded_panel_bar["data"][1][3]
def test_panel_bar_chart_takes_data_points_from_chart_if_panels_are_ethnicity( ): # GIVEN # from tests.test_data.chart_convert import ( panel_bar_chart_ethnicity_for_panels, panel_bar_chart_ethnicity_for_panels_source, ) # WHEN # upgraded_panel_bar = ChartObjectDataBuilder.upgrade_v1_to_v2( panel_bar_chart_ethnicity_for_panels, panel_bar_chart_ethnicity_for_panels_source) # THEN # first_data_point = upgraded_panel_bar["data"][1] assert "Asian" == first_data_point[0] assert "Business" == first_data_point[1] assert 2 == first_data_point[3]
def copy_chart_and_table_data(app): # noqa: C901 with app.app_context(): try: count = 0 chart_failures = 0 table_failures = 0 for dimension in Dimension.query.all(): # If there is a chart object, copy it to the chart table if dimension.chart: # Create a Chart() if necessary if dimension.dimension_chart is None: dimension.dimension_chart = Chart() # Don't overwrite if it already exists if dimension.dimension_chart.chart_object is None: dimension.dimension_chart.chart_object = dimension.chart if dimension.chart_2_source_data is not None: # If there is already a version 2 chart settings, copy settings straight across # But don't overwrite if it already exists if dimension.dimension_chart.settings_and_source_data is None: dimension.dimension_chart.settings_and_source_data = dimension.chart_2_source_data else: # Assume there are version 1 settings if no version 2; convert it and copy it across try: version_2_settings = ChartObjectDataBuilder.upgrade_v1_to_v2( dimension.chart, dimension.chart_source_data ) # Don't overwrite if it already exists if dimension.dimension_chart.settings_and_source_data is None: dimension.dimension_chart.settings_and_source_data = version_2_settings except Exception as e: chart_failures += 1 print(f"Error copying chart for {dimension.title} ({dimension.guid})") print(f" CHART EXCEPTION: {type(e)}: {e}") # If there is a table object, copy it to the table table if dimension.table: # Create a Table() if necessary if dimension.dimension_table is None: dimension.dimension_table = Table() # Don't overwrite if it already exists if dimension.dimension_table.table_object is None: dimension.dimension_table.table_object = dimension.table if dimension.table_2_source_data is not None: # If there is already a version 2 table settings, copy settings straight across # But don't overwrite if it already exists if dimension.dimension_table.settings_and_source_data is None: dimension.dimension_table.settings_and_source_data = dimension.table_2_source_data else: # Assume there are version 1 settings if no version 2; convert it and copy it across try: version_2_settings = TableObjectDataBuilder.upgrade_v1_to_v2( dimension.table, dimension.table_source_data, current_app.dictionary_lookup ) # Don't overwrite if it already exists if dimension.dimension_table.settings_and_source_data is None: dimension.dimension_table.settings_and_source_data = version_2_settings except Exception as e: table_failures += 1 print(f"Error copying table for {dimension.title} ({dimension.guid})") print(f" TABLE EXCEPTION: {type(e)}: {e}") count += 1 db.session.commit() print(f"Total: {count}, Chart failures: {chart_failures}, Table failures: {table_failures}") except Exception as e: print(e) db.session.rollback() raise e finally: db.session.close()