From 1888caf0dd68928bfa8af1a44b5a5e54be319391 Mon Sep 17 00:00:00 2001 From: Eugene Molotov Date: Sat, 6 Apr 2024 20:09:15 +0500 Subject: [PATCH] =?UTF-8?q?matches:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA=20=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D1=87=D0=B5=D0=B9=20=D0=B2=20=D0=B2=D0=B8=D0=B4=D0=B5=20?= =?UTF-8?q?json-=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D0=A0=D0=BE=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qllr/blueprints/matches/__init__.py | 16 +++- tests/samples/match_list_24hour_1.json | 110 +++++++++++++++++++++++++ tests/samples/match_list_24hour_2.json | 11 +++ tests/test_matches.py | 21 +++++ 4 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 tests/samples/match_list_24hour_1.json create mode 100644 tests/samples/match_list_24hour_2.json diff --git a/qllr/blueprints/matches/__init__.py b/qllr/blueprints/matches/__init__.py index 50c3309..63c8d27 100644 --- a/qllr/blueprints/matches/__init__.py +++ b/qllr/blueprints/matches/__init__.py @@ -1,9 +1,11 @@ +from time import time + from asyncpg import Connection from starlette.requests import Request from starlette.responses import JSONResponse from starlette.routing import Route -from qllr.endpoints import Endpoint +from qllr.endpoints import Endpoint, NoCacheEndpoint from qllr.templating import templates from .methods import get_best_matches_of_player, get_last_matches @@ -35,6 +37,17 @@ async def get_document(self, request: Request, con: Connection): return templates.TemplateResponse("match_list.html", context) +class Last24HoursMatchesForRobot(NoCacheEndpoint): + async def get_document(self, request: Request, con: Connection): + gametype = request.path_params["gametype"] + + context = await get_last_matches( + con, gametype, None, 0, int(time() - 60 * 60 * 24), int(time()) + ) + context["gametype"] = gametype + return JSONResponse(context) + + routes = [ Route("/", endpoint=MatchesHtml), Route("/{page:int}/", endpoint=MatchesHtml), @@ -44,5 +57,6 @@ async def get_document(self, request: Request, con: Connection): Route("/player/{steam_id:int}/{gametype}/{page:int}/", endpoint=MatchesHtml), Route("/player/{steam_id:int}/{gametype}/top", endpoint=BestMatchesHtml), Route("/{gametype}/", endpoint=MatchesHtml), + Route("/{gametype}/robot24hour.json", endpoint=Last24HoursMatchesForRobot), Route("/{gametype}/{page:int}/", endpoint=MatchesHtml), ] diff --git a/tests/samples/match_list_24hour_1.json b/tests/samples/match_list_24hour_1.json new file mode 100644 index 0000000..6581a65 --- /dev/null +++ b/tests/samples/match_list_24hour_1.json @@ -0,0 +1,110 @@ +[ + { + "datetime": "2019-03-10 05:23 +05", + "gametype": "tdm", + "map": "ragnarok", + "match_id": "7807b4f5-3c98-459c-b2f9-8ad6b4f75d58", + "team1_score": 147, + "team2_score": 157, + "timestamp": 1552177424 + }, + { + "datetime": "2019-03-10 04:52 +05", + "gametype": "tdm", + "map": "limbus", + "match_id": "a254f41d-125f-4d4b-b66e-564bf095b8f1", + "team1_score": 152, + "team2_score": 157, + "timestamp": 1552175533 + }, + { + "datetime": "2019-03-10 04:20 +05", + "gametype": "tdm", + "map": "hiddenfortress", + "match_id": "0778f428-2606-4f3c-83dc-b4099b970814", + "team1_score": 202, + "team2_score": 176, + "timestamp": 1552173642 + }, + { + "datetime": "2019-03-10 03:51 +05", + "gametype": "tdm", + "map": "purgatory", + "match_id": "6e34afa3-a8e0-4dba-a496-3fc17e615e8e", + "team1_score": 124, + "team2_score": 165, + "timestamp": 1552171881 + }, + { + "datetime": "2019-03-10 03:22 +05", + "gametype": "tdm", + "map": "grimdungeons", + "match_id": "9cbb425a-b7a9-4376-9b1a-e68e8622f851", + "team1_score": 128, + "team2_score": 118, + "timestamp": 1552170124 + }, + { + "datetime": "2019-03-10 02:48 +05", + "gametype": "tdm", + "map": "dreadfulplace", + "match_id": "55ef6e6a-f7ab-4f4b-ba26-c77963147b98", + "team1_score": 165, + "team2_score": 137, + "timestamp": 1552168122 + }, + { + "datetime": "2019-03-10 02:20 +05", + "gametype": "tdm", + "map": "purgatory", + "match_id": "a22d0122-1382-4533-bf01-403114fac08f", + "team1_score": 123, + "team2_score": 140, + "timestamp": 1552166418 + }, + { + "datetime": "2019-03-10 01:48 +05", + "gametype": "tdm", + "map": "purgatory", + "match_id": "c0ac214d-b228-440b-a3fd-b5fe6ce3081d", + "team1_score": 151, + "team2_score": 148, + "timestamp": 1552164488 + }, + { + "datetime": "2019-03-10 01:16 +05", + "gametype": "tdm", + "map": "dreadfulplace", + "match_id": "8d599bb1-6f7f-4dcf-9e95-da62f2b1a698", + "team1_score": 103, + "team2_score": 153, + "timestamp": 1552162600 + }, + { + "datetime": "2019-03-10 00:49 +05", + "gametype": "tdm", + "map": "hiddenfortress", + "match_id": "61aad138-b69d-4ae7-b02e-23a9cfb7935f", + "team1_score": 207, + "team2_score": 181, + "timestamp": 1552160956 + }, + { + "datetime": "2019-03-10 00:18 +05", + "gametype": "tdm", + "map": "purgatory", + "match_id": "4b4ee658-0140-46ea-9d84-5bb802199400", + "team1_score": 144, + "team2_score": 130, + "timestamp": 1552159115 + }, + { + "datetime": "2019-03-09 23:41 +05", + "gametype": "tdm", + "map": "ragnarok", + "match_id": "dd961b26-bafe-4bd3-a515-c0ec156fd85c", + "team1_score": 137, + "team2_score": 146, + "timestamp": 1552156868 + } +] diff --git a/tests/samples/match_list_24hour_2.json b/tests/samples/match_list_24hour_2.json new file mode 100644 index 0000000..85bd436 --- /dev/null +++ b/tests/samples/match_list_24hour_2.json @@ -0,0 +1,11 @@ +[ + { + "datetime": "2019-03-09 23:41 +05", + "gametype": "tdm", + "map": "ragnarok", + "match_id": "dd961b26-bafe-4bd3-a515-c0ec156fd85c", + "team1_score": 137, + "team2_score": 146, + "timestamp": 1552156868 + } +] diff --git a/tests/test_matches.py b/tests/test_matches.py index b152d71..d768b09 100644 --- a/tests/test_matches.py +++ b/tests/test_matches.py @@ -53,3 +53,24 @@ def test_old_routes(service, old_uri, new_uri): def test_root_route(service): resp = service.get("/", 307) assert resp.headers["Location"].endswith("/matches/") + + +@mark.parametrize( + "time_value,match_sample", + [ + param(1552177500, "match_list_24hour_1"), + param(1552159110, "match_list_24hour_2"), + ], +) +def test_matches_json24hours(service, monkeypatch, time_value, match_sample): + from qllr.blueprints import matches + + with monkeypatch.context() as m: + + def time(): + return time_value + + m.setattr(matches, "time", time) + + resp = service.get("/matches/tdm/robot24hour.json") + assert resp.json()["matches"] == read_json_sample(match_sample)