优化
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import asyncio
|
||||
|
||||
from api.routes import colg
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, region):
|
||||
self.region = region
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, traceback):
|
||||
return False
|
||||
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
async def json(self, content_type=None):
|
||||
await asyncio.sleep(0)
|
||||
return {
|
||||
'data': {
|
||||
'godDealRank': [{'price': '100'}, {'price': '101'}],
|
||||
'godTopDealRank': [{'price': '200'}, {'price': '201'}],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class FakeSession:
|
||||
def __init__(self, calls, **kwargs):
|
||||
self.calls = calls
|
||||
self.kwargs = kwargs
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, traceback):
|
||||
return False
|
||||
|
||||
def post(self, url, data):
|
||||
self.calls.append((url, data))
|
||||
return FakeResponse(data['serverNo'])
|
||||
|
||||
|
||||
def test_fetch_official_gold_uses_async_http_with_isolated_forms(monkeypatch):
|
||||
calls = []
|
||||
sessions = []
|
||||
|
||||
def create_session(**kwargs):
|
||||
session = FakeSession(calls, **kwargs)
|
||||
sessions.append(session)
|
||||
return session
|
||||
|
||||
monkeypatch.setattr(colg.aiohttp, 'ClientSession', create_session)
|
||||
|
||||
result = asyncio.run(colg._fetch_official_gold())
|
||||
|
||||
assert [data['serverNo'] for _, data in calls] == list(colg.OFFICIAL_GOLD_REGIONS)
|
||||
assert all(url == colg.OFFICIAL_GOLD_URL for url, _ in calls)
|
||||
assert len({id(data) for _, data in calls}) == len(colg.OFFICIAL_GOLD_REGIONS)
|
||||
assert [item['region'] for item in result] == [f'跨{region}' for region in colg.OFFICIAL_GOLD_REGIONS]
|
||||
assert result[0]['goldDealRank'] == [100, 101]
|
||||
assert result[0]['goldTopDealRank'] == [200, 201]
|
||||
assert sessions[0].kwargs['timeout'].total == 10
|
||||
assert 'serverNo' not in colg.payload_helper
|
||||
Reference in New Issue
Block a user