File size: 5,924 Bytes
a963d65 |
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 |
#!/usr/bin/env python3
"""
Test: Modal Organization and Structure
Test that the organized Modal files structure works correctly
"""
import os
import sys
import importlib
def test_modal_imports():
"""Test that Modal functions can be imported from organized structure"""
print("π Test: Modal Import Structure")
try:
# Add current directory to Python path
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
# Test that modal.functions can be imported
from modal import functions
print("β
Modal functions module imported")
# Test that modal.config can be imported
from modal import config
print("β
Modal config module imported")
# Test that specific functions exist
assert hasattr(functions, 'app'), "Modal app not found"
assert hasattr(functions, 'calculate_real_modal_cost'), "Cost calculation function not found"
print("β
Modal functions accessible")
return True
except ImportError as e:
print(f"β Import error: {e}")
return False
except Exception as e:
print(f"β Modal import test failed: {e}")
return False
def test_deployment_files():
"""Test that deployment files exist and are accessible"""
print("\nπ Test: Deployment Files")
try:
# Check modal deployment file
modal_deploy_path = "modal/deploy.py"
assert os.path.exists(modal_deploy_path), f"Modal deploy file not found: {modal_deploy_path}"
print("β
Modal deployment file exists")
# Check local deployment file
local_deploy_path = "deploy_local.py"
assert os.path.exists(local_deploy_path), f"Local deploy file not found: {local_deploy_path}"
print("β
Local deployment file exists")
# Check main README
readme_path = "README.md"
assert os.path.exists(readme_path), f"Main README not found: {readme_path}"
print("β
Main README exists")
return True
except Exception as e:
print(f"β Deployment files test failed: {e}")
return False
def test_environment_config():
"""Test environment configuration"""
print("\nπ Test: Environment Configuration")
try:
# Test environment variables
modal_token_id = os.getenv("MODAL_TOKEN_ID", "")
modal_token_secret = os.getenv("MODAL_TOKEN_SECRET", "")
if modal_token_id and modal_token_secret:
print("β
Modal tokens configured")
else:
print("β οΈ Modal tokens not configured (expected for tests)")
# Test cost configuration
l4_rate = float(os.getenv("MODAL_L4_HOURLY_RATE", "0.73"))
platform_fee = float(os.getenv("MODAL_PLATFORM_FEE", "15"))
assert l4_rate > 0, "L4 rate should be positive"
assert platform_fee > 0, "Platform fee should be positive"
print(f"β
L4 Rate: ${l4_rate}/hour")
print(f"β
Platform Fee: {platform_fee}%")
return True
except Exception as e:
print(f"β Environment config test failed: {e}")
return False
def test_cost_calculation():
"""Test cost calculation function"""
print("\nπ Test: Cost Calculation Function")
try:
# Add current directory to Python path
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from modal.functions import calculate_real_modal_cost
# Test L4 cost calculation
cost_l4_1s = calculate_real_modal_cost(1.0, "L4")
cost_l4_10s = calculate_real_modal_cost(10.0, "L4")
assert cost_l4_1s > 0, "L4 cost should be positive"
assert cost_l4_10s > cost_l4_1s, "10s should cost more than 1s"
print(f"β
L4 1s cost: ${cost_l4_1s:.6f}")
print(f"β
L4 10s cost: ${cost_l4_10s:.6f}")
# Test CPU cost calculation
cost_cpu = calculate_real_modal_cost(1.0, "CPU")
assert cost_cpu >= 0, "CPU cost should be non-negative"
print(f"β
CPU 1s cost: ${cost_cpu:.6f}")
return True
except Exception as e:
print(f"β Cost calculation test failed: {e}")
return False
def main():
"""Run organization tests"""
print("π Testing Modal Organization Structure")
print("=" * 50)
tests = [
("Modal Imports", test_modal_imports),
("Deployment Files", test_deployment_files),
("Environment Config", test_environment_config),
("Cost Calculation", test_cost_calculation)
]
results = {}
for test_name, test_func in tests:
try:
result = test_func()
results[test_name] = result
except Exception as e:
print(f"β Test {test_name} crashed: {e}")
results[test_name] = False
# Summary
print("\n" + "=" * 50)
print("π Organization Test Results")
print("=" * 50)
passed = sum(1 for r in results.values() if r)
total = len(results)
for test_name, result in results.items():
status = "β
PASS" if result else "β FAIL"
print(f"{test_name}: {status}")
print(f"\nOverall: {passed}/{total} tests passed")
if passed == total:
print("π Modal organization structure is working!")
print("\nπ Ready for deployment:")
print("1. Modal production: python modal/deploy.py")
print("2. Local development: python deploy_local.py")
else:
print("β οΈ Some organization tests failed.")
return passed == total
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1) |