def test_table_object_data_builder_does_return_object(
        stub_simple_table_object):
    builder = TableObjectDataBuilder()

    table = builder.get_data_table(stub_simple_table_object)

    assert table is not None
def test_table_object_data_builder_does_build_data_from_simple_table(
        stub_simple_table_object):
    # given - a table without a category_caption value
    builder = TableObjectDataBuilder()
    table_object = stub_simple_table_object

    # when we process the object
    data = builder.get_data_table(table_object)
    data.pop(0)

    # then the header for the returned table should match the ones from the simple table
    expected_data = [["White", "25.6", "0.256"], ["Other", "16.6", "0.166"]]
    assert data == expected_data
def test_table_object_data_builder_does_build_headers_from_legacy_simple_table(
        stub_simple_table_object):
    # given - a table without a category_caption value
    builder = TableObjectDataBuilder()
    table_object = stub_simple_table_object
    table_object.pop("category_caption", None)

    # when we process the object
    table = builder.get_data_table(table_object)

    # then the header for the returned table should match the ones from the simple table
    headers = table.pop(0)
    expected_headers = ["Ethnicity"] + table_object["columns"]
    assert headers == expected_headers
def test_table_object_data_builder_does_build_headers_from_simple_table(
        stub_simple_table_object):
    # given - a
    builder = TableObjectDataBuilder()
    table_object = stub_simple_table_object

    # when we process the object
    table = builder.get_data_table(table_object)

    # then the header for the returned table should match the ones from the simple table
    headers = table.pop(0)
    expected_headers = [table_object["category_caption"]
                        ] + table_object["columns"]
    assert headers == expected_headers
def test_table_object_data_builder_does_build_headers_from_grouped_table(
        stub_grouped_table_object):
    # given - a grouped table object
    builder = TableObjectDataBuilder()
    table_object = stub_grouped_table_object

    # when we process the object
    table = builder.get_data_table(table_object)

    # then the header for the returned table should match the ones from the simple table
    headers = table.pop(0)
    # ['Sex', 'Custom category caption', 'Value', 'Rate']
    expected_headers = [
        table_object["group_column"], table_object["category_caption"]
    ] + table_object["columns"]
    assert headers == expected_headers
Beispiel #6
0
    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
Beispiel #7
0
    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
Beispiel #8
0
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()