示例#1
0
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]
示例#2
0
def test_quiz_factory_constructor(rootdir, init_db, client):
    """Test that quiz factory creates the correct quiz objects"""

    test_file = os.path.join(rootdir, 'test_files/gender-profession.json')
    factory = QuizFactory(test_file)
    assert 'gender_profession' in factory.data
    assert 'social_profession' in factory.data
    assert 'hobby_profession' in factory.data
    assert 'eat' in factory.data
    assert 'video' in factory.data
    assert 'demographics' in factory.data
示例#3
0
 def get():
     """
     On a get request that uses this resource it returns
     a quiz for the data dissemination application.
     :return: The quiz to be completed
     """
     try:
         filename = os.path.join(current_app.static_folder,
                                 "IATs/{}.json".format("dissemination"))
         return QuizFactory(filename).create_dissemination_quiz(), 200
     except:
         return ANSWERS[404], 404
示例#4
0
    def get(self):
        """
        On a get request on the /random-quiz endpoint we return a random quiz with questions
        :return: random quiz and status 200
        """

        scenario = random.choice(list(Version))
        try:
            filename = os.path.join(current_app.static_folder,
                                    "IATs/{}.json".format(scenario.value))
            return QuizFactory(filename).create_collection_quiz(), 200
        except:
            return ANSWERS[404], 404
示例#5
0
    def get(self):
        """
        On a get request on the /demo endpoint we return a demo quiz with questions
        :return: demo quiz and status 200
        """

        try:
            filename = os.path.join(current_app.static_folder,
                                    "IATs/{}.json".format("demo"))
            return QuizFactory(filename).create_demo_quiz(), 200
        except:
            traceback.print_exc()
            return ANSWERS[404], 404
示例#6
0
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()
示例#7
0
    def get(self):
        """
        On a get request on the /quiz endpoint we return a quiz with questions
        :return: quiz and status 200
        """

        version = request.args.get("version")
        try:
            filename = os.path.join(
                current_app.static_folder,
                "IATs/{}.json".format(Version[version].value))
            return QuizFactory(filename).create_collection_quiz(), 200
        except:
            traceback.print_exc()
            return ANSWERS[404], 404