Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

「現段階の応募人数」をクラス別に表示 #224

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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")

Expand All @@ -92,6 +94,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()
Expand Down
4 changes: 4 additions & 0 deletions api/spec/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions test/test_lottery.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ def test_get_specific_lottery(client):
assert resp.get_json() == lottery


def test_lottery_application_num(client):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このテストは実質schemaのテストだからtest/schemas.pyに入れたほうがいいと思う

"""test the correct number of applications is returned fron the API
target_url: /lotteries/<id>
"""
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/<id>
Expand Down