Exemplo n.º 1
0
def test_match_questions(session):
    '''
    1. 새로운 유저 생성
    2. 새로운 질문 2개 생성
    3. 질문의 선택지 (4개) 생성
    4. 새로운 유저가 첫번째 질문에 대답.
    5. 답변 안한 question_id 와 답변 한 question_id 를 모두 q_ids 배열 담아서 user_answer 쿼리
    6. q_ids 배열의 길이와 쿼리 결과 리스트의 길이가 다르면 테스트 성공.
    '''
    user = test_user_creation(session)

    q_1 = test_question_creation(session)
    q_2 = test_question_creation(session)

    a_1 = test_answer_creation(session, question_id=q_1.id)
    a_2 = test_answer_creation(session, question_id=q_1.id)
    a_3 = test_answer_creation(session, question_id=q_2.id)
    a_3 = test_answer_creation(session, question_id=q_2.id)

    u_a_1 = test_user_answer_creation(session,
                                      user_id=user.id,
                                      question_id=q_1.id,
                                      answer_id=a_1.id)

    q_ids = [q_1.id, q_2.id]

    answers = session.query(UserAnswer).filter(
        (UserAnswer.question_id.in_(q_ids))
        & (UserAnswer.user_id == user.id)).all()
    assert len(answers) < len(q_ids)
Exemplo n.º 2
0
def test_user_answer_creation(session, **kwargs):
    '''
    외부로 부터 받을 수 있는 id 목록
    user_id
    question_id
    answer_id
    '''
    user_id = kwargs.get('user_id', None)
    if user_id is None:
        user_id = test_user_creation(session).id

    question_id = kwargs.get('question_id', None)
    if question_id is None:
        question_id = test_question_creation(session).id

    answer_id = kwargs.get('answer_id', None)
    if answer_id is None:
        answer_id = test_answer_creation(session).id

    user_answer = UserAnswer(user_id=user_id,
                             question_id=question_id,
                             answer_id=answer_id)
    session.add(user_answer)
    session.flush()
    return user_answer
Exemplo n.º 3
0
def test_query_time(session):
    print()

    user_1 = test_user_creation(session)
    user_2 = test_user_creation(session)

    questions = [test_question_creation(session) for _ in range(0, 200)]

    for q in questions:
        a_1 = test_answer_creation(session, question_id=q.id)
        test_user_answer_creation(session,
                                  user_id=user_1.id,
                                  question_id=q.id,
                                  answer_id=a_1.id)
        a_2 = test_answer_creation(session, question_id=q.id)
        test_user_answer_creation(session,
                                  user_id=user_2.id,
                                  question_id=q.id,
                                  answer_id=a_2.id)

    # without join
    start = time.time()
    user_answers = session.query(UserAnswer).filter(
        (UserAnswer.user_id == user_1.id)).all()
    [ua.question.json() for ua in user_answers]
    print('without join', time.time() - start)

    # with join
    start = time.time()
    user_answers = session.query(UserAnswer).filter(
        (UserAnswer.user_id == user_1.id)).join(Question).all()
    [ua.question.json() for ua in user_answers]
    print('with join', time.time() - start)

    assert False
Exemplo n.º 4
0
def test_answer_creation(session, **kwargs):
    '''
    테스트에서 answer 를 리턴해야 함.
    호출자 측(클라이언트 코드)에서 question_id 를 넘겨줄 수 있어야 합니다.
    '''
    question_id = kwargs.get('question_id', None)
    if question_id is None:
        question_id = test_question_creation(session).id

    answer = Answer(question_id=question_id)
    session.add(answer)
    session.flush()
    return answer