def test_should_call_load_survey_by_id_repo_if_load_survey_result_repo_returns_none(
        load_by_survey_id: MagicMock, load_by_id: MagicMock,
        sut: DbLoadSurveyResult):
    load_by_survey_id.return_value = None
    load_by_id.return_value = make_fake_survey()
    sut.load("any_survey_id")
    load_by_id.assert_called_with("any_survey_id")
def make_load_survey_result_controller():
    survey_collection = get_collection_surveys("surveys")
    survey_result_collection = get_collection_survey_results("survey_results")
    load_survey_result_repo = SurveyResultMongoRepo(
        survey_collection, survey_result_collection
    )
    load_survey_by_id = SurveyMongoRepo(survey_collection)
    db_save_survey_result = DbLoadSurveyResult(
        load_survey_result_repo, load_survey_by_id
    )
    return LoadSurveyResultController(load_survey_by_id, db_save_survey_result)
def test_should_return_survey_result_on_success(sut: DbLoadSurveyResult):
    survey_result = sut.load("any_survey_id")
    expected = SurveyResultModel(
        survey_id="any_survey_id",
        question="any_question",
        answers=[
            dict(answer="any_answer", count=1, percent=40, image="any_image"),
            dict(answer="other_answer", count=2, percent=60,
                 image="any_image"),
        ],
        date=datetime.utcnow(),
    )
    assert survey_result == expected
def test_should_return_survey_result_answers_count_0_if_load_survey_result_repo_returns_none(
        load_by_survey_id: MagicMock, sut: DbLoadSurveyResult):
    load_by_survey_id.return_value = None
    survey_result = sut.load("any_survey_id")
    expected = SurveyResultModel(
        survey_id="any_survey_id",
        question="any_question",
        answers=[
            dict(answer="any_answer", count=0, percent=0, image="any_image"),
            dict(answer="other_answer", count=0, percent=0, image="any_image"),
        ],
        date=datetime.utcnow(),
    )
    assert survey_result == expected
def test_should_raise_exception_if_load_survey_result_repo_raise(
        load_by_survey_id: MagicMock, sut: DbLoadSurveyResult):
    load_by_survey_id.side_effect = Exception("Error on matrix")
    with pytest.raises(Exception) as excinfo:
        assert sut.load("any_survey_id")
    assert type(excinfo.value) is Exception
def test_should_call_load_by_survey_id_on_load_survey_result_repo_correct_values(
        load_by_survey_id: MagicMock, sut: DbLoadSurveyResult):
    sut.load("any_survey_id")
    load_by_survey_id.assert_called_with("any_survey_id")
def sut():
    load_survey_result_repo_stub = LoadSurveyResultRepoStub()
    load_survey_by_id_repo_stub = LoadSurveyByIdRepoStub()
    yield DbLoadSurveyResult(load_survey_result_repo_stub,
                             load_survey_by_id_repo_stub)