-
Notifications
You must be signed in to change notification settings - Fork 60
189 lines (164 loc) · 6.01 KB
/
cli_test.yaml
File metadata and controls
189 lines (164 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
name: CLI Test
on: [push, workflow_call, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
app_type: ["Blank", "SyncORM", "AsyncORM", "MongoDB", "PostgresSync", "PostgresAsync", "MySQLSync", "MySQLAsync"]
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install uv
uses: astral-sh/setup-uv@v7
with:
version: "0.9.7"
enable-cache: true
- name: Install dependencies
run: uv sync --no-dev --group test
- name: Test CLI Commands
run: |
app_name="${{ matrix.app_type }}App"
case "${{ matrix.app_type }}" in
"Blank")
uv run pynest generate application -n "$app_name"
;;
"SyncORM")
uv run pynest generate application -n "$app_name" -db sqlite
;;
"AsyncORM")
uv run pynest generate application -n "$app_name" -db sqlite --is-async
;;
"MongoDB")
uv run pynest generate application -n "$app_name" -db mongodb
;;
"PostgresSync")
uv run pynest generate application -n "$app_name" -db postgresql
;;
"PostgresAsync")
uv run pynest generate application -n "$app_name" -db postgresql --is-async
;;
"MySQLSync")
uv run pynest generate application -n "$app_name" -db mysql
;;
"MySQLAsync")
uv run pynest generate application -n "$app_name" -db mysql --is-async
;;
esac
cd "$app_name"
uv run pynest generate resource -n user
uv run pynest generate gateway -n chat -p src
- name: Verify Boilerplate
run: |
app_name="${{ matrix.app_type }}App"
if [ -d "$app_name" ]; then
echo "Directory $app_name exists."
else
echo "Directory $app_name does not exist."
exit 1
fi
if [ -d "$app_name/src/user" ]; then
echo "Directory $app_name/src/user exists."
else
echo "Directory $app_name does not exist."
exit 1
fi
declare -a files=("main.py" "requirements.txt" "README.md")
declare -a src_level_files=("app_module.py" "app_service.py" "app_controller.py")
declare -a module_files=("user_controller.py" "user_service.py" "user_module.py" "user_model.py")
for file in "${files[@]}"; do
if [ -f "$app_name/$file" ]; then
echo "$file exists in $app_name."
else
echo "$file does not exist in $app_name."
exit 1
fi
done
for file in "${src_level_files[@]}"; do
if [ -f "$app_name/src/$file" ]; then
echo "$file exists in $app_name."
else
echo "$file does not exist in $app_name."
exit 1
fi
done
for file in "${module_files[@]}"; do
if [ -f "$app_name/src/user/$file" ]; then
echo "$file exists in $app_name."
else
echo "$file does not exist in $app_name."
exit 1
fi
done
gateway_file="$app_name/src/chat_gateway.py"
if [ -f "$gateway_file" ]; then
echo "$gateway_file exists."
else
echo "$gateway_file does not exist."
exit 1
fi
if ! grep -q '@WebSocketGateway(namespace="/chat")' "$gateway_file"; then
echo "$gateway_file is missing the expected @WebSocketGateway decorator."
exit 1
fi
echo "Boilerplate for ${{ matrix.app_type }} generated successfully."
- name: Ping generated WebSocket app
if: matrix.app_type == 'Blank'
run: |
cd "${{ matrix.app_type }}App"
uv run python - <<'PY'
import asyncio
import importlib.util
import json
import socket
import uvicorn
import websockets
from nest.core import Module, PyNestContainer, PyNestFactory
spec = importlib.util.spec_from_file_location(
"chat_gateway", "src/chat_gateway.py"
)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
ChatGateway = mod.ChatGateway
@Module(providers=[ChatGateway])
class App:
pass
def free_port():
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
return sock.getsockname()[1]
async def main():
PyNestContainer._instance = None
app = PyNestFactory.create(App).get_server()
port = free_port()
config = uvicorn.Config(
app, host="127.0.0.1", port=port,
log_level="critical", lifespan="off",
)
server = uvicorn.Server(config)
task = asyncio.create_task(server.serve())
for _ in range(200):
if server.started:
break
await asyncio.sleep(0.05)
try:
async with websockets.connect(
f"ws://127.0.0.1:{port}/chat"
) as ws:
await ws.send(json.dumps(
{"event": "ping", "data": {"hello": "world"}}
))
response = json.loads(await ws.recv())
assert response == {
"event": "pong", "data": {"hello": "world"},
}, response
print("WEBSOCKET_PING_OK")
finally:
server.should_exit = True
await task
asyncio.run(main())
PY