aquabarrier / tests /unit /test_manual_verification.py
rajeshbms's picture
Add validation report for AquaBarrier project implementation
d5f727d
#!/usr/bin/env python3
"""
Manual verification test to check actual database operations
"""
import sys
import requests
import json
import time
import subprocess
import os
def start_server():
"""Start the FastAPI server in background"""
env = os.environ.copy()
env['PATH'] = '/Users/mukeshkapoor/projects/aquabarrier/ab-ms-core/venv/bin:' + env['PATH']
cmd = [
'/Users/mukeshkapoor/projects/aquabarrier/ab-ms-core/venv/bin/uvicorn',
'app.app:app',
'--host', '0.0.0.0',
'--port', '8000'
]
process = subprocess.Popen(
cmd,
cwd='/Users/mukeshkapoor/projects/aquabarrier/ab-ms-core',
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Wait for server to start
print("Starting FastAPI server...")
time.sleep(3)
return process
def manual_deletion_test():
"""Manual step-by-step deletion test with verification"""
base_url = "http://localhost:8000/api/v1"
print("πŸ” MANUAL VERIFICATION TEST")
print("=" * 50)
try:
# Step 1: Create a barrier size
print("\\n1. Creating a test barrier size...")
barrier_data = {
'height': 15.0,
'width': 30.0,
'length': 200.0,
'cable_units': 10,
'price': 2500.00,
'is_standard': True,
'inventory_id': 2001,
'install_advisor_fees': 400.00,
'bidder_id': 100
}
response = requests.post(
f"{base_url}/bidders/barrier-sizes-sql",
params=barrier_data
)
if response.status_code == 201:
result = response.json()
barrier_size_id = result['barrier_size_id']
association_id = result['bidder_barrier_size_id']
bidder_id = 100
print(f" βœ… Created barrier size ID: {barrier_size_id}")
print(f" βœ… Created association ID: {association_id}")
print(f" βœ… For bidder ID: {bidder_id}")
# Step 2: Verify we can get the barrier size for this bidder
print("\\n2. Verifying barrier size association exists...")
response = requests.get(f"{base_url}/bidders/{bidder_id}/barrier-sizes")
if response.status_code == 200:
barrier_sizes = response.json()
found = any(bs.get('BarrierSizeId') == barrier_size_id for bs in barrier_sizes)
if found:
print(f" βœ… Barrier size {barrier_size_id} found in bidder {bidder_id}'s associations")
else:
print(f" ⚠️ Barrier size {barrier_size_id} not found in associations")
else:
print(f" ⚠️ Could not retrieve barrier sizes for bidder {bidder_id}")
# Step 3: Test the specific deletion endpoint
print(f"\\n3. Testing deletion endpoint...")
print(f" Executing: DELETE /bidders/barrier-sizes/bidder/{bidder_id}/barrier/{barrier_size_id}?cascade=true")
response = requests.delete(
f"{base_url}/bidders/barrier-sizes/bidder/{bidder_id}/barrier/{barrier_size_id}?cascade=true"
)
if response.status_code == 204:
print(f" βœ… Deletion successful (HTTP 204)")
# Step 4: Verify the association is deleted
print("\\n4. Verifying deletion...")
response = requests.get(f"{base_url}/bidders/{bidder_id}/barrier-sizes")
if response.status_code == 200:
barrier_sizes = response.json()
found = any(bs.get('BarrierSizeId') == barrier_size_id for bs in barrier_sizes)
if not found:
print(f" βœ… Association successfully removed from bidder {bidder_id}")
else:
print(f" ❌ Association still exists for bidder {bidder_id}")
# Step 5: Try to delete again (should get 404)
print("\\n5. Testing deletion of non-existent association...")
response = requests.delete(
f"{base_url}/bidders/barrier-sizes/bidder/{bidder_id}/barrier/{barrier_size_id}"
)
if response.status_code == 404:
print(f" βœ… Correctly returns 404 for already deleted association")
else:
print(f" ⚠️ Unexpected status: {response.status_code}")
else:
print(f" ❌ Deletion failed: {response.status_code}")
print(f" Response: {response.text}")
else:
print(f" ❌ Failed to create test data: {response.status_code}")
print(f" Response: {response.text}")
return False
# Step 6: Test cascade behavior with multiple bidders
print("\\n6. Testing cascade behavior with shared barrier size...")
# Create barrier size for bidder 200
barrier_data_shared = {
'height': 20.0,
'width': 40.0,
'length': 300.0,
'cable_units': 15,
'price': 3000.00,
'is_standard': False,
'bidder_id': 200
}
response = requests.post(f"{base_url}/bidders/barrier-sizes-sql", params=barrier_data_shared)
if response.status_code == 201:
shared_result = response.json()
shared_barrier_id = shared_result['barrier_size_id']
print(f" βœ… Created shared barrier size {shared_barrier_id} for bidder 200")
# Try to delete with cascade=true (should preserve barrier size since only one association)
response = requests.delete(
f"{base_url}/bidders/barrier-sizes/bidder/200/barrier/{shared_barrier_id}?cascade=true"
)
if response.status_code == 204:
print(f" βœ… Deleted association and barrier size {shared_barrier_id} (no other users)")
else:
print(f" Status: {response.status_code} - {response.text}")
return True
except requests.exceptions.ConnectionError:
print("❌ FAILED: Could not connect to the API server")
return False
except Exception as e:
print(f"❌ FAILED: {e}")
import traceback
traceback.print_exc()
return False
def main():
server_process = start_server()
try:
success = manual_deletion_test()
if success:
print("\\n" + "=" * 50)
print("πŸŽ‰ MANUAL VERIFICATION COMPLETED!")
print("βœ… Database operations working correctly")
print("βœ… Associations properly created and deleted")
print("βœ… Cascade logic functioning as expected")
print("βœ… Error handling appropriate")
else:
print("\\n" + "=" * 50)
print("❌ MANUAL VERIFICATION FAILED")
finally:
print("\\nStopping server...")
server_process.terminate()
try:
server_process.wait(timeout=5)
except subprocess.TimeoutExpired:
server_process.kill()
server_process.wait()
print("Server stopped.")
if __name__ == "__main__":
main()