def test_measure_page_download_table_tabular_data_link_correct(
            self, test_app_client, logged_in_rdu_user, static_mode,
            expected_url):
        from tests.test_data.chart_and_table import chart, simple_table

        MeasureVersionWithDimensionFactory(
            status="DRAFT",
            version="1.0",
            measure__subtopics__topic__slug="topic",
            measure__subtopics__slug="subtopic",
            measure__slug="measure",
            dimensions__guid="dimension-guid",
            dimensions__title="dimension-title",
            dimensions__dimension_chart__chart_object=chart,
            dimensions__dimension_table__table_object=simple_table(),
            uploads__guid="test-download",
        )

        resp = test_app_client.get(
            f"/topic/subtopic/measure/latest?static_mode={static_mode}",
            follow_redirects=False)
        page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")

        data_links = page.findAll("a", href=expected_url)
        assert len(data_links) == 1
    def test_measure_page_download_measure_source_data_link_correct(
            self, test_app_client, logged_in_rdu_user, static_mode,
            expected_url):
        from tests.test_data.chart_and_table import chart, simple_table

        MeasureVersionWithDimensionFactory(
            status="DRAFT",
            version="1.0",
            measure__subtopics__topic__slug="topic",
            measure__subtopics__slug="subtopic",
            measure__slug="measure",
            dimensions__guid="dimension-guid",
            dimensions__dimension_chart__chart_object=chart,
            dimensions__dimension_table__table_object=simple_table(),
            uploads__guid="test-download",
            uploads__title="Test measure page data",
            uploads__file_name="test-measure-page-data.csv",
        )
        resp = test_app_client.get("/topic/subtopic/measure/latest",
                                   follow_redirects=False)
        page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")
        data_links = page.findAll(
            "a",
            href=True,
            text=re.compile(r"Test measure page data\s+-\s+Spreadsheet"))
        assert len(data_links) == 1
        assert data_links[0].attrs["href"] == expected_url
Beispiel #3
0
def test_table_object_builder_does_build_with_page_level_data_from_simple_table():
    data_source = DataSourceFactory.build(
        title="DWP Stats", source_url="http://dwp.gov.uk", publisher__name="Department for Work and Pensions"
    )
    measure_version = MeasureVersionWithDimensionFactory(
        title="Test Measure Page",
        area_covered=[UKCountry.ENGLAND],
        data_sources=[data_source],
        dimensions__dimension_table__table_object=simple_table(),
        measure__slug="test-measure-page-slug",
    )
    # given - a table without a category_caption value
    builder = DimensionObjectBuilder()
    dimension = measure_version.dimensions[0]

    # when we process the object
    dimension_object = builder.build(dimension)

    # then the measure level info should be brought through
    assert dimension_object["context"]["measure"] == "Test Measure Page"
    assert dimension_object["context"]["measure_slug"] == "test-measure-page-slug"
    assert dimension_object["context"]["location"] == "England"
    assert dimension_object["context"]["title"] == "DWP Stats"
    assert dimension_object["context"]["source_url"] == "http://dwp.gov.uk"
    assert dimension_object["context"]["publisher"] == "Department for Work and Pensions"
Beispiel #4
0
def test_table_object_builder_does_build_with_dimension_level_data_from_simple_table():
    measure_version = MeasureVersionWithDimensionFactory(
        title="Test Measure Page",
        area_covered=[UKCountry.ENGLAND],
        dimensions__title="Dimension title",
        dimensions__guid="dimension-guid",
        dimensions__time_period="dimension-time-period",
        dimensions__dimension_table__table_object=simple_table(),
        measure__slug="test-measure-page-slug",
    )
    # given - a table without a category_caption value
    builder = DimensionObjectBuilder()
    dimension = measure_version.dimensions[0]

    # when we process the object
    dimension_object = builder.build(dimension)

    # then the dimension level info should be brought through
    assert dimension_object["context"]["dimension"] == "Dimension title"
    assert dimension_object["context"]["guid"] == "dimension-guid"
    assert dimension_object["context"]["time_period"] == "dimension-time-period"
def test_static_site_build(db_session, single_use_app):
    """
    A basic test for the core flow of the static site builder. This patches/mocks a few of the key integrations to
    help prevent calling out to external services accidentally, and where possible, includes two levels of failsafes.
    1) We change the application config to not push/deploy the site
    2) We mock out the push_site, so that even if the config setting fails, this test will raise an error.

    Unfortunately, due to circular dependencies between build/build_service, it's not easy to mock out `deploy_site`.
    So we mock out the S3FileSystem, which is initialized within `deploy_site`. This will throw an error if invoked.

    `create_versioned_assets` is mocked out because that function is only needed to generate css/js, which is tested
    in a separate step outside of pytest.

    `write_html` is mocked out so that we don't need to be able to write to a filesystem.

    We should look at expanding test coverage of the static site builder eventually, but such a task should probably
    also include refactoring the site builder to be more modular, less tightly-coupled, and more easy to test.
    """
    with patch.dict(single_use_app.config):
        with patch(
                "application.sitebuilder.build.push_site") as push_site_patch:
            with patch("application.sitebuilder.build.pull_current_site"
                       ) as pull_current_site_patch:
                with patch("application.sitebuilder.build_service.S3FileSystem"
                           ) as s3_fs_patch:
                    with patch(
                            "application.dashboard.data_helpers.trello_service"
                    ) as trello_service_patch:
                        with patch(
                                "application.sitebuilder.build.create_versioned_assets"
                        ):
                            with patch(
                                    "application.sitebuilder.build.write_html"
                            ):
                                single_use_app.config["PUSH_SITE"] = False
                                single_use_app.config["DEPLOY_SITE"] = False
                                pull_current_site_patch.side_effect = UnexpectedMockInvocationException
                                push_site_patch.side_effect = UnexpectedMockInvocationException
                                s3_fs_patch.side_effect = UnexpectedMockInvocationException
                                trello_service_patch.get_measure_cards.return_value = []

                                from tests.test_data.chart_and_table import chart, simple_table

                                # Including these three versioned pages ensures the build test exercises the logic to
                                # build multiple page versions
                                measure = MeasureFactory()
                                # Outdated version
                                MeasureVersionWithDimensionFactory(
                                    measure=measure,
                                    status="APPROVED",
                                    latest=False,
                                    published_at=datetime.now().date(),
                                    version="1.0",
                                    dimensions__dimension_chart=None,
                                    dimensions__dimension_table__table_object=
                                    simple_table(),
                                )
                                # Latest published version
                                MeasureVersionWithDimensionFactory(
                                    measure=measure,
                                    status="APPROVED",
                                    latest=False,
                                    published_at=datetime.now().date(),
                                    version="2.0",
                                    dimensions__dimension_chart=None,
                                    dimensions__dimension_table__table_object=
                                    simple_table(),
                                )
                                # Newer draft version
                                MeasureVersionWithDimensionFactory(
                                    measure=measure,
                                    status="DRAFT",
                                    published_at=None,
                                    latest=True,
                                    version="2.1",
                                    dimensions__dimension_chart=None,
                                    dimensions__dimension_table=None,
                                )

                                # Publish another page with dimension, chart and table to ensure there's an item for
                                # each of the dashboard views
                                MeasureVersionWithDimensionFactory(
                                    status="APPROVED",
                                    latest=True,
                                    published_at=datetime.now().date() -
                                    timedelta(weeks=1),
                                    version="1.0",
                                    measure__subtopics__topic__slug="topic",
                                    measure__subtopics__slug="subtopic",
                                    measure__slug="measure",
                                    dimensions__guid="dimension-guid",
                                    dimensions__dimension_chart__chart_object=
                                    chart,
                                    dimensions__dimension_table__table_object=
                                    simple_table(),
                                    uploads__guid="test-download",
                                    uploads__title="Test measure page data",
                                    uploads__file_name=
                                    "test-measure-page-data.csv",
                                )

                                # Materialized views are initially empty - populate them with our fixture page data
                                refresh_materialized_views()

                                do_it(single_use_app, request_build())
Beispiel #6
0
def test_table_object_builder_does_build_object_from_simple_table():
    measure_version = MeasureVersionWithDimensionFactory(dimensions__dimension_table__table_object=simple_table())
    # given - a table without a category_caption value
    builder = DimensionObjectBuilder()
    dimension = measure_version.dimensions[0]

    # when we process the object
    table_object = builder.build(dimension)

    # then the header for the returned table should match the ones from the simple table
    assert table_object is not None
    assert table_object.get("table").get("title") == "Title of simple table"
def stub_simple_table_object():
    return simple_table()