Skip to content

Commit ad60427

Browse files
authored
fix: do not generate API docs in tests (#1015)
This slows the tests down to a crawl.
1 parent 1386c6a commit ad60427

3 files changed

Lines changed: 124 additions & 117 deletions

File tree

src/aleph/web/__init__.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ def init_cors(app: web.Application):
3232
cors.add(route)
3333

3434

35-
def create_aiohttp_app() -> web.Application:
35+
def create_aiohttp_app(with_swagger: bool = True) -> web.Application:
3636
app = web.Application(client_max_size=1024**2 * 64)
3737

38-
components_path = str(
39-
importlib.resources.files("aleph.web.controllers") / "components.yaml"
40-
)
41-
swagger = SwaggerDocs(
42-
app,
43-
info=SwaggerInfo(title="Aleph CCN API", version="0.9.3"),
44-
swagger_ui_settings=SwaggerUiSettings(path="/api/docs"),
45-
components=components_path,
46-
validate=False,
47-
)
38+
swagger = None
39+
if with_swagger:
40+
components_path = str(
41+
importlib.resources.files("aleph.web.controllers") / "components.yaml"
42+
)
43+
swagger = SwaggerDocs(
44+
app,
45+
info=SwaggerInfo(title="Aleph CCN API", version="0.9.3"),
46+
swagger_ui_settings=SwaggerUiSettings(path="/api/docs"),
47+
components=components_path,
48+
validate=False,
49+
)
4850

4951
tpl_path = str(importlib.resources.files("aleph.web") / "templates")
5052
jinja_loader = jinja2.ChoiceLoader(
Lines changed: 110 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib.resources
2+
from typing import Optional
23

34
from aiohttp import web
45
from aiohttp_swagger3 import SwaggerDocs
@@ -20,7 +21,7 @@
2021
from aleph.web.controllers.programs import get_programs_on_message
2122

2223

23-
def register_routes(app: web.Application, swagger: SwaggerDocs):
24+
def register_routes(app: web.Application, swagger: Optional[SwaggerDocs]):
2425
app.router.add_static(
2526
"/static/",
2627
path=str(importlib.resources.files("aleph.web") / "static/"),
@@ -49,107 +50,111 @@ def register_routes(app: web.Application, swagger: SwaggerDocs):
4950
)
5051
app.router.add_get("/api/v0/version", version.version)
5152

52-
# All API routes go through swagger for documentation
53-
swagger.add_routes(
54-
[
55-
web.get("/metrics", main.metrics),
56-
web.get("/metrics.json", main.metrics_json),
57-
web.get("/api/v0/core/{node_id}/metrics", main.ccn_metric),
58-
web.get("/api/v0/compute/{node_id}/metrics", main.crn_metric),
59-
web.get("/api/v0/aggregates/{address}.json", aggregates.address_aggregate),
60-
web.get("/api/v0/aggregates", aggregates.view_aggregates_list),
61-
web.get("/api/v0/channels/list.json", channels.used_channels),
62-
web.get("/api/v0/info/public.json", info.public_multiaddress),
63-
web.get("/api/v0/messages.json", messages.view_messages_list),
64-
web.post("/api/v0/messages", p2p.pub_message),
65-
web.get("/api/v0/messages/hashes", messages.view_message_hashes),
66-
web.get("/api/v0/messages/{item_hash}", messages.view_message),
67-
web.get(
68-
"/api/v0/messages/{item_hash}/content",
69-
messages.view_message_content,
70-
),
71-
web.get(
72-
"/api/v0/messages/{item_hash}/status",
73-
messages.view_message_status,
74-
),
75-
web.post("/api/v0/ipfs/pubsub/pub", p2p.pub_json),
76-
web.get("/api/v0/posts.json", posts.view_posts_list_v0),
77-
web.get("/api/v1/posts.json", posts.view_posts_list_v1),
78-
web.get("/api/v0/costs", prices.get_costs),
79-
web.get("/api/v0/price/{item_hash}", prices.message_price),
80-
web.post("/api/v0/price/estimate", prices.message_price_estimate),
81-
web.post(
82-
"/api/v0/price/recalculate",
83-
prices.recalculate_message_costs,
84-
),
85-
web.get(
86-
"/api/v0/addresses/stats.json",
87-
accounts.addresses_stats_view_v0,
88-
),
89-
web.get(
90-
"/api/v1/addresses/stats.json",
91-
accounts.addresses_stats_view_v1,
92-
),
93-
web.get(
94-
"/api/v0/addresses/{address}/balance",
95-
accounts.get_account_balance,
96-
),
97-
web.get("/api/v0/balances", accounts.get_chain_balances),
98-
web.get(
99-
"/api/v0/credit_balances",
100-
accounts.get_credit_balances_handler,
101-
),
102-
web.get(
103-
"/api/v0/addresses/{address}/files",
104-
accounts.get_account_files,
105-
),
106-
web.get(
107-
"/api/v0/addresses/{address}/post_types",
108-
accounts.get_account_post_types,
109-
),
110-
web.get(
111-
"/api/v0/addresses/{address}/channels",
112-
accounts.get_account_channels,
113-
),
114-
web.get(
115-
"/api/v0/addresses/{address}/credit_history",
116-
accounts.get_account_credit_history,
117-
),
118-
web.get(
119-
"/api/v0/messages/{item_hash}/consumed_credits",
120-
accounts.get_resource_consumed_credits_controller,
121-
),
122-
web.post("/api/v0/ipfs/add_file", ipfs.ipfs_add_file),
123-
web.post(
124-
"/api/v0/ipfs/add_json",
125-
storage.add_ipfs_json_controller,
126-
),
127-
web.post(
128-
"/api/v0/storage/add_file",
129-
storage.storage_add_file,
130-
),
131-
web.post(
132-
"/api/v0/storage/add_json",
133-
storage.add_storage_json_controller,
134-
),
135-
web.get("/api/v0/storage/{file_hash}", storage.get_hash),
136-
web.get("/api/v0/storage/raw/{file_hash}", storage.get_raw_hash),
137-
web.get(
138-
"/api/v0/storage/by-message-hash/{message_hash}",
139-
storage.get_file_metadata_by_message_hash,
140-
),
141-
web.get(
142-
"/api/v0/storage/by-ref/{ref}",
143-
storage.get_file_metadata_by_ref,
144-
),
145-
web.get(
146-
"/api/v0/storage/count/{hash}",
147-
storage.get_file_pins_count,
148-
),
149-
web.get("/version", version.version),
150-
web.get(
151-
"/api/v0/programs/on/message",
152-
get_programs_on_message,
153-
),
154-
]
155-
)
53+
# API routes: go through swagger for documentation when available,
54+
# otherwise register directly on the app router.
55+
api_routes = [
56+
web.get("/metrics", main.metrics),
57+
web.get("/metrics.json", main.metrics_json),
58+
web.get("/api/v0/core/{node_id}/metrics", main.ccn_metric),
59+
web.get("/api/v0/compute/{node_id}/metrics", main.crn_metric),
60+
web.get("/api/v0/aggregates/{address}.json", aggregates.address_aggregate),
61+
web.get("/api/v0/aggregates", aggregates.view_aggregates_list),
62+
web.get("/api/v0/channels/list.json", channels.used_channels),
63+
web.get("/api/v0/info/public.json", info.public_multiaddress),
64+
web.get("/api/v0/messages.json", messages.view_messages_list),
65+
web.post("/api/v0/messages", p2p.pub_message),
66+
web.get("/api/v0/messages/hashes", messages.view_message_hashes),
67+
web.get("/api/v0/messages/{item_hash}", messages.view_message),
68+
web.get(
69+
"/api/v0/messages/{item_hash}/content",
70+
messages.view_message_content,
71+
),
72+
web.get(
73+
"/api/v0/messages/{item_hash}/status",
74+
messages.view_message_status,
75+
),
76+
web.post("/api/v0/ipfs/pubsub/pub", p2p.pub_json),
77+
web.get("/api/v0/posts.json", posts.view_posts_list_v0),
78+
web.get("/api/v1/posts.json", posts.view_posts_list_v1),
79+
web.get("/api/v0/costs", prices.get_costs),
80+
web.get("/api/v0/price/{item_hash}", prices.message_price),
81+
web.post("/api/v0/price/estimate", prices.message_price_estimate),
82+
web.post(
83+
"/api/v0/price/recalculate",
84+
prices.recalculate_message_costs,
85+
),
86+
web.get(
87+
"/api/v0/addresses/stats.json",
88+
accounts.addresses_stats_view_v0,
89+
),
90+
web.get(
91+
"/api/v1/addresses/stats.json",
92+
accounts.addresses_stats_view_v1,
93+
),
94+
web.get(
95+
"/api/v0/addresses/{address}/balance",
96+
accounts.get_account_balance,
97+
),
98+
web.get("/api/v0/balances", accounts.get_chain_balances),
99+
web.get(
100+
"/api/v0/credit_balances",
101+
accounts.get_credit_balances_handler,
102+
),
103+
web.get(
104+
"/api/v0/addresses/{address}/files",
105+
accounts.get_account_files,
106+
),
107+
web.get(
108+
"/api/v0/addresses/{address}/post_types",
109+
accounts.get_account_post_types,
110+
),
111+
web.get(
112+
"/api/v0/addresses/{address}/channels",
113+
accounts.get_account_channels,
114+
),
115+
web.get(
116+
"/api/v0/addresses/{address}/credit_history",
117+
accounts.get_account_credit_history,
118+
),
119+
web.get(
120+
"/api/v0/messages/{item_hash}/consumed_credits",
121+
accounts.get_resource_consumed_credits_controller,
122+
),
123+
web.post("/api/v0/ipfs/add_file", ipfs.ipfs_add_file),
124+
web.post(
125+
"/api/v0/ipfs/add_json",
126+
storage.add_ipfs_json_controller,
127+
),
128+
web.post(
129+
"/api/v0/storage/add_file",
130+
storage.storage_add_file,
131+
),
132+
web.post(
133+
"/api/v0/storage/add_json",
134+
storage.add_storage_json_controller,
135+
),
136+
web.get("/api/v0/storage/{file_hash}", storage.get_hash),
137+
web.get("/api/v0/storage/raw/{file_hash}", storage.get_raw_hash),
138+
web.get(
139+
"/api/v0/storage/by-message-hash/{message_hash}",
140+
storage.get_file_metadata_by_message_hash,
141+
),
142+
web.get(
143+
"/api/v0/storage/by-ref/{ref}",
144+
storage.get_file_metadata_by_ref,
145+
),
146+
web.get(
147+
"/api/v0/storage/count/{hash}",
148+
storage.get_file_pins_count,
149+
),
150+
web.get("/version", version.version),
151+
web.get(
152+
"/api/v0/programs/on/message",
153+
get_programs_on_message,
154+
),
155+
]
156+
157+
if swagger is not None:
158+
swagger.add_routes(api_routes)
159+
else:
160+
app.router.add_routes(api_routes)

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def ccn_test_aiohttp_app(mocker, mock_config, session_factory, node_cache: NodeC
201201
event_loop = asyncio.get_event_loop()
202202
event_loop.set_debug(True)
203203

204-
app = create_aiohttp_app()
204+
app = create_aiohttp_app(with_swagger=False)
205205
app[APP_STATE_CONFIG] = mock_config
206206
app[APP_STATE_NODE_CACHE] = node_cache
207207
app[APP_STATE_P2P_CLIENT] = mocker.AsyncMock()

0 commit comments

Comments
 (0)