示例#1
0
def update_todo(uid):
    """特定の Todo オブジェクトを更新する DynamoDBTodo.update_item をコールします

    リクエストに含まれる json_body に格納されているパラメータをバリデーションします。

    DynamoDB テーブルから特定のユーザー、uid の Todo オブジェクトを更新するため、
    DynamoDBTodo.update_item をコールします。

    Raises:
        BadRequestError:
            json_body が存在しないケースで例外が発生します。
            バリデーションに失敗したケースで例外が発生します。

    Return:
        uid: (str): 正常に更新された Todo の uid を返す

    """
    body = app.current_request.json_body
    if body is None:
        raise BadRequestError("json_body is None.")

    subject = body.get('subject')
    description = body.get('description')
    state = body.get('state')
    username = get_authorized_username(app.current_request)

    Validates.subject(subject) if subject is not None else None
    Validates.description(description) if description is not None else None
    Validates.state(state) if state is not None else None
    Validates.username(username)

    return get_app_db().update_item(
        uid=uid,
        subject=subject,
        description=description,
        state=state,
        username=username
    )
示例#2
0
    def update_item(self, uid, subject=None, description=None,
                    state=None, username=DEFAULT_USERNAME):
        """特定の Todo オブジェクトを更新します

        DynamoDB テーブルから特定のユーザー、uid の Todo オブジェクトを更新します。
        subject, description, state, username キーの値にバリデーションを行います。

        Args:
            subject (str): Todo のタイトルが渡ってきます
            description (str): Todo の説明が渡ってきます
            username (str): Todo のユーザー名が渡ってきます
            state (str): Todo のステータスが渡ってきます

        Raises:
            BadRequestError: バリデーションに失敗したケースで例外が発生します
            NotFoundError: DynamoDB のレスポンスの Attributes の中に uid キーがないケースで例外が発生します

        Return:
            str: 更新に成功した Todo オブジェクトの uid を返します

        """
        Validates.username(username)
        item = self.get_item(uid, username)
        if subject is not None:
            Validates.subject(subject)
            item['subject'] = subject
        if description is not None:
            Validates.description(description)
            item['description'] = description
        if state is not None:
            Validates.state(state)
            item['state'] = state
        response = self._table.put_item(Item=item, ReturnValues='ALL_OLD')
        try:
            res = response['Attributes']['uid']
        except KeyError:
            raise NotFoundError(f"Todo not found. (id: {uid})")
        return res
示例#3
0
    def add_item(self, subject, description='', username=DEFAULT_USERNAME):
        """Todo オブジェクトを新規に追加します

        DynamoDB テーブルに特定のユーザーの Todo オブジェクトを追加します。
        uid は uuid4 で新規に生成します。*1
        state は 初期値に 'unstarted' が設定されます。
        subject, description, state, username キーの値にバリデーションを行います。

        (*1 同一ユーザーの Todo オブジェクトにおける uuid4 の衝突は今回の仕様では考慮しません。)

        Args:
            subject (str): Todo のタイトルが渡ってきます
            description (str): Todo の説明 が渡ってきます 指定がない場合空文字を指定します
            username (str): Todo のユーザー名が渡ってきます

        Raises:
            BadRequestError: バリデーションに失敗したケースで例外が発生します

        Return:
            str: 登録に成功した Todo オブジェクトの uid を返します

        """
        item = {
            'uid': str(uuid4()),
            'subject': subject,
            'description': description,
            'state': 'unstarted',
            'username': username
        }
        Validates.subject(item['subject'])
        Validates.description(item['description'])
        Validates.state(item['state'])
        Validates.username(item['username'])

        self._table.put_item(Item=item, ReturnValues='ALL_OLD')
        return item['uid']
 def test_Pass_case_state_in_enum(self, state):
     """state: Validates.STATE_ENUMに含まれる場合、例外をパスできる"""
     assert Validates.state(state) == None
 def test_Raise_BadRequestError_case_bad_type(self, bad_type_state):
     """state: str以外の型 のケース、例外を発生させることができる"""
     with pytest.raises(BadRequestError):
         Validates.state(bad_type_state)
 def test_Raise_BadRequestError_case_None(self):
     """state: None のケース、例外を発生させることができる"""
     with pytest.raises(BadRequestError):
         Validates.state(None)
 def test_Pass_case_normal(self, state):
     """state: 通常 のケース、例外をパスできる"""
     assert Validates.state(state) == None
 def test_Raise_BadRequestError_case_not_state_in_enum(self, state):
     """state: Validates.STATE_ENUMに含まれない場合、例外を発生させることができる"""
     with pytest.raises(BadRequestError):
         Validates.state(state)