From 9b61d315fcedd1cf01cc2025edf69a789ff8364c Mon Sep 17 00:00:00 2001 From: tamazasa Date: Sun, 16 Sep 2018 15:22:52 +0900 Subject: [PATCH 1/3] Add: add application_num to schema and test code --- api/schemas.py | 5 +++++ test/test_lottery.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/api/schemas.py b/api/schemas.py index 6e86e67..53847b2 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -74,6 +74,8 @@ class LotterySchema(Schema): index = fields.Int() done = fields.Boolean() name = fields.Method("format_name", dump_only=True) + application_num = fields.Method("get_application_num", + dump_only=True) winners = fields.Method("get_winners", dump_only=True) end_of_drawing = fields.Method("calc_end_of_drawing") @@ -83,6 +85,9 @@ def format_name(self, lottery): index = lottery.index return f"{grade}{name}.{index}" + def get_application_num(self, lottery): + return len(lottery.application) + def get_winners(self, lottery): winners = Application.query.filter_by( lottery_id=lottery.id, status="won").all() diff --git a/test/test_lottery.py b/test/test_lottery.py index 975aca4..54f92a5 100644 --- a/test/test_lottery.py +++ b/test/test_lottery.py @@ -138,6 +138,27 @@ def test_get_specific_lottery(client): assert resp.get_json() == lottery +def test_lottery_application_num(client): + """test the correct number of applications is returned fron the API + target_url: /lotteries/ + """ + idx = 1 + + with client.application.app_context(): + target_lottery = Lottery.query.get(idx) + users = User.query.all() + users_num = len(users) + + apps = (Application(lottery=target_lottery, user=user) + for user in users) + for app in apps: + db.session.add(app) + db.session.commit() + + resp = client.get(f'/lotteries/{idx}') + assert resp.get_json()['application_num'] == users_num + + def test_get_specific_lottery_invalid_id(client): """test proper errpr is returned from the API target_url: /lotteries/ From dcf6acfb26b1e172d1df3f5f3201b667729d6790 Mon Sep 17 00:00:00 2001 From: tamazasa Date: Sun, 16 Sep 2018 15:36:21 +0900 Subject: [PATCH 2/3] Update: update API doc --- api/spec/template.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/spec/template.yml b/api/spec/template.yml index 4b2edff..3cde179 100644 --- a/api/spec/template.yml +++ b/api/spec/template.yml @@ -118,6 +118,10 @@ definitions: description: Display name of the lottery example: 5A.0 type: string + application_num: + description: The number of applications to the lottery + example: 180 + type: integer winners: type: array items: From 6a4c28d8a639d8b2a59108b525eb2abc0596d81d Mon Sep 17 00:00:00 2001 From: tamazasa Date: Sun, 16 Sep 2018 16:23:15 +0900 Subject: [PATCH 3/3] Fix: fix indent error --- api/schemas.py | 5 +++++ api/spec/template.yml | 4 ++++ test/test_lottery.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/api/schemas.py b/api/schemas.py index 6e86e67..53847b2 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -74,6 +74,8 @@ class LotterySchema(Schema): index = fields.Int() done = fields.Boolean() name = fields.Method("format_name", dump_only=True) + application_num = fields.Method("get_application_num", + dump_only=True) winners = fields.Method("get_winners", dump_only=True) end_of_drawing = fields.Method("calc_end_of_drawing") @@ -83,6 +85,9 @@ def format_name(self, lottery): index = lottery.index return f"{grade}{name}.{index}" + def get_application_num(self, lottery): + return len(lottery.application) + def get_winners(self, lottery): winners = Application.query.filter_by( lottery_id=lottery.id, status="won").all() diff --git a/api/spec/template.yml b/api/spec/template.yml index 4b2edff..cb382ef 100644 --- a/api/spec/template.yml +++ b/api/spec/template.yml @@ -118,6 +118,10 @@ definitions: description: Display name of the lottery example: 5A.0 type: string + application_num: + description: The number of applications to the lottery + example: 180 + type: integer winners: type: array items: diff --git a/test/test_lottery.py b/test/test_lottery.py index 975aca4..20d6565 100644 --- a/test/test_lottery.py +++ b/test/test_lottery.py @@ -138,6 +138,25 @@ def test_get_specific_lottery(client): assert resp.get_json() == lottery +def test_lottery_application_num(client): + """test the correct number of applications is returned fron the API + target_url: /lotteries/ + """ + idx = 1 + with client.application.app_context(): + target_lottery = Lottery.query.get(idx) + users = User.query.all() + users_num = len(users) + apps = (Application(lottery=target_lottery, user=user) + for user in users) + for app in apps: + db.session.add(app) + db.session.commit() + + resp = client.get(f'/lotteries/{idx}') + assert resp.get_json()['application_num'] == users_num + + def test_get_specific_lottery_invalid_id(client): """test proper errpr is returned from the API target_url: /lotteries/