Exemple #1
0
    def remember(self, target_slack_user_id: str, quote: str):
        """
        Add quote to a NB's memory of a Person with target_slack_user_id, if they exist.
        If they don't exist, look up their info and create a new Person in memory first.

        Args:
            target_slack_user_id: string representing slack user id for Person to insert
                                  quote into.
            quote: string representing content of Quote to store.

        Returns:
            A Result namedtuple.
        """
        if not get_person_by_slack_user_id(target_slack_user_id):
            user_info = self.fetch_user_info(target_slack_user_id)
            name = user_info.get("name")
            first_name = name.split()[0]

            create_person_dto = CreatePersonDTO(
                slack_user_id=target_slack_user_id, first_name=first_name)

            create_person(create_person_dto)

        add_quote_dto = AddQuoteDTO(slack_user_id=target_slack_user_id,
                                    content=quote)

        try:
            add_quote_to_person(add_quote_dto)
        except QuoteAlreadyExistsException:
            return self.Result(ok=True, message="This quote already exists.")

        return self.Result(ok=True, message="Memory stored!")
Exemple #2
0
def test_add_quote_to_person(client, session):
    person = mixer.blend(Person)

    assert len(person.quotes) == 0

    content = "My face always looks like such a tomato."
    data = AddQuoteDTO(person.slack_user_id, content)

    add_quote_to_person(data)

    assert len(person.quotes) == 1
    assert person.has_said(content)
Exemple #3
0
def test_quote_service_raises_exception_if_quote_content_already_exists(
        client, session):
    assert Person.query.count() == 0

    # Note: mixer will also create the pre-requisite Person when making this Quote
    quote = mixer.blend(Quote)

    # Since there were no people before, the person created should have id == 1
    person = Person.query.get(1)

    assert person.has_said(quote.content)

    with pytest.raises(QuoteAlreadyExistsException):
        data = AddQuoteDTO(person.slack_user_id, quote.content)
        add_quote_to_person(data)
Exemple #4
0
    def post(self):
        parsed_args = self.parser.parse_args()
        slack_user_id = parsed_args.get("slack_user_id")
        content = parsed_args.get("content")

        target_person = Person.query.filter(Person.slack_user_id == slack_user_id).one_or_none()

        if not target_person:
            return abort(
                404,
                message=self.ERRORS.get("person_does_not_exist").format(
                    slack_user_id=slack_user_id
                ),
            )

        if target_person.has_said(parsed_args.get("content")):
            return abort(
                409,
                message=self.ERRORS.get("already_exists").format(slack_user_id=slack_user_id),
            )

        data = AddQuoteDTO(slack_user_id, content)
        new_quote = add_quote_to_person(data)

        return marshal(new_quote, self.fields), 201
Exemple #5
0
def test_quote_service_raises_exception_if_required_fields_are_empty(
        client, session):
    data = AddQuoteDTO(None, None)

    with pytest.raises(EmptyRequiredFieldException):
        add_quote_to_person(data)
Exemple #6
0
def test_quote_service_raises_exception_if_person_does_not_exist(
        client, session):
    data = AddQuoteDTO("nonexistent", "Should not be saved!")

    with pytest.raises(PersonDoesNotExistException):
        add_quote_to_person(data)