Files
OSIT-AE-API-FastAPI/admin/development/test_v3_search.py

96 lines
3.4 KiB
Python

import requests
import json
# Configuration
BASE_URL = "https://dev-api.oneskyit.com/v3/crud"
# Valid account id_random provided by user
ACCOUNT_ID = "nqOzejLCDXM"
headers = {
"X-Account-ID": ACCOUNT_ID,
"Content-Type": "application/json"
}
def test_search(obj_type, query, description, params=None):
"""
Helper to run a search test and print results.
"""
print(f"--- Testing: {description} ---")
url = f"{BASE_URL}/{obj_type}/search"
try:
response = requests.post(url, headers=headers, json=query, params=params)
print(f"URL: {response.url}")
print(f"Status Code: {response.status_code}")
data = response.json()
# Check if the result is a list or single object
result_data = data.get('data')
if isinstance(result_data, list):
print(f"Result Count: {len(result_data)}")
if len(result_data) > 0:
# Print first item as example
print(f"First Item Example: {json.dumps(result_data[0], indent=2)[:200]}...")
else:
print(f"Result Type: {type(result_data)}")
print(f"Result Data: {json.dumps(result_data, indent=2)}")
if response.status_code != 200:
print(f"Meta Details: {json.dumps(data.get('meta', {}), indent=2)}")
except requests.exceptions.ConnectionError:
print(f"Error: Could not connect to {BASE_URL}. Is the API running?")
except Exception as e:
print(f"Error during test: {e}")
print("-" * 40 + "\n")
if __name__ == "__main__":
print(f"Starting Aether V3 Search Tests against {BASE_URL}\n")
# 1. Standardized Global Search (q property)
query_q = {
"q": "%" # Standardized full-text search across indexed columns
}
test_search("journal", query_q, "Global Search (q property)")
# 2. Hybrid Filtering (POST Body + Query Params)
query_simple = {
"and": [{"field": "name", "op": "like", "value": "%"}]
}
params_hybrid = {"enabled": "disabled"} # Should find disabled journals if any
test_search("journal", query_simple, "Hybrid Filtering (Body + ?enabled=disabled)", params=params_hybrid)
# 3. View Selection (view parameter)
# Testing with 'site_domain' which has 'tbl_alt' defined as 'v_site_domain_fqdn_id'
query_site = {"q": "%"}
params_view = {"view": "alt"}
test_search("site_domain", query_site, "View Selection (view=alt)", params=params_view)
# 4. Explicit Parent Filtering
# Testing 'journal_entry' belonging to a journal (journal_id=1 exists based on previous tests)
# We'll use the 'for_obj_type' and 'for_obj_id' as query params
# Assuming id_random 'DCAV-06-35-85' exists for journal_id 1
query_empty = {}
params_parent = {
"for_obj_type": "journal",
"for_obj_id": "DCAV-06-35-85"
}
test_search("journal_entry", query_empty, "Explicit Parent Filtering (?for_obj_type=journal)", params=params_parent)
# 5. Complex Nested Logic (Recap)
query_nested = {
"and": [
{"field": "enable", "op": "eq", "value": True},
{
"or": [
{"field": "name", "op": "like", "value": "%Journal%"},
{"field": "summary", "op": "is_not_null"}
]
}
]
}
test_search("journal", query_nested, "Nested Logic (AND + OR group)")
print("Tests Complete.")