def construct_test_table_builder_page(driver, live_server, measure_version, rdu_user):
    driver_login(driver, live_server, rdu_user)
    """
    BROWSE TO POINT WHERE WE CAN ADD A MEASURE
    """
    home_page = HomePage(driver, live_server)
    home_page.click_topic_link(measure_version.measure.subtopic.topic)
    topic_page = TopicPage(driver, live_server, measure_version.measure.subtopic.topic)
    topic_page.expand_accordion_for_subtopic(measure_version.measure.subtopic)
    """
    SET UP A SIMPLE DIMENSION WE CAN BUILD TEST TABLES ON
    """
    topic_page.click_add_measure(measure_version.measure.subtopic)
    topic_page.wait_until_url_contains("/measure/new")
    create_measure(
        driver, live_server, measure_version, measure_version.measure.subtopic.topic, measure_version.measure.subtopic
    )
    topic_page.wait_until_url_contains("/edit")
    edit_measure_page = MeasureEditPage(driver)
    edit_measure_page.get()
    dimension = MinimalRandomDimension()
    edit_measure_page.click_add_dimension()
    edit_measure_page.wait_until_url_contains("/dimension/new")
    create_dimension_page = DimensionAddPage(driver)
    create_dimension_page.set_title(dimension.title)
    create_dimension_page.set_time_period(dimension.time_period)
    create_dimension_page.set_summary(dimension.summary)
    create_dimension_page.click_save()
    edit_dimension_page = DimensionEditPage(driver)
    edit_dimension_page.get()
    edit_dimension_page.wait_for_seconds(1)
    edit_dimension_page.click_create_table()
    edit_dimension_page.wait_until_url_contains("create-table")
    table_builder_page = TableBuilderPage(driver)
    return table_builder_page
def construct_test_chart_builder_page(
    driver, live_server, page, stub_subtopic_page, stub_topic_page, test_app_editor, stub_published_measure_page
):
    login(driver, live_server, test_app_editor)
    """
    BROWSE TO POINT WHERE WE CAN ADD A MEASURE
    """
    home_page = HomePage(driver, live_server)
    home_page.click_topic_link(stub_topic_page)
    topic_page = TopicPage(driver, live_server, stub_topic_page)
    topic_page.expand_accordion_for_subtopic(stub_subtopic_page)
    """
    SET UP A SIMPLE DIMENSION WE CAN BUILD TEST CHARTS ON
    """
    topic_page.click_add_measure(stub_subtopic_page)
    topic_page.wait_until_url_contains("/measure/new")
    create_measure(driver, live_server, page, stub_topic_page, stub_subtopic_page)
    topic_page.wait_until_url_contains("/edit")
    edit_measure_page = MeasureEditPage(driver)
    edit_measure_page.get()
    dimension = MinimalRandomDimension()
    edit_measure_page.click_add_dimension()
    edit_measure_page.wait_until_url_contains("/dimension/new")
    create_dimension_page = DimensionAddPage(driver)
    create_dimension_page.set_title(dimension.title)
    create_dimension_page.set_time_period(dimension.time_period)
    create_dimension_page.set_summary(dimension.summary)
    create_dimension_page.click_save()
    edit_dimension_page = DimensionEditPage(driver)
    edit_dimension_page.get()
    edit_dimension_page.wait_for_seconds(1)
    edit_dimension_page.click_create_chart()
    edit_dimension_page.wait_until_url_contains("create-chart")
    chart_builder_page = ChartBuilderPage(driver, edit_dimension_page)
    return chart_builder_page
def create_measure_starting_at_topic_page(driver, live_server,
                                          stub_subtopic_page, stub_topic_page):
    """
    CREATE v1 1: Click through to subtopic topic page
    """
    topic_page = TopicPage(driver, live_server, stub_topic_page)
    assert topic_page.is_current()
    topic_page.expand_accordion_for_subtopic(stub_subtopic_page)
    """
    CREATE v1 2: Add measure page
    """
    topic_page.click_add_measure(stub_subtopic_page)
    measure_create_page = MeasureCreatePage(driver, live_server,
                                            stub_topic_page,
                                            stub_subtopic_page)
    assert measure_create_page.is_current()
    """
    CREATE v1 3: Fill measure create page
    """
    page = RandomMeasure()
    measure_create_page.set_title(page.title)
    measure_create_page.click_save()
    """
    CREATE v1 4: Add some content
    """
    measure_edit_page = MeasureEditPage(driver)
    measure_edit_page.fill_measure_page(page)
    measure_edit_page.click_save()
    """
    CREATE v1 5: Now it has been added we ought to have a generated GUID which we will need so
    we may have to retrieve the page again
    """
    page_service = PageService()
    page = page_service.get_page_with_title(page.title)
    return measure_edit_page, page
def create_measure_with_minimal_content(driver, live_server,
                                        stub_subtopic_page, stub_topic_page):
    """
    CREATE v1 1: Click through to subtopic topic page
    """
    topic_page = TopicPage(driver, live_server, stub_topic_page)
    assert topic_page.is_current()
    topic_page.expand_accordion_for_subtopic(stub_subtopic_page)
    """
    CREATE v1 2: Add measure page
    """
    topic_page.click_add_measure(stub_subtopic_page)
    measure_create_page = MeasureCreatePage(driver, live_server,
                                            stub_topic_page,
                                            stub_subtopic_page)
    assert measure_create_page.is_current()
    """
    CREATE v1 3: Fill measure create page
    """
    page = RandomMeasure()
    measure_create_page.set_title(page.title)
    measure_create_page.click_save()

    measure_edit_page = MeasureEditPage(driver)
    measure_edit_page.click_breadcrumb_for_page(stub_topic_page)
    """
    CREATE v1 5: Now it has been added we ought to have a generated GUID which we will need so
    we have to retrieve the page again
    """
    page = get_page_with_title(page.title)
    return page
def navigate_to_edit_page(driver, live_server, topic, subtopic, measure):
    """
    ENTRY 1: Home page
    """
    topic_page = TopicPage(driver, live_server, topic)
    if not topic_page.is_current():
        navigate_to_topic_page(driver, live_server, topic)

    topic_page.expand_accordion_for_subtopic(subtopic)
    topic_page.click_edit_button(measure)
def create_measure_starting_at_topic_page(
    driver, live_server, topic, subtopic, sample_measure_version, sample_data_source
):
    """
    CREATE v1 1: Click through to subtopic topic page
    """
    topic_page = TopicPage(driver, live_server, topic)
    assert topic_page.is_current()
    topic_page.expand_accordion_for_subtopic(subtopic)
    """
    CREATE v1 2: Add measure page
    """
    topic_page.click_add_measure(subtopic)
    measure_create_page = MeasureCreatePage(driver, live_server, topic, subtopic)
    assert measure_create_page.is_current()
    """
    CREATE v1 3: Fill measure create page
    """
    measure_create_page.set_title(sample_measure_version.title)
    measure_create_page.click_save()
    """
    CREATE v1 4: Add some content
    """
    measure_edit_page = MeasureEditPage(driver)
    measure_edit_page.fill_measure_page(sample_measure_version)
    measure_edit_page.click_save()
    """
    CREATE v1 5: Add a data source
    """
    measure_edit_page.click_add_primary_data_source()

    fill_in(driver, label_text="Search for an existing data source", with_text="My data source")
    click_button_with_text(driver, "Search")
    click_link_with_text(driver, "Create a new data source")

    data_source_page = CreateDataSourcePage(driver)
    data_source_page.fill_data_source(sample_data_source)
    data_source_page.click_save()
    data_source_page.click_back()
    """
    CREATE v1 5: Upload a source data file
    """
    measure_edit_page.click_add_source_data()
    add_source_data_page = AddSourceDataPage(driver)
    add_source_data_page.fill_source_data_page(sample_measure_version.uploads[0])
    add_source_data_page.click_save()
    """
    CREATE v1 6: Now it has been added we ought to have a generated GUID which we will need so
    we may have to retrieve the page again
    """
    created_measure_version = get_page_with_title(sample_measure_version.title)
    return measure_edit_page, created_measure_version
def assert_page_correct(driver, live_server, stub_topic_page, stub_subtopic_page, page, status):
    topic_page = TopicPage(driver, live_server, stub_topic_page)
    topic_page.expand_accordion_for_subtopic(stub_subtopic_page)

    assert_page_status(driver, topic_page, page, status)

    topic_page.click_preview_measure(page)
    assert_page_details(driver, page)

    driver.back()
def test_delete_a_draft_1_0_measure(driver, live_server):
    rdu_user = UserFactory(user_type=TypeOfUser.RDU_USER, active=True)
    measure_version = MeasureVersionFactory(status="APPROVED")

    assert Measure.query.count() == 1

    # GIVEN we create a version 1.0 measure
    driver_login(driver, live_server, rdu_user)
    navigate_to_topic_page(driver, live_server,
                           measure_version.measure.subtopic.topic)
    measure = create_measure_with_minimal_content(
        driver, live_server, measure_version.measure.subtopic,
        measure_version.measure.subtopic.topic)

    assert Measure.query.count() == 2

    # WHEN we go to the topic page
    topic_page = TopicPage(driver, live_server,
                           measure_version.measure.subtopic.topic)
    topic_page.get()

    # No longer required; accordion remembers the expanded state from earlier in the test.
    # topic_page.expand_accordion_for_subtopic(measure_version.measure.subtopic)

    # THEN measure is listed
    assert topic_page.measure_is_listed(measure) is True

    driver.find_element_by_link_text(measure.title).click()

    # WHEN we walk through the delete process
    driver.find_element_by_xpath(
        f"//a[contains(., 'Delete page: {measure.title}')]").click()

    driver.find_element_by_xpath('//button[text()="Yes, delete"]').click()

    topic_page.get()
    # This test has been passing when the final .get() returned a 500 error page.
    # TODO: Check the page didn't error and is the actual page we expect to not find the thing in
    # TODO: Maybe assert 200 responses after all get()s in functional tests?
    assert topic_page.measure_is_listed(measure) is False

    assert Measure.query.count() == 1
def test_adding_an_existing_data_source(driver, app, live_server):
    rdu_user = UserFactory(user_type=TypeOfUser.RDU_USER, active=True)

    topic = TopicFactory.create(title="Police and crime")
    subtopic = SubtopicFactory.create(title="Policing", topic=topic)
    DataSourceFactory.create(title="Police statistics 2019")

    existing_measure = MeasureFactory.create(subtopics=[subtopic])
    MeasureVersionFactory.create(status="APPROVED", measure=existing_measure)

    driver_login(driver, live_server, rdu_user)
    home_page = HomePage(driver, live_server)

    home_page.click_topic_link(topic)

    topic_page = TopicPage(driver, live_server, topic)
    topic_page.expand_accordion_for_subtopic(subtopic)

    topic_page.click_add_measure(subtopic)

    create_measure_page = MeasureEditPage(driver)
    create_measure_page.set_title("Arrests")
    create_measure_page.click_save()

    create_measure_page.click_add_primary_data_source()

    fill_in(driver,
            label_text="Search for an existing data source",
            with_text="Police statistics")
    click_button_with_text(driver, "Search")

    label_for_existing_data_source = driver.find_element_by_xpath(
        "//label[text()='Police statistics 2019']")

    label_for_existing_data_source.click()

    click_button_with_text(driver, "Select")

    assert "Successfully added the data source ‘Police statistics 2019’" in driver.page_source

    click_link_with_text(driver, "Preview this version")

    assert "Police statistics 2019" in driver.page_source
def test_can_create_a_measure_page(driver, app, test_app_editor, live_server,
                                   stub_topic_page, stub_subtopic_page,
                                   stub_published_measure_page):
    page = RandomMeasure()

    login(driver, live_server, test_app_editor)
    """
    BROWSE TO POINT WHERE WE CAN ADD A MEASURE
    """
    home_page = HomePage(driver, live_server)
    home_page.click_topic_link(stub_topic_page)

    topic_page = TopicPage(driver, live_server, stub_topic_page)
    topic_page.expand_accordion_for_subtopic(stub_subtopic_page)
    """
    CREATE A NEW MEASURE
    """
    topic_page.click_add_measure(stub_subtopic_page)
    topic_page.wait_until_url_contains("/measure/new")
    create_measure(driver, live_server, page, stub_topic_page,
                   stub_subtopic_page)
    """
    EDIT THE MEASURE
    """
    topic_page.wait_until_url_contains("/edit")
    edit_measure_page = MeasureEditPage(driver)

    edit_measure_page.set_measure_summary(page.measure_summary)
    edit_measure_page.set_summary(page.summary)
    edit_measure_page.click_save()
    assert edit_measure_page.is_current()
    """
    PREVIEW CURRENT PROGRESS
    """
    edit_measure_page.click_preview()
    edit_measure_page.wait_until_url_does_not_contain("/cms/")

    preview_measure_page = MeasurePreviewPage(driver)
    assert preview_measure_page.is_current()

    assert_page_contains(preview_measure_page, page.title)
    assert_page_contains(preview_measure_page, page.measure_summary)
    assert_page_contains(preview_measure_page, page.summary)
    """
    ADD A DIMENSION
    Save some dimension data
    """
    edit_measure_page.get()
    assert edit_measure_page.is_current()

    dimension = RandomDimension()

    edit_measure_page.click_add_dimension()
    edit_measure_page.wait_until_url_contains("/dimension/new")

    create_dimension_page = DimensionAddPage(driver)

    create_dimension_page.set_title(dimension.title)
    create_dimension_page.set_time_period(dimension.time_period)
    create_dimension_page.set_summary(dimension.summary)
    create_dimension_page.click_save()

    create_dimension_page.wait_for_seconds(1)
    edit_dimension_page = DimensionEditPage(driver)
    assert edit_dimension_page.is_current()

    preview_measure_page.get()
    edit_dimension_page.wait_until_url_does_not_contain("/cms/")
    assert_page_contains(preview_measure_page, dimension.title)
    assert_page_contains(preview_measure_page, dimension.time_period)
    assert_page_contains(preview_measure_page, dimension.summary)
    """
    EDIT A DIMENSION
    """
    edit_dimension_page.get()
    edit_dimension_page.wait_for_seconds(1)
    assert edit_dimension_page.is_current()

    edit_dimension_page.set_summary("some updated text")
    edit_dimension_page.click_update()

    assert edit_dimension_page.is_current()

    preview_measure_page.get()
    edit_dimension_page.wait_until_url_does_not_contain("/cms/")
    assert_page_contains(preview_measure_page, "some updated text")
    """
    CHART BUILDER
    test content has been moved to a separate set of functional tests
    """
    """
    CREATE A SIMPLE TABLE
    """
    edit_dimension_page.get()
    edit_dimension_page.wait_for_seconds(1)
    assert edit_dimension_page.is_current()
    edit_dimension_page.click_create_table()
    edit_dimension_page.wait_until_url_contains("create-table")

    table_builder_page = TableBuilderPage(driver)
    assert table_builder_page.is_current()

    inject_data(driver, simple_data)
    table_builder_page.click_data_okay()
    table_builder_page.wait_for_seconds(1)
    table_builder_page.select_column(1, "Value")
    table_builder_page.wait_for_seconds(1)
    table_builder_page.click_save()
    table_builder_page.wait_for_seconds(1)
    """
    CREATE A TABLE WITH TWO COLUMNS
    """
    table_builder_page.get()
    table_builder_page.click_data_edit()
    table_builder_page.wait_for_seconds(1)
    inject_data(driver, ethnicity_by_gender_data)
    table_builder_page.click_data_okay()
    table_builder_page.wait_for_seconds(1)

    table_builder_page.select_data_style("Use ethnicity for rows")
    table_builder_page.wait_for_seconds(1)
    table_builder_page.select_columns_when_ethnicity_is_row("Gender")
    table_builder_page.select_column(1, "Value")
    table_builder_page.select_column(2, "Gender")
    table_builder_page.wait_for_seconds(1)

    table_builder_page.click_save()
    table_builder_page.wait_for_seconds(1)
def test_delete_a_draft_1_0_measure(driver, test_app_editor, live_server,
                                    stub_topic_page, stub_subtopic_page,
                                    stub_published_measure_page):
    # GIVEN we create a version 1.0 measure
    login(driver, live_server, test_app_editor)
    navigate_to_topic_page(driver, live_server, stub_topic_page)
    measure = create_measure_with_minimal_content(driver, live_server,
                                                  stub_subtopic_page,
                                                  stub_topic_page)

    # WHEN we go to the topic page
    topic_page = TopicPage(driver, live_server, stub_topic_page)
    topic_page.get()
    topic_page.expand_accordion_for_subtopic(stub_subtopic_page)

    # THEN measure is listed
    assert topic_page.measure_is_listed(measure) is True

    # WHEN we walk through the delete process
    topic_page.click_delete_button(measure)
    topic_page.select_yes_radio(measure)
    topic_page.click_confirm_delete(measure)

    topic_page.get()
    assert topic_page.measure_is_listed(measure) is False