def test_quiz_answers_participant_no_gender_no_ethnicity(init_db, client): """ Tests that if the participant decides to not answer questions regarding gender or ethnicity, nothing is saved in the database. :param init_db: Initializes the database :param client: Testing client for requests :return: Nothing """ populate() form_response = client.post('/form', json=consent_data) assert form_response.status_code == 200 login_response = client.post("/login", data=dict(username='******', password='******')) assert login_response.status_code == 200 token = login_response.get_json()['access_token'] assert Participant.query.filter_by(id=1).first() client.post('/answers', json=GENDER_ETHNICITY_NOANSWER, headers={'Authorization': 'Bearer ' + token}) participant = Participant.query.filter_by(id=1).first() assert participant.age is None assert participant.gender is None
def test_quiz_factory_iat(rootdir, init_db, client): """ Populates the database and creates a gender IAT Tests that the response has the right format """ populate() test_file = os.path.join(rootdir, 'test_files/gender-profession.json') factory = QuizFactory(test_file) # No binary questions in the database before the create binary_questions = Question.query.filter_by( q_type=QuestionType.binary).all() assert len(binary_questions) == 0 # After the create the question should be inserted in the database response = factory.gender_profession.create_iat() binary_questions = Question.query.filter_by( q_type=QuestionType.binary).all() assert len(binary_questions) == 1 # The response should have the following format assert response[0]['q_type'] == QuestionType.binary_information.value for i in range(1, len(response) - 1): assert response[i]['q_type'] == QuestionType.binary.value assert 'categories_left' in response[i] assert 'categories_right' in response[i] assert 'image' in response[i]
def test_quiz_results(init_db, client): """ Tests that a request for available results returns two arrays: A array with the names of the columns for an excel spreadsheet A array of rows for the respective columns. :param init_db: Initializes the database :param client: Testing client for requests :return: Nothing """ populate() response = client.post('/form', json=consent_data) assert response.status_code == 200 response = client.post("/login", data=dict(username='******', password='******')) assert response.status_code == 200 token = response.get_json()['access_token'] response = client.post('/answers', json=answer, headers={'Authorization': 'Bearer ' + token}) assert response.status_code == 201 response = client.get('/results', headers={'Authorization': 'Bearer ' + token}) assert response.status_code == 200 print(response.get_json()) assert response.get_json()['columns'] == COLUMNS_RESULTS name = "{} {}".format(consent_data['children'][0]['firstName'], consent_data['children'][0]['lastName']) assert response.get_json()['data'][0][0] == name q_id = answer['data'][4]['question_id'] assert int(response.get_json()['data'][0][1]) == q_id question = Question.query.filter_by(id=q_id).first() q_type = question.q_type.value q_text = question.text assert response.get_json()['data'][0][2] == q_type assert response.get_json()['data'][0][3] == q_text q_answer = answer['data'][4]['answers'] assert int(response.get_json()['data'][0][4]) == q_answer img_link = None assert response.get_json()['data'][0][5] == img_link response_time = None assert response.get_json()['data'][0][6] == response_time before_video = answer['data'][4]['before_video'] assert response.get_json()['data'][0][7] == before_video
def test_get_dissemination_quiz(client, init_db): """ Tests that a request for a dissemination quiz returns a list of questions :param client: Testing client for requests :param init_db: Initializes database :return: Nothing """ populate() response = client.get('/iat') assert response.status_code == 200 assert isinstance(response.get_json(), list)
def make_quiz(): """ Populates the database and creates a gender-profession IAT """ db.drop_all() populate() root = os.path.dirname(os.path.abspath(__file__)) test_file = os.path.join(root, 'test_files/intervention-hobby-female.json') QuizFactory(test_file).create_collection_quiz() question_3 = Question.query.filter_by(id=24).first() question_5 = Question.query.filter_by(id=26).first() yield question_3, question_5 db.session.close() db.drop_all()
def test_get_random_quiz(init_db, client): """ Tests that a request for a random quiz returns a list of questions :param init_db: Initializes the database :param client: Testing client for requests :return: Nothing """ populate() response = client.post("/login", data=dict(username='******', password='******')) assert response.status_code == 200 token = response.get_json()['access_token'] response = client.get('/random-quiz', headers={'Authorization': 'Bearer ' + token}) assert response.status_code == 200 assert isinstance(response.get_json(), list)
def test_get_nonexistent_quiz_version(init_db, client): """ Tests that a request that asks for a non-existing versions returns a 404 Not found message. :param init_db: Initializes the database :param client: Testing client for requests :return: Nothing """ populate() response = client.post("/login", data=dict(username='******', password='******')) assert response.status_code == 200 token = response.get_json()['access_token'] version = 'nonexistent' response = client.get('/quiz?version={}'.format(version), headers={'Authorization': 'Bearer ' + token}) assert response.status_code == 404
def test_get_quiz_version(init_db, client): """ Tests that when requesting a quiz version a list of questions is returned :param init_db: Initializes the database :param client: Testing client for requests :return: Nothing """ populate() response = client.post("/login", data=dict(username='******', password='******')) assert response.status_code == 200 token = response.get_json()['access_token'] version = 'A' response = client.get('/quiz?version={}'.format(version), headers={'Authorization': 'Bearer ' + token}) assert response.status_code == 200 assert isinstance(response.get_json(), list)
def test_quiz_answers_participant_info(init_db, client): """ Tests that the participant, gender and age of a participant are changed after answer to respective questions are submitted. :param init_db: Initializes the database :param client: Testing client for requests :return: Nothing """ populate() form_response = client.post('/form', json=consent_data) assert form_response.status_code == 200 login_response = client.post("/login", data=dict(username='******', password='******')) assert login_response.status_code == 200 token = login_response.get_json()['access_token'] assert Participant.query.filter_by(id=1).first() client.post('/answers', json=answer, headers={'Authorization': 'Bearer ' + token}) participant = Participant.query.filter_by(id=1).first() assert participant.age == \ int(QuestionChoice.query.filter_by(question_id=answer['data'][0]["question_id"], choice_num=answer['data'][0]['answers']).first().text) ethinicities = [] for choice_num in answer['data'][1]['answers']: eth = QuestionChoice.query.filter_by( choice_num=choice_num, question_id=answer['data'][1]["question_id"]).first().text ethinicities.append(eth) assert participant.ethnicity == ethinicities assert participant.gender == \ QuestionChoice.query.filter_by(question_id=answer['data'][2]["question_id"], choice_num=answer['data'][2]['answers']).first().text assert participant.quiz_version == Version[answer['version']]
def test_calculate_result_no_bias(client, make_quiz): """ Tests that same response times in blocks 3 and 5 => no association :param client: the client that simulates the request :param make_quiz: fixture that creates the quiz :return: nothing """ populate() response_range = range(100, 101) question3, question5 = make_quiz data = {} data['data'] = generate_answers(question3, question5, response_range, response_range) data['email'] = '*****@*****.**' response = client.post('/calculate', json=data) assert response.status_code == 200 assert response.get_json() == DISSEMINATION_NO_ASSOCIATION
def test_quiz_answers_non_participant_info(init_db, client): """ Tests that answers to questions that don't have any relation to the participant info are saved in the database :param init_db: Initializes the database :param client: Testing client for requests :return: Nothing """ populate() form_response = client.post('/form', json=consent_data) assert form_response.status_code == 200 login_response = client.post("/login", data=dict(username='******', password='******')) assert login_response.status_code == 200 token = login_response.get_json()['access_token'] assert Participant.query.filter_by(id=1).first() client.post('/answers', json=answer, headers={'Authorization': 'Bearer ' + token}) participant = Participant.query.filter_by(id=1).first() assert participant.answers