def test_should_call_load_survey_result_repo_correct_values(
        load_by_survey_id: MagicMock, sut: DbSaveSurveyResult):
    survey_result = SaveSurveyResultModel(survey_id="any_survey_id",
                                          account_id="any_account_id",
                                          answer="any_answer")
    sut.save(survey_result)
    load_by_survey_id.assert_called_with("any_survey_id")
def test_should_call_save_on_save_survey_result_repo_correct_values(
        mock_save: MagicMock, sut: DbSaveSurveyResult):
    survey_result = SaveSurveyResultModel(survey_id="any_survey_id",
                                          account_id="any_account_id",
                                          answer="any_answer")
    sut.save(survey_result)
    mock_save.assert_called_with(survey_result)
def test_should_raise_exception_if_load_survey_result_repo_raise(
        load_by_survey_id: MagicMock, sut: DbSaveSurveyResult):
    load_by_survey_id.side_effect = Exception("Error on matrix")
    survey_result = SaveSurveyResultModel(survey_id="any_survey_id",
                                          account_id="any_account_id",
                                          answer="any_answer")
    with pytest.raises(Exception) as excinfo:
        assert sut.save(survey_result)
    assert type(excinfo.value) is Exception
Ejemplo n.º 4
0
def test_should_add_a_survey_result_if_its_new(sut: SurveyResultMongoRepo):
    survey = make_survey()
    account = make_account()
    sut.save(
        SaveSurveyResultModel(survey_id=survey.id,
                              account_id=account.id,
                              answer=survey.answers[0].answer))
    survey_result = MOCK_DATABASE["survey_results"].find_one(
        dict(survey_id=ObjectId(survey.id), account_id=ObjectId(account.id)))
    assert survey_result
    assert str(survey_result["survey_id"]) == survey.id
    assert str(survey_result["account_id"]) == account.id
    assert survey_result["answer"] == survey.answers[0].answer
def test_should_return_survey_result_on_success(sut: DbSaveSurveyResult):
    data = SaveSurveyResultModel(survey_id="any_survey_id",
                                 account_id="any_account_id",
                                 answer="any_answer")
    survey_result = sut.save(data)
    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
Ejemplo n.º 6
0
def test_should_calls_save_survey_result_with_values(
    mock_save: MagicMock,
    mock_get_collection: MagicMock,
    sut: SaveSurveyResultController,
):
    mock_get_collection.return_value = mongomock.MongoClient().db.collection
    sut.handle(
        HttpRequest(
            params=dict(survey_id="any_id"),
            body=dict(answer="any_answer"),
            account_id="any_account_id",
        ))
    mock_save.assert_called_with(
        SaveSurveyResultModel(survey_id="any_id",
                              account_id="any_account_id",
                              answer="any_answer"))
Ejemplo n.º 7
0
def test_should_update_a_survey_result_if_its_not_new(
        sut: SurveyResultMongoRepo):
    survey = make_survey()
    account = make_account()
    MOCK_DATABASE["survey_results"].insert_one(
        dict(
            survey_id=ObjectId(survey.id),
            account_id=ObjectId(account.id),
            answer=survey.answers[0].answer,
            date=datetime.utcnow(),
        ))
    survey_result = sut.save(
        SaveSurveyResultModel(survey_id=survey.id,
                              account_id=account.id,
                              answer=survey.answers[1].answer))
    documents = MOCK_DATABASE["survey_results"].find(
        dict(survey_id=ObjectId(survey.id), account_id=ObjectId(account.id)))
    survey_result = list(documents)
    assert survey_result
    assert len(survey_result) == 1
    assert survey_result[0]["answer"] == survey.answers[1].answer
 def handle(self, request: HttpRequest) -> HttpResponse:
     try:
         survey_id = request.params.get("survey_id")
         answer = request.body.get("answer")
         account_id = request.account_id
         survey = self._load_survey_by_id.load_by_id(survey_id)
         if survey:
             if any(answers.answer == answer for answers in survey.answers):
                 data = dict(
                     survey_id=survey_id,
                     account_id=account_id,
                     answer=answer,
                 )
                 survey_result = self._save_survey_result.save(
                     SaveSurveyResultModel(**data))
                 return ok(asdict(survey_result))
             else:
                 return forbidden(InvalidParamError("answer"))
         else:
             return forbidden(InvalidParamError("survey_id"))
     except Exception:
         return server_error(ServerError(), traceback.format_exc())