示例#1
0
def test_index(client):
    path = f"/admin"
    job_id = "test_index_job"
    api_key = "api_key"
    secret = app.config["ADMIN_SECRET"]
    base_code = "base_code"
    expected_judgments = 16
    with app.test_request_context(path):
        # if clear_session:
        #     with client:
        #         with client.session_transaction() as sess:
        #             sess.clear()
        client.get(path, follow_redirects=True)
        return client.post(path,
                           data={
                               "job_id": job_id,
                               api_key: "api_key",
                               "secret": secret,
                               "base_code": base_code,
                               "expected_judgments": expected_judgments
                           },
                           follow_redirects=True)
    job_config = get_job_config(get_db(), job_id)
    assert job_config["job_id"] == job_id
    assert job_config["base_code"] == base_code
    assert job_config["api_key"] == api_key
    assert job_config["expected_judgments"] == expected_judgments

    expected_judgments = 32
    base_code = "super_base_code"
    job_config["expected_judgments"] = expected_judgments
    job_config["base_code"] = base_code
    with app.test_request_context(path):
        client.get(path, follow_redirects=True)
        return client.post(path, data=job_config, follow_redirects=True)
示例#2
0
def test_index_auto(client, treatment, prefix=""):
    #takes the role assigned by the system and solves the corresponding tasks
    client = None
    job_id = "test_auto"
    for _ in range(TASK_REPETITION):
        auto_worker_id = generate_worker_id(f"{prefix}index_auto")
        path = f"/{treatment}/?worker_id={auto_worker_id}&job_id={job_id}"
        with app.test_request_context(path):
            client = get_client()
            res = client.get(path, follow_redirects=True)
            time.sleep(0.001)
            # Play as responder
            # print(res.data)
            if b"role of a RESPONDER" in res.data:
                # res = _process_resp(client, treatment, job_id=job_id, worker_id=auto_worker_id, min_offer=MIN_OFFER)
                # process_tasks(client, job_id=job_id, worker_id=auto_worker_id, bonus_mode="full", url_kwargs={"auto_finalize": 1, "treatment": treatment})
                res = _process_resp_tasks(client, treatment, job_id=job_id, worker_id=auto_worker_id, min_offer=get_min_offer())
                assert b"resp:" in res.data
                assert is_worker_available(auto_worker_id, get_table("resp", job_id=job_id, schema="result"))
            # Play a proposer
            elif b"role of a PROPOSER" in res.data:
                app.logger.warning("PROPOSER")
                res = client.get(path, follow_redirects=True)
                res = _process_prop_round(client, treatment, job_id=job_id, worker_id=auto_worker_id, offer=get_offer(), response_available=True)
                assert b"prop:" in res.data
                assert is_worker_available(auto_worker_id, get_table("resp", job_id=job_id, schema="result"))
            else:
                assert False, "Nooooooooooooooo"
            time.sleep(WEBHOOK_DELAY)
示例#3
0
def test_index(client, treatment, prefix="", completion_code_prefix="resp:", resp_feedback_fields=None):
    client = None
    job_id = "test"
    completion_code_prefix_bytes = completion_code_prefix.encode("utf-8") if completion_code_prefix else b"resp:"
    for _ in range(TASK_REPETITION):
        client = get_client()
        resp_worker_id = generate_worker_id(f"{prefix}index_resp")
        path = f"/{treatment}/?worker_id={resp_worker_id}"
        with app.test_request_context(path):
            res = client.get(path, follow_redirects=True)
            assert b"RESPONDER" in res.data
            # res = _process_resp_tasks(client, worker_id=worker_id)

            res = _process_resp(client, treatment, job_id=job_id, worker_id=resp_worker_id, min_offer=get_min_offer(), resp_feedback_fields=resp_feedback_fields)
            process_tasks(client, job_id=job_id, worker_id=resp_worker_id, bonus_mode="random", url_kwargs={"auto_finalize": 1, "treatment": treatment})
            assert completion_code_prefix_bytes in res.data
        time.sleep(WEBHOOK_DELAY)
        # let the auto-responder kick-in
        with app.app_context():
            # let the auto-responder kick-in
            for _ in range(NB_MAX_AUTO_PLAY_RETRIES):
                if is_resp_in_prop_result(resp_worker_id, job_id, treatment):
                    break
                time.sleep(AUTO_PROP_DELAY)
            assert is_resp_in_prop_result(resp_worker_id, job_id, treatment)
示例#4
0
def test_index(client,
               treatment,
               prefix="",
               response_available=False,
               resp_feedback_fields=None):
    client = None
    job_id = "test"
    for _ in range(TASK_REPETITION):
        client = get_client()
        if not response_available:
            resp_worker_id = generate_worker_id(f"{prefix}index_resp")
            path = f"/{treatment}/?worker_id={resp_worker_id}"
            with app.test_request_context(path):
                res = client.get(path, follow_redirects=True)
                assert b"RESPONDER" in res.data
                # res = _process_resp_tasks(client, worker_id=worker_id)

                res = _process_resp(client,
                                    treatment,
                                    job_id=job_id,
                                    worker_id=resp_worker_id,
                                    min_offer=get_min_offer(),
                                    resp_feedback_fields=resp_feedback_fields)
                process_tasks(client,
                              job_id=job_id,
                              worker_id=resp_worker_id,
                              bonus_mode="random",
                              url_kwargs={
                                  "auto_finalize": 1,
                                  "treatment": treatment
                              })
                assert b"resp:" in res.data
        prop_worker_id = generate_worker_id(f"{prefix}index_prop")
        time.sleep(WEBHOOK_DELAY)
        path = f"/{treatment}/?worker_id={prop_worker_id}"
        with app.test_request_context(path):
            res = client.get(path, follow_redirects=True)
            # assert b"PROPOSER" in res.data
            res = _process_prop_round(client,
                                      treatment,
                                      worker_id=prop_worker_id,
                                      offer=get_offer(),
                                      response_available=True)
            assert b"prop:" in res.data
示例#5
0
def test_survey_unique(client, treatment):
    """
        test if the submissed is dropped without control on other fields when <drop> is set to "1"
    """
    with app.app_context():
        worker_id = generate_worker_id("survey")
        path = f"/survey/{treatment}/?job_id=test&worker_id={worker_id}"
        with app.test_request_context(path):
            form = MainForm()
            form_data = {key: "" for key in form._fields}
            form_data["drop"] = "1"
            res = client.post(path, data=form_data, follow_redirects=True)
            assert b"dropped" in res.data
        with app.test_request_context(path):
            form = MainForm()
            form_data = {key: "" for key in form._fields}
            form_data["drop"] = "0"
            res = client.post(path, data=form_data, follow_redirects=True)
            assert b"dropped" in res.data
示例#6
0
def test_index_feedback(client,
                        treatment,
                        prefix="",
                        response_available=False,
                        resp_feedback_fields=None):
    client = None
    job_id = "test"
    for _ in range(TASK_REPETITION):
        client = get_client()
        if not response_available:
            resp_worker_id = generate_worker_id(f"{prefix}index_feedback_resp")
            path = f"/{treatment}/?worker_id={resp_worker_id}"
            with app.test_request_context(path):
                res = client.get(path, follow_redirects=True)
                assert b"RESPONDER" in res.data
                # res = _process_resp_tasks(client, worker_id=worker_id)

                res = _process_resp(client,
                                    treatment,
                                    job_id=job_id,
                                    worker_id=resp_worker_id,
                                    min_offer=get_min_offer(),
                                    resp_feedback_fields=resp_feedback_fields)
                assert b"resp:" in res.data
        else:
            prop_worker_id = generate_worker_id(f"{prefix}index_prop")
            path = f"/{treatment}/?worker_id={prop_worker_id}"
            with app.test_request_context(path):
                res = client.get(path, follow_redirects=True)
                res = _process_prop_round(client,
                                          treatment,
                                          worker_id=prop_worker_id,
                                          offer=get_offer(),
                                          response_available=True,
                                          finalize_round=False)
                assert b"prop:" in res.data
示例#7
0
def test_prop_check(client,
                    treatment,
                    response_available=False,
                    resp_feedback_fields=None):
    worker_id = generate_worker_id("prop_check")
    path = f"/{treatment}/prop/?job_id=test&worker_id={worker_id}"
    if not response_available:
        _process_resp_tasks(client,
                            treatment,
                            worker_id=None,
                            min_offer=get_min_offer(),
                            resp_feedback_fields=resp_feedback_fields)
    with app.test_request_context(path):
        client.get(path, follow_redirects=True)
        res = client.get(f"{treatment}/prop/check/?offer={OFFER}",
                         follow_redirects=True).data
        assert b"acceptance_probability" in res
        assert b"best_offer_probability" in res
示例#8
0
def test_survey_no_workerid(client, treatment):
    for _ in range(TASK_REPETITION_LOWER):
        with app.app_context():
            assignment_id = generate_worker_id().upper().replace("_", "")
            path = f"/survey/{treatment}/?assignment_id={assignment_id}&preview=true"
            with app.test_request_context(path):
                client.get(path, follow_redirects=True)
                form = MainForm()
                form_data = {}
                for field, item in form._fields.items():
                    if field in SURVEY_CONTROL_FIELDS:
                        form_data[field] = "correct"
                    elif field == SURVEY_MAIN_TASK_CODE_FIELD:
                        form_data[field] = "resp:"
                    elif field.startswith("code_"):
                        form_data[field] = get_completion_code(field)
                    elif field in SURVEY_CHOICE_FIELDS:
                        form_data[field] = random.choice(item.choices)[0]
                    else:
                        form_data[field] = "abc"
                res = client.post(path, data=form_data, follow_redirects=True)
                print("RESULT", res.data)
示例#9
0
def test_survey_prop(client, treatment):
    with app.app_context():
        worker_id = generate_worker_id("survey")
        assignment_id = worker_id.upper().replace("_", "")
        path = f"/survey/{treatment}/?job_id=test&worker_id={worker_id}&assignment_id={assignment_id}"
        with app.test_request_context(path):
            client.get(path, follow_redirects=True)
            form = MainForm()
            form_data = {}
            for field, item in form._fields.items():
                if field in SURVEY_CONTROL_FIELDS:
                    form_data[field] = "correct"
                elif field == SURVEY_MAIN_TASK_CODE_FIELD:
                    form_data[field] = "prop:"
                elif field.startswith("code_"):
                    form_data[field] = ""
                elif field in SURVEY_CHOICE_FIELDS:
                    form_data[field] = random.choice(item.choices)[0]
                else:
                    form_data[field] = "abc"
            res = client.post(path, data=form_data, follow_redirects=True)
            assert b"Your survey completion code is:" in res.data
            assert b"dropped" not in res.data