def add_new_todo(): """Todo を新規追加する DynamoDBTodo.add_item をコールします リクエストの json_body に含まれているパラメータをバリデーションし、 DynamoDB テーブルに特定のユーザーの Todo オブジェクトを追加するため、 DynamoDBTodo.add_item をコールします。 Raises: BadRequestError: json_body が存在しないケースで例外が発生します。 バリデーションに失敗したケースで例外が発生します。 Return: str: 登録に成功した Todo の uid を返します """ body = app.current_request.json_body if body is None: raise BadRequestError('current_request.json_body is None.') subject = body.get('subject') description = body.get('description') username = get_authorized_username(app.current_request) Validates.subject(subject) Validates.description(description) if description is not None else None Validates.username(username) return get_app_db().add_item( subject=subject, description=description, username=username )
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 )
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
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_Raise_BadRequestError_case_boundery_length(self, bad_length_description): """description: 文字の長さが境界値を超過しているケース、例外を発生させることができる""" with pytest.raises(BadRequestError): Validates.description(bad_length_description)
def test_Pass_case_boundery_legth(self): """description: 文字の長さが境界値の限界 のケース、例外をパスできる""" assert Validates.description('@'*Validates.DESCRIPTION_MAX_LEN) == None
def test_Raise_BadRequestError_case_bad_type(self, bad_type_description): """description: str以外の型 のケース、例外を発生させることができる""" with pytest.raises(BadRequestError): Validates.description(bad_type_description)
def test_Pass_case_None(self): """description: None のケース、例外をパスできる""" assert Validates.description(None) == None
def test_Pass_case_normal(self, description): """description: 通常 のケース、例外をパスできる""" assert Validates.description(description) == None