示例#1
0
def test_calculate_count(client, init_database):
    """
    GIVEN a Flask application, study, responses
    WHEN CreateHeatMap.calculate_count is called 
    THEN check returned data
    """

    admin = create_admin(client)
    participant_1 = create_participant(client)
    participant_2 = create_participant(client, username='******')
    user_group = create_user_group(client,
                                   participants=[participant_1, participant_2],
                                   creator=admin)
    study = create_study(client,
                         creator=admin,
                         user_group=user_group,
                         data_value_labels=[])
    response_1 = create_response(client,
                                 study=study,
                                 creator=admin,
                                 participant=participant_1)
    response_2 = create_response(client,
                                 study=study,
                                 creator=admin,
                                 participant=participant_2)

    c_1 = Card.query.filter_by(name='Entertainment').first()
    c_2 = Card.query.filter_by(name='Health Service').first()

    c = CreateOneHeatMapCount(study)
    data = c.calculate_count(c_1, c_2)

    assert 2 in data["values"]
示例#2
0
def test_get_all_responses(client, init_database):
    """
    GIVEN a Flask application and logged in user
    WHEN '/api/get/response/all' is requested with valid access token (GET)
    THEN check status code and response content and length
    """
    with client.application.test_request_context():
        admin = create_admin(client)
        access_token = api_login(client)
        participant_1 = create_participant(client, username="******")
        participant_2 = create_participant(client, username="******")
        user_group = create_user_group(
            client, participants=[participant_1, participant_2], creator=admin)
        study = create_study(client, creator=admin, user_group=user_group)
        study_response_1 = create_response(client,
                                           creator=admin,
                                           participant=participant_1,
                                           study=study)
        study_response_2 = create_response(client,
                                           creator=admin,
                                           participant=participant_2,
                                           study=study)

        response = client.get(
            url_for("api.get_all_responses"),
            headers={
                "Authorization":
                "Bearer {}".format(access_token["access_token"])
            },
        )
        assert response.status_code == 200
        assert len(response.json) == 2
def test_get_heat_maps(client, init_database):
    """
    GIVEN a Flask application, admin, study, responses
    WHEN '/responses/heat_maps/<int:id>' is requested (GET)
    THEN check response content, status code
    """
    with client.application.test_request_context():
        admin = create_admin(client)

        participant_1 = create_participant(client, username="******")
        participant_2 = create_participant(client, username="******")

        user_group = create_user_group(
            client, creator=admin, participants=[participant_1, participant_2]
        )
        study = create_study(client, creator=admin, user_group=user_group)
        study_response_1 = create_response(
            client, participant=participant_1, creator=admin, study=study
        )
        study_response_2 = create_response(
            client, participant=participant_2, creator=admin, study=study
        )

        login(client, username="******", password="******")

        response = client.get(
            url_for("responses.heat_maps", id=study.id), follow_redirects=True
        )

        assert response.status_code == 200
        assert b"<h3>Heat Maps</h3>" in response.data
        if len(study.data_value_labels) > 0:
            assert b"<label>Data Value Label</label>" in response.data
        assert b"<label>Type</label>" in response.data
        assert b'<option type="true">Count</option>' in response.data
        assert b'<option type="false">Not Count</option>' in response.data

        assert bytes(study.card_set_x.name, "utf-8") in response.data
        for card in study.card_set_x.cards:
            assert bytes(card.name, "utf-8") in response.data
        assert bytes(study.card_set_y.name, "utf-8") in response.data
        for card in study.card_set_y.cards:
            assert bytes(card.name, "utf-8") in response.data
        for data_value_label in study.data_value_labels:
            assert bytes(data_value_label.label, "utf-8") in response.data

        assert bytes(str(participant_1.id), "utf-8") in response.data
        assert bytes(str(participant_2.id), "utf-8") in response.data

        client.get(url_for("auth.logout"))
        admin2 = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")

        response = client.get(
            url_for("responses.heat_maps", id=study.id), follow_redirects=False
        )

        assert urlparse(response.location).path == url_for("auth.login")
示例#4
0
def test_post_compare_responses(client, init_database):
    """
    GIVEN a Flask application, admin, study, responses
    WHEN '/responses/compare_responses/<int:id>' is posted (POST)
    THEN check response content, status code
    """
    with client.application.test_request_context():
        admin = create_admin(client)

        participant_1 = create_participant(
            client,
            username="******",
            password="******",
            gender="Male",
            age_group="20-29",
            country_of_birth="AF",
            latest_country="AX",
            education_level="L6",
            occupation="Almoner",
            completed_form=True,
        )
        participant_2 = create_participant(client, username="******")

        user_group = create_user_group(
            client, creator=admin, participants=[participant_1, participant_2])
        study = create_study(client, creator=admin, user_group=user_group)
        study_response_1 = create_response(client,
                                           participant=participant_2,
                                           creator=admin,
                                           study=study)
        study_response_2 = create_response(client,
                                           participant=participant_2,
                                           creator=admin,
                                           study=study)

        login(client, username="******", password="******")

        data = {
            "response_id_1": study_response_1.id,
            "response_id_2": study_response_2.id,
        }

        response = client.post(
            url_for("responses.compare_responses", id=study.id),
            json=data,
            follow_redirects=True,
        )

        for data_value in study_response_1.data_values:
            assert bytes(data_value.data_value_label.label,
                         'utf-8') in response.data
            assert bytes(str(data_value.value), 'utf-8') in response.data

        for data_value in study_response_2.data_values:
            assert bytes(data_value.data_value_label.label,
                         'utf-8') in response.data
            assert bytes(str(data_value.value), 'utf-8') in response.data
示例#5
0
def test_get_compare_responses(client, init_database):
    """
    GIVEN a Flask application, admin, study, responses
    WHEN '/responses/compare_responses/<int:id>' is requested (GET)
    THEN check response content, status code
    """
    with client.application.test_request_context():
        admin = create_admin(client)

        participant_1 = create_participant(client, username="******")
        participant_2 = create_participant(client, username="******")

        user_group = create_user_group(
            client, creator=admin, participants=[participant_1, participant_2])
        study = create_study(client, creator=admin, user_group=user_group)
        study_response_1 = create_response(client,
                                           participant=participant_1,
                                           creator=admin,
                                           study=study)
        study_response_2 = create_response(client,
                                           participant=participant_2,
                                           creator=admin,
                                           study=study)

        login(client, username="******", password="******")

        response = client.get(
            url_for("responses.compare_responses", id=study.id),
            follow_redirects=True,
        )

        assert response.status_code == 200
        assert b"Compare Responses" in response.data
        assert (bytes(
            "Here you can compare responses to study <b>{}</b> from pairs of users."
            .format(study.name),
            "utf-8",
        ) in response.data)
        assert b"Response 1" in response.data
        assert b"Response 2" in response.data
        assert b"Average" in response.data
        assert bytes(str(participant_1.username), "utf-8") in response.data
        assert bytes(str(participant_2.username), "utf-8") in response.data

        client.get(url_for("auth.logout"))
        admin2 = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")

        response = client.get(
            url_for("responses.compare_responses", id=study.id),
            follow_redirects=False,
        )

        assert urlparse(response.location).path == url_for("auth.login")
def test_get_create_pdf(client, init_database):
    """
    GIVEN a Flask application, admin, study, responses
    WHEN '/responses/create_pdf/<int:id>' is requested (GET)
    THEN check response content, status code
    """
    with client.application.test_request_context():
        admin = create_admin(client)

        participant_1 = create_participant(client, username="******")
        participant_2 = create_participant(client, username="******")

        user_group = create_user_group(
            client, creator=admin, participants=[participant_1, participant_2]
        )
        study = create_study(client, creator=admin, user_group=user_group)
        study_response_1 = create_response(
            client, participant=participant_1, creator=admin, study=study
        )
        study_response_2 = create_response(
            client, participant=participant_2, creator=admin, study=study
        )

        login(client, username="******", password="******")

        response = client.get(
            url_for("responses.create_pdf", id=study.id), follow_redirects=True
        )

        assert response.status_code == 200
        assert b"Create PDF" in response.data
        assert (
            b"<p>Here you can generate a PDF and select what responses you want to include in the pdf</p>"
            in response.data
        )
        assert b"All Responses" in response.data
        assert b"Average Response" in response.data
        assert b"Specific Responses" in response.data
        assert bytes(str(participant_1.username), "utf-8") in response.data
        assert bytes(str(participant_2.username), "utf-8") in response.data

        client.get(url_for("auth.logout"))
        admin2 = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")

        response = client.get(
            url_for("responses.create_pdf", id=study.id),
            follow_redirects=False,
        )

        assert urlparse(response.location).path == url_for("auth.login")
示例#7
0
def test_CreateOneHeatMapCount(client, init_database):
    """
    GIVEN a Flask application, study, response
    WHEN CreateOneHeatMapCount.add is called 
    THEN check no error and content
    """
    admin = create_admin(client)
    participant = create_participant(client)
    user_group = create_user_group(client,
                                   participants=[participant],
                                   creator=admin)
    study = create_study(client,
                         creator=admin,
                         user_group=user_group,
                         data_value_labels=[])
    response = create_response(client,
                               study=study,
                               creator=admin,
                               participant=participant)

    c = CreateOneHeatMapCount(study)
    c.add(study.card_set_x.cards[0], study.card_set_y.cards[0], None)
    plot = c.plots

    assert isinstance(plot, list)
    assert isinstance(plot[0], tuple)
    assert len(plot) == 1
示例#8
0
def test_average_response(client, init_database):
    """
    GIVEN a Flask application, study, responses
    WHEN average_response is called 
    THEN check no error and return type
    """
    with client.application.test_request_context():
        admin = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")
        p1 = create_participant(client, username="******")
        p2 = create_participant(client, username="******")
        p3 = create_participant(client, username="******")
        p4 = create_participant(client, username="******")
        user_group = create_user_group(client,
                                       creator=admin,
                                       participants=[p1, p2, p3, p4])
        study = create_study(client, user_group=user_group, creator=admin)
        response_1 = create_response(client,
                                     study=study,
                                     participant=p1,
                                     creator=admin)
        response_2 = create_response(client,
                                     study=study,
                                     participant=p2,
                                     creator=admin)
        response_3 = create_response(client,
                                     study=study,
                                     participant=p3,
                                     creator=admin)
        response_4 = create_response(client,
                                     study=study,
                                     participant=p4,
                                     creator=admin)

        avg_response = average_response(study)

        for card_position_avg in avg_response.card_positions:
            for card_position in response_1.card_positions:
                if card_position_avg.card == card_position.card:
                    assert card_position_avg.position == card_position.position
def test_get_general_study(client, init_database):
    """
    GIVEN a Flask application, admin, study, responses
    WHEN '/responses/<int:id>' is requested (GET)
    THEN check response content, status code
    """
    with client.application.test_request_context():
        admin = create_admin(client)
        participant = create_participant(
            client,
            username="******",
            password="******",
            gender="Male",
            age_group="20-29",
            country_of_birth="AF",
            latest_country="AX",
            education_level="L6",
            occupation="Almoner",
            completed_form=True,
        )

        user_group = create_user_group(client,
                                       creator=admin,
                                       participants=[participant])
        study = create_study(client, creator=admin, user_group=user_group)
        study_response = create_response(client,
                                         participant=participant,
                                         creator=admin,
                                         study=study)

        login(client, username="******", password="******")

        response = client.get(url_for("responses.general", id=study.id),
                              follow_redirects=True)

        assert response.status_code == 200
        assert (bytes(str(len(study.responses)) + " Responses", "utf-8")
                in response.data)

        client.get(url_for("auth.logout"))
        admin2 = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")

        response = client.get(url_for("responses.general", id=study.id),
                              follow_redirects=False)

        assert urlparse(response.location).path == url_for("auth.login")
def test_get_card_y_responses(client, init_database):
    """
    GIVEN a Flask application, study, response
    WHEN get_card_y_responses is called
    THEN check no error and return type
    """
    admin = create_admin(client)
    participant = create_participant(client)
    user_group = create_user_group(client,
                                   participants=[participant],
                                   creator=admin)
    study = create_study(client, creator=admin, user_group=user_group)
    response = create_response(client,
                               study=study,
                               creator=admin,
                               participant=participant)

    updated_heat_maps = get_card_y_responses(study)

    assert isinstance(updated_heat_maps, tuple)
示例#11
0
def test_get_individual_response(client, init_database):
    """
    GIVEN a Flask application and a logged in admin
    WHEN '/api/get/response/1' is requested with valid access token (GET)
    THEN check response code, and content
    """

    with client.application.test_request_context():
        admin = create_admin(client)
        access_token = api_login(client)
        study = create_study(client, creator=admin)
        study_response = create_response(client,
                                         creator=admin,
                                         study=study,
                                         participant=study.user_group.users[0])

        response = client.get(
            url_for("api.get_response", id=1),
            headers={
                "Authorization":
                "Bearer {}".format(access_token["access_token"])
            },
        )

        assert response.status_code == 200

        json = response.json
        assert json["id"] == study_response.id
        assert json["study_id"] == study_response.study_id
        assert json["participant_id"] == study_response.participant_id
        assert json["card_positions"] == [
            create_card_position_json(card_position)
            for card_position in study_response.card_positions
        ]
        assert json["data_values"] == [
            create_data_value_json(data_value)
            for data_value in study_response.data_values
        ]
def test_get_average_response(client, init_database):
    """
    GIVEN a Flask application, admin, study, responses
    WHEN '/responses/average_response/<int:id>' is requested (GET)
    THEN check response content, status code
    """
    with client.application.test_request_context():
        admin = create_admin(client)

        participant_1 = create_participant(client, username="******")
        participant_2 = create_participant(client, username="******")

        user_group = create_user_group(
            client, creator=admin, participants=[participant_1, participant_2])

        study = create_study(client, creator=admin, user_group=user_group)
        study_response_1 = create_response(client,
                                           participant=participant_1,
                                           creator=admin,
                                           study=study)
        study_response_2 = create_response(client,
                                           participant=participant_2,
                                           creator=admin,
                                           study=study)

        login(client, username="******", password="******")

        response = client.get(
            url_for("responses.average_response", id=study.id),
            follow_redirects=True,
        )

        assert response.status_code == 200
        for card in study.card_set_x.cards:
            assert bytes(card.name, "utf-8") in response.data
            assert bytes(card.description, "utf-8") in response.data
            assert bytes(card.image, "utf-8") in response.data

        assert (bytes("Highest " + study.card_set_x.measure, "utf-8")
                in response.data)
        assert (bytes("Lowest " + study.card_set_x.measure, "utf-8")
                in response.data)

        for card in study.card_set_y.cards:
            assert bytes(card.name, "utf-8") in response.data
            assert bytes(card.description, "utf-8") in response.data
            assert bytes(card.image, "utf-8") in response.data

        assert (bytes("Highest " + study.card_set_y.measure, "utf-8")
                in response.data)
        assert (bytes("Lowest " + study.card_set_y.measure, "utf-8")
                in response.data)

        assert (bytes(study.data_value_labels[0].label, "utf-8")
                in response.data)

        client.get(url_for("auth.logout"))
        admin2 = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")

        response = client.get(
            url_for("responses.average_response", id=study.id),
            follow_redirects=False,
        )

        assert urlparse(response.location).path == url_for("auth.login")
示例#13
0
def test_calculate_price(client, init_database):
    """
    GIVEN a Flask application, study, responses
    WHEN CreateHeatMap.calculate_price is called 
    THEN check returned data
    """
    admin = create_admin(client)
    participant_1 = create_participant(client)
    participant_2 = create_participant(client, username='******')
    user_group = create_user_group(client,
                                   participants=[participant_1, participant_2],
                                   creator=admin)
    study = create_study(client,
                         creator=admin,
                         user_group=user_group,
                         data_value_labels=[])
    response_1 = create_response(client,
                                 study=study,
                                 creator=admin,
                                 participant=participant_1)
    response_2 = create_response(client,
                                 study=study,
                                 creator=admin,
                                 participant=participant_2)

    dv1 = DataValue.query.filter(DataValue.response == response_1).filter(
        DataValue.column == 0).filter(DataValue.row == 1).filter(
            DataValue.data_value_label == study.data_value_labels[0]).first()
    dv2 = DataValue.query.filter(DataValue.response == response_2).filter(
        DataValue.column == 0).filter(DataValue.row == 1).filter(
            DataValue.data_value_label == study.data_value_labels[0]).first()
    val_1 = 100
    val_2 = 50
    dv1.value = val_1
    dv2.value = val_2

    c_1 = Card.query.filter_by(name='Entertainment').first()
    c_2 = Card.query.filter_by(name='Health Service').first()
    for pos in c_1.positions:
        pos.position = 0
    for pos in c_2.positions:
        pos.position = 1

    db.session.commit()
    max_val = float('-inf')
    min_val = float('inf')
    for data_value in response_1.data_values:
        if data_value.data_value_label == study.data_value_labels[0]:
            if data_value.value > max_val:
                max_val = data_value.value
            if data_value.value < min_val:
                min_val = data_value.value

    normalised_1 = (val_1 - min_val) / (max_val - min_val)

    max_val = float('-inf')
    min_val = float('inf')

    for data_value in response_2.data_values:
        if data_value.data_value_label == study.data_value_labels[0]:
            if data_value.value > max_val:
                max_val = data_value.value
            if data_value.value < min_val:
                min_val = data_value.value

    normalised_2 = (val_2 - min_val) / (max_val - min_val)

    avg_normalised = (normalised_1 + normalised_2) / 2

    c = CreateOneHeatMapCount(study)

    data = c.calculate_price(card_x=c_1,
                             card_y=c_2,
                             data_value_label=study.data_value_labels[0])
    assert float("{:.2f}".format(avg_normalised)) in data["values"]
def test_create_pdf(client, init_database):
    """
    GIVEN a Flask application, study
    WHEN create_pdf is called 
    THEN check no error and return type
    """
    with client.application.test_request_context():
        admin = create_admin(client, username="******", password="******")
        login(client, username="******", password="******")
        p1 = create_participant(client, username="******")
        p2 = create_participant(client, username="******")
        p3 = create_participant(client, username="******")
        p4 = create_participant(client, username="******")
        user_group = create_user_group(client,
                                       creator=admin,
                                       participants=[p1, p2, p3, p4])
        study = create_study(client, user_group=user_group, creator=admin)
        response_1 = create_response(client,
                                     study=study,
                                     participant=p1,
                                     creator=admin)
        response_2 = create_response(client,
                                     study=study,
                                     participant=p2,
                                     creator=admin)
        response_3 = create_response(client,
                                     study=study,
                                     participant=p3,
                                     creator=admin)
        response_4 = create_response(client,
                                     study=study,
                                     participant=p4,
                                     creator=admin)

        file_path_1 = create_pdf(study, all_responses=True)

        assert isinstance(file_path_1, str)
        path = "app" + file_path_1
        assert os.path.exists(path) is True

        file_path_2 = create_pdf(study, average_response2=True)

        assert isinstance(file_path_2, str)
        path = "app" + file_path_2
        assert os.path.exists(path) == True
        assert file_path_1 != file_path_2

        file_path_3 = create_pdf(study,
                                 response_ids=[response_1.id, response_3.id])

        assert isinstance(file_path_3, str)
        path = "app" + file_path_3
        assert os.path.exists(path) == True
        assert file_path_2 != file_path_3
        assert file_path_1 != file_path_3

        file_path_4 = create_pdf(
            study,
            average_response2=True,
            response_ids=[response_1.id, response_3.id],
        )

        assert isinstance(file_path_4, str)
        path = "app" + file_path_4
        assert os.path.exists(path) == True
        assert file_path_3 != file_path_4
        assert file_path_2 != file_path_4
        assert file_path_1 != file_path_4

        file_path_5 = create_pdf(study,
                                 average_response2=True,
                                 all_responses=True)

        assert isinstance(file_path_1, str)
        path = "app" + file_path_5
        assert os.path.exists(path) == True
        assert file_path_4 != file_path_5
        assert file_path_3 != file_path_5
        assert file_path_2 != file_path_5
        assert file_path_1 != file_path_5