-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FY2024 資料更新: テストプログラミング ハンズオン (#166)
* 余計な末尾スペースの削除 * 不要な改行を削除 <br/> がなくても整形されていて十分可読性があるため、削除してみた。 * 「概論」、「準備」について加筆。 講義の約束事、その他自然な日本語になるよう修正した。 * 「同値クラス、境界値テスト」について加筆。 サンプルコードも手元で動かせるように修正。その他、細かな文言などの修正。 * 「APIと関数のモック」について加筆。 サンプルコードも手元で動かせるように修正。その他、パスなど細かい部分の修正。 * 「TDDをやってみる」について加筆。その他、細かな修正 (表現の統一など) * fastapi テストクライアント用に、requests から httpx に変更 * 資料修正に合わせてサンプルコードを修正。 * 指摘事項取り込み (#166 (comment))
- Loading branch information
Showing
12 changed files
with
758 additions
and
499 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 18 additions & 16 deletions
34
src/server-app/test-hands-on/exercises/answer3/challenge.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/server-app/test-hands-on/exercises/answer3/test_challenge.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import unittest | ||
from fastapi.testclient import TestClient | ||
from . import challenge | ||
|
||
client = TestClient(challenge.app) | ||
|
||
|
||
class ApiTestCase(unittest.TestCase): | ||
def test_success(self): | ||
res = client.get("/") | ||
self.assertEqual(res.json(), {"current_number": 0}) | ||
res = client.get("/add/123") | ||
self.assertEqual(res.json(), {"current_number": 123}) | ||
res = client.get("/sub/13") | ||
self.assertEqual(res.json(), {"current_number": 110}) | ||
res = client.get("/mul/5") | ||
self.assertEqual(res.json(), {"current_number": 550}) | ||
res = client.get("/div/275") | ||
self.assertEqual(res.json(), {"current_number": 2}) | ||
|
||
client.get("/sub/2") | ||
|
||
def test_tdd(self): | ||
res = client.get("/") | ||
data = res.json() | ||
self.assertEqual(data, {"current_number": 0}) | ||
|
||
res = client.get("/add/10") | ||
data = res.json() | ||
self.assertEqual(data, {"current_number": 10}) | ||
|
||
res = client.get("/sub/5") | ||
data = res.json() | ||
self.assertEqual(data, {"current_number": 5}) | ||
|
||
res = client.get("/mul/3") | ||
data = res.json() | ||
self.assertEqual(data, {"current_number": 15}) | ||
|
||
res = client.get("/div/5") | ||
data = res.json() | ||
self.assertEqual(data, {"current_number": 3}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
- [x] 現在の値を返すエンドポイント | ||
- [ ] 現在の値に、パラメータで指定した値を加算した結果を返すエンドポイント | ||
- [ ] 現在の値に、パラメータで指定した値を減算した結果を返すエンドポイント | ||
- [ ] 現在の値に、パラメータで指定した値を乗算した結果を返すエンドポイント | ||
- [ ] 現在の値に、パラメータで指定した値を除算した結果を返すエンドポイント |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 8 additions & 12 deletions
20
src/server-app/test-hands-on/exercises/exercise2/challenge.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,27 @@ | ||
from fastapi import FastAPI | ||
import random | ||
|
||
|
||
# コンソールから実行は以下のコマンド | ||
# $ python3 -m uvicorn challenge:app --reload | ||
app = FastAPI() | ||
|
||
|
||
@app.get("/") | ||
async def get_index(): | ||
def get_index(): | ||
return {"message": "hello world"} | ||
|
||
|
||
@app.get("/echo/{data}") | ||
async def get_echo(data: str): | ||
return { | ||
"message": "got the message: {0}".format(data) | ||
} | ||
def get_echo(data: str): | ||
return {"message": "got the message: {0}".format(data)} | ||
|
||
|
||
# 100分の1で当たるガチャ関数 | ||
def _exec_gacha(): | ||
return random.randrange(0, 100) == 0 | ||
|
||
|
||
@app.get("/gacha") | ||
async def get_gacha(): | ||
def get_gacha(): | ||
message = "you lose" | ||
if _exec_gacha(): | ||
message = "you win" | ||
return { | ||
"message": message | ||
} | ||
return {"message": message} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 11 additions & 14 deletions
25
src/server-app/test-hands-on/exercises/exercise3/challenge.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,26 @@ | ||
from fastapi import FastAPI | ||
|
||
################################################################ | ||
# 今回の講義ではプログラムの視認性を上げるため、セッションを使用せずに | ||
# サーバの値を保持していますが、実際のプロダクトでこのようにユーザーを | ||
# 識別せずに値を保持すると、セキュリティ・インシデントになるので | ||
# 気をつけてください | ||
################################################################ | ||
############################################################################ | ||
# 今回の講義ではプログラムの視認性を上げるため、セッションを使用せず値を保持しています。 | ||
# 実際のプロダクトコードでこのようにするとセキュリティ事故になり得るので気をつけてください。 | ||
# (任意のユーザが読み書きできる値をサーバが保持しており、情報漏洩に繋がる可能性があるため) | ||
############################################################################ | ||
|
||
current_number = 0 | ||
app = FastAPI() | ||
|
||
|
||
def get_current_number(): | ||
return current_number | ||
|
||
|
||
def set_current_number(number: int): | ||
global current_number | ||
current_number = int(number) | ||
|
||
################################################################ | ||
|
||
# コンソールから実行は以下のコマンド | ||
# $ python3 -m uvicorn challenge:app --reload | ||
app = FastAPI() | ||
|
||
@app.get("/") | ||
async def get_index(): | ||
def get_index(): | ||
return {"current_number": get_current_number()} | ||
|
||
# 以下に、テスト駆動開発を使用して、加減乗除を行うAPIのエンドポイントを | ||
# 作成していこう | ||
# 以下に、TDD を使って、加減乗除を行うAPIのエンドポイントを作成していきましょう |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
fastapi | ||
uvicorn[standard] | ||
requests | ||
httpx |