Download test_api_endpoints.py from isold/meloon: direct link, hf CLI and curl.
- Browser
- Download file 4.02 kB
-
https://huggingface.co/spaces/isold/meloon/resolve/main/test_api_endpoints.py
- Command line
-
hf download hf://spaces/isold/meloon/test_api_endpoints.py
-
curl -L -o test_api_endpoints.py https://huggingface.co/spaces/isold/meloon/resolve/main/test_api_endpoints.py
4.02 kB
| import requests | |
| import time | |
| import sys | |
| BASE_URL = "http://127.0.0.1:8000/api/v1" | |
| EMAIL = f"testuser_{int(time.time())}@example.com" | |
| PASSWORD = "password123" | |
| def print_result(name, res, expected_status=200): | |
| if res.status_code == expected_status: | |
| print(f"✅ {name} - Status: {res.status_code}") | |
| try: | |
| return res.json() | |
| except: | |
| return None | |
| else: | |
| print(f"❌ {name} - Status: {res.status_code}") | |
| print(f"Response: {res.text}") | |
| sys.exit(1) | |
| def run_tests(): | |
| print("--- Starting API Tests ---") | |
| # 1. Register | |
| print("\n1. Authentication") | |
| res = requests.post(f"{BASE_URL}/auth/register", json={"email": EMAIL, "password": PASSWORD}) | |
| print_result("Register User", res, 201) | |
| # 2. Login | |
| res = requests.post(f"{BASE_URL}/auth/login", json={"email": EMAIL, "password": PASSWORD}) | |
| login_data = print_result("Login User", res, 200) | |
| token = login_data.get("token") | |
| headers = {"Authorization": f"Bearer {token}"} | |
| # 3. Get Me | |
| res = requests.get(f"{BASE_URL}/auth/me", headers=headers) | |
| print_result("Get Me", res, 200) | |
| # 4. Data Sources | |
| print("\n2. Data Sources") | |
| res = requests.get(f"{BASE_URL}/datasources/", headers=headers) | |
| print_result("List Data Sources (Initial)", res, 200) | |
| # Create dummy data source | |
| ds_payload = { | |
| "name": "Test DB", | |
| "type": "mysql", | |
| "host": "localhost", | |
| "port": 3306, | |
| "username": "root", | |
| "password": "password", | |
| "database_name": "test_db", | |
| "description": "A test db" | |
| } | |
| res = requests.post(f"{BASE_URL}/datasources/", json=ds_payload, headers=headers) | |
| ds_data = print_result("Create Data Source", res, 201) | |
| ds_id = ds_data.get("id") | |
| # Update Data Source | |
| res = requests.put(f"{BASE_URL}/datasources/{ds_id}/", json={"name": "Test DB Updated", "type": "mysql", "description": "Updated desc"}, headers=headers) | |
| print_result("Update Data Source", res, 200) | |
| # Get Data Sources | |
| res = requests.get(f"{BASE_URL}/datasources/", headers=headers) | |
| list_ds = print_result("List Data Sources", res, 200) | |
| if not isinstance(list_ds, list) or len(list_ds) == 0: | |
| print("❌ Data Source List is empty or not a list") | |
| sys.exit(1) | |
| # Test Connection | |
| res = requests.post(f"{BASE_URL}/datasources/{ds_id}/test/", headers=headers) | |
| print_result("Test Connection Data Source", res, 200) | |
| # 5. Conversations | |
| print("\n3. Conversations") | |
| # Create conversation | |
| res = requests.post(f"{BASE_URL}/conversations/", json={"data_source_id": ds_id, "title": "Test Chat"}, headers=headers) | |
| conv_data = print_result("Create Conversation", res, 200) | |
| conv_id = conv_data.get("id") | |
| # List conversations | |
| res = requests.get(f"{BASE_URL}/conversations/", headers=headers) | |
| print_result("List Conversations", res, 200) | |
| # Send message | |
| msg_payload = {"content": "Hello ai"} | |
| res = requests.post(f"{BASE_URL}/conversations/{conv_id}/messages/", json=msg_payload, headers=headers) | |
| # The message endpoint might fail since we don't have a real data source or gemini key depending on .env | |
| # We will accept 200 or 500 here just to see what happens | |
| if res.status_code in [200, 500]: | |
| print(f"✅ Send Message - Status: {res.status_code}") | |
| else: | |
| print_result("Send Message", res, 200) | |
| # Get history | |
| res = requests.get(f"{BASE_URL}/conversations/{conv_id}/messages/", headers=headers) | |
| print_result("Get Message History", res, 200) | |
| # Clean up (Delete Conversation) | |
| res = requests.delete(f"{BASE_URL}/conversations/{conv_id}/", headers=headers) | |
| print_result("Delete Conversation", res, 204) | |
| # Clean up (Delete Data Source) | |
| res = requests.delete(f"{BASE_URL}/datasources/{ds_id}/", headers=headers) | |
| print_result("Delete Data Source", res, 204) | |
| print("\n--- All Tests Passed! ---") | |
| if __name__ == "__main__": | |
| run_tests() | |