Skip to content

Commit c0d5f4b

Browse files
committed
style[test]: streamline JSON formatting and remove unused imports in test files
1 parent 6373d6b commit c0d5f4b

2 files changed

Lines changed: 88 additions & 116 deletions

File tree

tests/test_vm_routes.py

Lines changed: 82 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ class TestCreateVM:
4242

4343
def test_create_vm_success(self):
4444
"""Test successful VM creation"""
45-
payload = {
46-
"name": "test-vm",
47-
"flavor": "m1.small",
48-
"image": "ubuntu-22.04"
49-
}
45+
payload = {"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"}
5046

5147
response = client.post("/api/v1/vms", json=payload)
5248

@@ -60,11 +56,7 @@ def test_create_vm_success(self):
6056

6157
def test_create_vm_duplicate_name(self):
6258
"""Test creating VM with duplicate name fails"""
63-
payload = {
64-
"name": "test-vm",
65-
"flavor": "m1.small",
66-
"image": "ubuntu-22.04"
67-
}
59+
payload = {"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"}
6860

6961
# Create first VM
7062
response1 = client.post("/api/v1/vms", json=payload)
@@ -79,7 +71,7 @@ def test_create_vm_invalid_name(self):
7971
payload = {
8072
"name": "ab", # Too short
8173
"flavor": "m1.small",
82-
"image": "ubuntu-22.04"
74+
"image": "ubuntu-22.04",
8375
}
8476

8577
response = client.post("/api/v1/vms", json=payload)
@@ -90,7 +82,7 @@ def test_create_vm_invalid_flavor(self):
9082
payload = {
9183
"name": "test-vm",
9284
"flavor": "invalid-flavor",
93-
"image": "ubuntu-22.04"
85+
"image": "ubuntu-22.04",
9486
}
9587

9688
response = client.post("/api/v1/vms", json=payload)
@@ -116,7 +108,7 @@ def test_list_vms_with_items(self):
116108
payload = {
117109
"name": f"test-vm-{i}",
118110
"flavor": "m1.small",
119-
"image": "ubuntu-22.04"
111+
"image": "ubuntu-22.04",
120112
}
121113
client.post("/api/v1/vms", json=payload)
122114

@@ -134,7 +126,7 @@ def test_list_vms_pagination(self):
134126
payload = {
135127
"name": f"test-vm-{i}",
136128
"flavor": "m1.small",
137-
"image": "ubuntu-22.04"
129+
"image": "ubuntu-22.04",
138130
}
139131
client.post("/api/v1/vms", json=payload)
140132

@@ -149,18 +141,16 @@ def test_list_vms_pagination(self):
149141
def test_list_vms_with_status_filter(self):
150142
"""Test filtering VMs by status"""
151143
# Create 2 VMs
152-
response1 = client.post("/api/v1/vms", json={
153-
"name": "vm-1",
154-
"flavor": "m1.small",
155-
"image": "ubuntu-22.04"
156-
})
144+
response1 = client.post(
145+
"/api/v1/vms",
146+
json={"name": "vm-1", "flavor": "m1.small", "image": "ubuntu-22.04"},
147+
)
157148
vm1_id = response1.json()["id"]
158149

159-
client.post("/api/v1/vms", json={
160-
"name": "vm-2",
161-
"flavor": "m1.small",
162-
"image": "ubuntu-22.04"
163-
})
150+
client.post(
151+
"/api/v1/vms",
152+
json={"name": "vm-2", "flavor": "m1.small", "image": "ubuntu-22.04"},
153+
)
164154

165155
# Start one VM
166156
client.post(f"/api/v1/vms/{vm1_id}/start")
@@ -178,11 +168,10 @@ class TestGetVM:
178168
def test_get_vm_success(self):
179169
"""Test retrieving existing VM"""
180170
# Create VM
181-
create_response = client.post("/api/v1/vms", json={
182-
"name": "test-vm",
183-
"flavor": "m1.small",
184-
"image": "ubuntu-22.04"
185-
})
171+
create_response = client.post(
172+
"/api/v1/vms",
173+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
174+
)
186175
vm_id = create_response.json()["id"]
187176

188177
# Get VM
@@ -208,11 +197,10 @@ class TestGetVMStatus:
208197
def test_get_status_success(self):
209198
"""Test getting VM status"""
210199
# Create VM
211-
create_response = client.post("/api/v1/vms", json={
212-
"name": "test-vm",
213-
"flavor": "m1.small",
214-
"image": "ubuntu-22.04"
215-
})
200+
create_response = client.post(
201+
"/api/v1/vms",
202+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
203+
)
216204
vm_id = create_response.json()["id"]
217205

218206
# Get status
@@ -238,11 +226,10 @@ class TestStartVM:
238226
def test_start_vm_success(self):
239227
"""Test starting a stopped VM"""
240228
# Create VM
241-
create_response = client.post("/api/v1/vms", json={
242-
"name": "test-vm",
243-
"flavor": "m1.small",
244-
"image": "ubuntu-22.04"
245-
})
229+
create_response = client.post(
230+
"/api/v1/vms",
231+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
232+
)
246233
vm_id = create_response.json()["id"]
247234

248235
# Start VM
@@ -255,11 +242,10 @@ def test_start_vm_success(self):
255242
def test_start_vm_already_running(self):
256243
"""Test starting an already running VM fails"""
257244
# Create and start VM
258-
create_response = client.post("/api/v1/vms", json={
259-
"name": "test-vm",
260-
"flavor": "m1.small",
261-
"image": "ubuntu-22.04"
262-
})
245+
create_response = client.post(
246+
"/api/v1/vms",
247+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
248+
)
263249
vm_id = create_response.json()["id"]
264250
client.post(f"/api/v1/vms/{vm_id}/start")
265251

@@ -275,11 +261,10 @@ class TestStopVM:
275261
def test_stop_vm_success(self):
276262
"""Test stopping a running VM"""
277263
# Create and start VM
278-
create_response = client.post("/api/v1/vms", json={
279-
"name": "test-vm",
280-
"flavor": "m1.small",
281-
"image": "ubuntu-22.04"
282-
})
264+
create_response = client.post(
265+
"/api/v1/vms",
266+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
267+
)
283268
vm_id = create_response.json()["id"]
284269
client.post(f"/api/v1/vms/{vm_id}/start")
285270

@@ -293,11 +278,10 @@ def test_stop_vm_success(self):
293278
def test_stop_vm_already_stopped(self):
294279
"""Test stopping an already stopped VM fails"""
295280
# Create VM (already stopped)
296-
create_response = client.post("/api/v1/vms", json={
297-
"name": "test-vm",
298-
"flavor": "m1.small",
299-
"image": "ubuntu-22.04"
300-
})
281+
create_response = client.post(
282+
"/api/v1/vms",
283+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
284+
)
301285
vm_id = create_response.json()["id"]
302286

303287
# Try to stop
@@ -312,11 +296,10 @@ class TestRestartVM:
312296
def test_restart_vm_success(self):
313297
"""Test restarting a running VM"""
314298
# Create and start VM
315-
create_response = client.post("/api/v1/vms", json={
316-
"name": "test-vm",
317-
"flavor": "m1.small",
318-
"image": "ubuntu-22.04"
319-
})
299+
create_response = client.post(
300+
"/api/v1/vms",
301+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
302+
)
320303
vm_id = create_response.json()["id"]
321304
client.post(f"/api/v1/vms/{vm_id}/start")
322305

@@ -330,11 +313,10 @@ def test_restart_vm_success(self):
330313
def test_restart_stopped_vm_fails(self):
331314
"""Test restarting a stopped VM fails"""
332315
# Create VM (stopped)
333-
create_response = client.post("/api/v1/vms", json={
334-
"name": "test-vm",
335-
"flavor": "m1.small",
336-
"image": "ubuntu-22.04"
337-
})
316+
create_response = client.post(
317+
"/api/v1/vms",
318+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
319+
)
338320
vm_id = create_response.json()["id"]
339321

340322
# Try to restart
@@ -349,11 +331,10 @@ class TestPauseVM:
349331
def test_pause_vm_success(self):
350332
"""Test pausing a running VM"""
351333
# Create and start VM
352-
create_response = client.post("/api/v1/vms", json={
353-
"name": "test-vm",
354-
"flavor": "m1.small",
355-
"image": "ubuntu-22.04"
356-
})
334+
create_response = client.post(
335+
"/api/v1/vms",
336+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
337+
)
357338
vm_id = create_response.json()["id"]
358339
client.post(f"/api/v1/vms/{vm_id}/start")
359340

@@ -367,11 +348,10 @@ def test_pause_vm_success(self):
367348
def test_pause_stopped_vm_fails(self):
368349
"""Test pausing a stopped VM fails"""
369350
# Create VM (stopped)
370-
create_response = client.post("/api/v1/vms", json={
371-
"name": "test-vm",
372-
"flavor": "m1.small",
373-
"image": "ubuntu-22.04"
374-
})
351+
create_response = client.post(
352+
"/api/v1/vms",
353+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
354+
)
375355
vm_id = create_response.json()["id"]
376356

377357
# Try to pause
@@ -386,11 +366,10 @@ class TestResumeVM:
386366
def test_resume_vm_success(self):
387367
"""Test resuming a paused VM"""
388368
# Create, start, and pause VM
389-
create_response = client.post("/api/v1/vms", json={
390-
"name": "test-vm",
391-
"flavor": "m1.small",
392-
"image": "ubuntu-22.04"
393-
})
369+
create_response = client.post(
370+
"/api/v1/vms",
371+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
372+
)
394373
vm_id = create_response.json()["id"]
395374
client.post(f"/api/v1/vms/{vm_id}/start")
396375
client.post(f"/api/v1/vms/{vm_id}/pause")
@@ -405,11 +384,10 @@ def test_resume_vm_success(self):
405384
def test_resume_running_vm_fails(self):
406385
"""Test resuming a running VM fails"""
407386
# Create and start VM
408-
create_response = client.post("/api/v1/vms", json={
409-
"name": "test-vm",
410-
"flavor": "m1.small",
411-
"image": "ubuntu-22.04"
412-
})
387+
create_response = client.post(
388+
"/api/v1/vms",
389+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
390+
)
413391
vm_id = create_response.json()["id"]
414392
client.post(f"/api/v1/vms/{vm_id}/start")
415393

@@ -425,11 +403,10 @@ class TestDeleteVM:
425403
def test_delete_vm_success(self):
426404
"""Test deleting a VM"""
427405
# Create VM
428-
create_response = client.post("/api/v1/vms", json={
429-
"name": "test-vm",
430-
"flavor": "m1.small",
431-
"image": "ubuntu-22.04"
432-
})
406+
create_response = client.post(
407+
"/api/v1/vms",
408+
json={"name": "test-vm", "flavor": "m1.small", "image": "ubuntu-22.04"},
409+
)
433410
vm_id = create_response.json()["id"]
434411

435412
# Delete VM
@@ -456,11 +433,14 @@ class TestCompleteWorkflows:
456433
def test_full_lifecycle(self):
457434
"""Test complete VM lifecycle"""
458435
# Create
459-
response = client.post("/api/v1/vms", json={
460-
"name": "workflow-vm",
461-
"flavor": "m1.medium",
462-
"image": "ubuntu-22.04"
463-
})
436+
response = client.post(
437+
"/api/v1/vms",
438+
json={
439+
"name": "workflow-vm",
440+
"flavor": "m1.medium",
441+
"image": "ubuntu-22.04",
442+
},
443+
)
464444
assert response.status_code == 201
465445
vm_id = response.json()["id"]
466446

@@ -499,11 +479,14 @@ def test_multiple_vms_workflow(self):
499479

500480
# Create 3 VMs
501481
for i in range(3):
502-
response = client.post("/api/v1/vms", json={
503-
"name": f"multi-vm-{i}",
504-
"flavor": "m1.small",
505-
"image": "ubuntu-22.04"
506-
})
482+
response = client.post(
483+
"/api/v1/vms",
484+
json={
485+
"name": f"multi-vm-{i}",
486+
"flavor": "m1.small",
487+
"image": "ubuntu-22.04",
488+
},
489+
)
507490
assert response.status_code == 201
508491
vm_ids.append(response.json()["id"])
509492

0 commit comments

Comments
 (0)