diff --git a/api/schemas.py b/api/schemas.py index 7e395b3..829e8b8 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -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") @@ -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() 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 b6521d9..4eceada 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/