|
|
|
""" |
|
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: |
|
|
|
import sys |
|
import os |
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) |
|
|
|
|
|
from modal import functions |
|
print("β
Modal functions module imported") |
|
|
|
|
|
from modal import config |
|
print("β
Modal config module imported") |
|
|
|
|
|
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: |
|
|
|
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") |
|
|
|
|
|
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") |
|
|
|
|
|
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: |
|
|
|
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)") |
|
|
|
|
|
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: |
|
|
|
import sys |
|
import os |
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) |
|
|
|
from modal.functions import calculate_real_modal_cost |
|
|
|
|
|
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}") |
|
|
|
|
|
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 |
|
|
|
|
|
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) |