def test_get_paging_0(self): '''paging番号が0またはそれ以下の場合,強制的に1のpagingを返却 ''' self.load_fixtures() thread = Thread() test_data = self.filter_test_data( table='thread', field='thread_id', target=[1, 2, 5, 6, 7, 8, 9, 10, 11, 12]) # paging = 0 actual = thread.get_all_by_c_id(category_id=1, sort_id=ID_ASC, paging=0) expect = test_data self.assertListEqual(expect, actual) # paging = -1 actual = thread.get_all_by_c_id(category_id=1, sort_id=ID_ASC, paging=0) self.assertListEqual(expect, actual)
def test_delete_no_thread(self): '''存在しないthread削除 ''' self.load_fixtures() thread = Thread() with self.assertRaises(Exception) as e: thread.delete(100) self.assertEqual('thread not found', str(e.exception))
def test_get_all(self): '''すべてのthread取得 ''' self.load_fixtures() thread = Thread() actual = thread.get_all() expect = self.test_data.get('thread') self.assertListEqual(expect, actual)
def test_get_by_c_id_no_thread(self): '''category_idに紐づくthreadが存在しない場合 ''' self.load_fixtures() thread = Thread() actual = thread.get_all_by_c_id(category_id=100, sort_id=ID_ASC, paging=1) self.assertListEqual([], actual)
def test_delete(self): '''thread削除 ''' self.load_fixtures() thread = Thread() thread.delete(1) actual = thread.get(1) self.assertEqual(None, actual)
def get_all_by_c_id(): '''category_idに紐づけられたthreadリストの取得 Args: QueryString: category_id: カテゴリID sort_id: ソートID paging: ページング番号 Returns: 200: list(dict): threads情報(dict)のリスト 400: パラメータ不正 500: サーバエラー ページング番号 1の時は1~10,2の時11~20のthreadを取得 ''' try: params = parse_params(request.args) category_id = int(params.get('category_id')) sort_id = int(params.get('sort_id')) paging = int(params.get('paging')) if not category_id or not sort_id or not paging: return make_response('', 400) result = Thread.get_all_by_c_id( category_id, sort_id, paging ) return jsonify(result) except Exception as e: return make_response('', 500)
def post_comment(token_data): '''comment投稿 Args: text: コメントテキスト thread_id: スレッドID Returns: 200: 正常終了 list(dict): comment情報のリスト 500: サーバエラー ''' try: params = request.json user_id = token_data.get('user_id') user = User.get(user_id=user_id) params.update({'user_id': user_id, 'name': user.get('nick_name')}) # comment作成 Comment.post(params) # comment追加後,thread_idに紐づく thread = Thread.get(thread_id=params.get('thread_id')) result = thread.get('comments') return make_response(jsonify(result), 201) except Exception as e: if str(e) == 'over text length': result = {'error_message': 'テキストが長すぎます'} return make_response(jsonify(result), 400) return make_response('', 500)
def post(): if request.method == "POST": pub_date = datetime.now() title = request.form["title"] password = request.form["password"] content = request.form["content"] if title == "" or password == "" or content == "": q = randint(1, 99999) output = Thread.query.order_by(Thread.id.desc()).all() title = request.form["title"] content = request.form["content"] return render_template("index.html", output=output, title=title, content=content, error="空欄を埋めてください", q=q) note = Thread(pub_date=pub_date, title=title, password=password, content=content) db.session.add(note) db.session.commit() return redirect(url_for("index")) return redirect(url_for("index"))
def test_get_sort_speed_desc_2(self): '''人気高い順2 ''' self.load_fixtures() thread = Thread() test_data = self.filter_test_data(table='thread', field='thread_id', target=[4, 3]) actual = thread.get_all_by_c_id(category_id=2, sort_id=SPEED_DESC, paging=1) expect = test_data self.assertListEqual(expect, actual)
def test_get_paging_3(self): '''ページング21-30 ''' self.load_fixtures() thread = Thread() test_data = self.filter_test_data(table='thread', field='thread_id', target=[23]) actual = thread.get_all_by_c_id(category_id=1, sort_id=ID_ASC, paging=3) expect = test_data self.assertListEqual(expect, actual)
def test_get_paging_outside(self): '''paging番号による取得できるthread数が0の場合,最後のpagingを表示する ''' self.load_fixtures() thread = Thread() test_data = self.filter_test_data(table='thread', field='thread_id', target=[23]) actual = thread.get_all_by_c_id(category_id=1, sort_id=ID_ASC, paging=100) expect = test_data self.assertListEqual(expect, actual)
def test_get_paging_2(self): '''ページング11-20 ''' self.load_fixtures() thread = Thread() test_data = self.filter_test_data( table='thread', field='thread_id', target=[13, 14, 15, 16, 17, 18, 19, 20, 21, 22]) actual = thread.get_all_by_c_id(category_id=1, sort_id=ID_ASC, paging=2) expect = test_data self.assertListEqual(expect, actual)
def test_get_paging_1(self): '''ページング1-10 ''' self.load_fixtures() thread = Thread() test_data = self.filter_test_data( table='thread', field='thread_id', target=[1, 2, 5, 6, 7, 8, 9, 10, 11, 12]) actual = thread.get_all_by_c_id(category_id=1, sort_id=ID_ASC, paging=1) expect = test_data self.assertListEqual(expect, actual)
def test_get_sort_num_comment_asc(self): '''コメント数少ない順 ''' self.load_fixtures() thread = Thread() test_data = self.filter_test_data( table='thread', field='thread_id', target=[1, 2, 23, 14, 7, 8, 9, 10, 11, 12]) actual = thread.get_all_by_c_id(category_id=1, sort_id=SPEED_ASC, paging=1) expect = test_data self.assertListEqual(expect, actual)
def test_get_sort_id_desc(self): '''ID降順 ''' self.load_fixtures() thread = Thread() test_data = self.filter_test_data(table='thread', field='category_id', target=1, paging=1, reverse=True) actual = thread.get_all_by_c_id(category_id=1, sort_id=ID_DESC, paging=1) expect = test_data self.assertListEqual(expect, actual)
def delete(thread_id): '''threadを削除 同時に,thread_idに紐づくcommentテーブルも削除する Args: thread_id: スレッドID Returns: 200: 正常削除 400: threadが存在しない 500: サーバエラー ''' try: Thread.delete(thread_id) return make_response('', 200) except Exception as e: if str(e) == 'thread not found': return make_response('', 400) return make_response('', 500)
def post(token_data): '''threadを作成 Args: title: スレッドタイトル category_id: カテゴリID comment_text: 1コメ Returns: dict: 作成されたthread情報 ''' try: params = request.json new_thread = Thread.post( title=params.get('title'), category_id=params.get('category_id') ) user_id = token_data.get('user_id') user = User.get(user_id) data = { 'name': user.get('nick_name'), 'thread_id': new_thread.get('thread_id'), 'text': params.get('comment_text'), 'user_id': user_id, } Comment.post(data) result = Thread.get(new_thread.get('thread_id')) return jsonify(result) except Exception as e: if str(e) == 'over title length': result = { 'error_message': 'タイトルが長すぎます' } return make_response(jsonify(result), 400) return make_response('', 500)
def test_get(self): '''thread_idに紐づくthread取得 ''' self.load_fixtures() thread = Thread() actual = thread.get(1) expect = { 'thread': self.filter_test_data(table='thread', field='thread_id', target=[1])[0], 'comments': self.filter_test_data(table='comment', field='thread_id', target=[1]) } self.assertDictEqual(expect, actual)
def get_by_title(title): '''titleからthread情報取得 Args: 200: list(dict): threads情報(dict)のリスト 400: パラメータ不正 500: サーバエラー ''' try: result = Thread.get_by_title(title=title) return jsonify(result) except Exception as e: return make_response('', 500)
def test_post(self): '''新しいthread作成 ''' self.load_fixtures() thread = Thread() actual = thread.post(title='title24', category_id=1, params={ 'create_at': datetime.now(JST), 'update_at': datetime.now(JST), }) expect = { 'thread_id': 24, 'title': 'title24', 'create_at': datetime(2018, 1, 24, 0, 0), 'update_at': datetime(2018, 1, 24, 0, 0), 'speed': 0, 'comment_count': 0, 'category_id': 1, } self.assertDictEqual(expect, actual)
def get(thread_id): '''thread_idからthread情報取得 Args: thread_id: スレッドID Returns: 200: dict: thread: thread情報 comments: list(dict) comment情報のリスト 400: threadが存在しない場合 500: サーバエラnー ''' try: result = Thread.get(thread_id) if not result: return make_response('', 400) return jsonify(result) except Exception as e: return make_response('', 500)
def update_thread(cls, session, thread_id): data = Thread(thread_id=thread_id, comment_count=(Thread.comment_count + 1), update_at=datetime.now(JST)) session.merge(data)