def test_when_3_questions_requested_then_response_created(news_list):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz', {'num_of_questions': 3})
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_201_CREATED
    assert len(str(response.data['id'])) > 0
    assert len(response.data['questions']) == 3
def test_when_default_then_response_created(news_list):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz')
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_201_CREATED
    assert len(str(response.data['id'])) > 0
    assert len(response.data['questions']
               ) == settings.FZW_DEFAULT_NUM_OF_QUIZ_QUESTIONS  # noqa: E501
def test_when_no_news_in_db_then_response_created(topic_categories,
                                                  manipulation_categories):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz')
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_201_CREATED
    assert len(str(response.data['id'])) > 0
    assert len(response.data['questions']) == 0
def test_when_topic_category_requested_then_response_created(news_list):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz', {
        'topic_category_name': 'politics',
    })
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_201_CREATED
    assert {str(q['news_id'])
            for q in response.data['questions']}.issubset({
                str(news.id)
                for news in news_list if news.topic_category.name == 'politics'
            })
def test_when_too_many_questions_requested_then_response_created(news_list):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz', {
        'num_of_questions': len(news_list) + 1,
    })
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_201_CREATED
    assert len(str(response.data['id'])) > 0
    assert len(response.data['questions']) == len(news_list)
    assert {str(q['news_id'])
            for q in response.data['questions']
            } == {str(news.id)
                  for news in news_list}
def test_created_when_lang_selected(news_list, news_list_en, input_lang,
                                    expected_lang):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz', {'language': input_lang})
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_201_CREATED
    assert len(str(response.data['id'])) > 0
    assert len(response.data['questions']
               ) == settings.FZW_DEFAULT_NUM_OF_QUIZ_QUESTIONS  # noqa: E501
    for question in response.data['questions']:
        assert len(str(question['news_id'])) > 0
        news = News.objects.get(id=question['news_id'])
        assert news.language == str(expected_lang)
def test_when_one_true_news_then_response_created(
        true_news_without_manipulation):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz')
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_201_CREATED
    assert len(str(response.data['id'])) > 0
    assert len(response.data['questions']) == 1
    question_data = response.data['questions'][0]
    assert question_data['source_name'] == ''
    assert question_data['source_url'] == ''
    assert question_data['analysis_name'] == ''
    assert question_data['analysis_url'] == ''
    assert question_data['answer_explanation_html'] == (
        '<p>Ten news jest prawdziwy</p>')
def test_created_when_lang_and_topic_category_selected(news_list,
                                                       news_list_en):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz', {
        'language': 'en',
        'topic_category_name': 'politics',
    })
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_201_CREATED
    assert len(str(response.data['id'])) > 0
    for question in response.data['questions']:
        assert len(str(question['news_id'])) > 0
        news = News.objects.get(id=question['news_id'])
        assert news.language == str(Language.ENGLISH)
        assert news.topic_category.name == 'politics'
def test_when_one_fake_news_then_response_created(news):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz')
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_201_CREATED
    assert len(str(response.data['id'])) > 0
    assert len(response.data['questions']) == 1
    question_data = response.data['questions'][0]
    assert question_data['source_name'] == ''
    assert question_data['source_url'] == ''
    assert question_data['analysis_name'] == ''
    assert question_data['analysis_url'] == ''
    assert question_data['answer_explanation_html'] == '\n'.join([
        '<p>Ten news to manipulacja obrazem.</p>',
        '<ul>',
        '<li>Uważaj na nie tylko treść newsa ale także na obrazki</li>',
        '<li>Wizualny montaż jest niezwykle skuteczny</li>',
        '</ul>',
    ])
def test_when_negative_questions_requested_then_bad_response(news_list):
    factory = APIRequestFactory()
    request = factory.post('/api/v1/quiz', {'num_of_questions': -1})
    response = generate_quiz(request)
    assert response.status_code == status.HTTP_400_BAD_REQUEST