comgen-api / test_api.py
sk-shehan's picture
Initial API Commit
c913c62
Raw
History Blame Contribute Delete
3.55 kB
"""
Demo test script for the Enterprise Comment API.
Sends multiple separate code snippets (one per request) and prints
clean, presentation-friendly output.
"""
import json
import requests
URL = "http://127.0.0.1:8000/generate_comment"
test_cases = [
{
"name": "payment_terms",
"code": """def compute_due_date(invoice_date, terms):
# terms could include credit_days, installment_percentages, etc.
credit_days = terms.get("credit_days", 0)
due_date = add_days(invoice_date, credit_days)
return due_date""",
},
{
"name": "payment_entry_allocation_and_outstanding_balances",
"code": """def allocate_payment(payment_amount: float, references: list[dict]):
allocated = []
remaining = payment_amount
for ref in references: # e.g. invoices with outstanding_amount
take = min(ref["outstanding_amount"], remaining)
allocated.append({
"reference_doctype": ref["doctype"],
"reference_name": ref["name"],
"allocated_amount": take,
})
remaining -= take
if remaining <= 0:
break
if remaining > 0:
raise ValueError("Payment exceeds referenced outstanding balances")
return allocated""",
},
{
"name": "chart_of_accounts_account_and_account_number",
"code": """def validate_account_hierarchy(account: dict):
# group accounts vs ledger accounts
if account["account_type"] == "Group":
assert account.get("parent_account") is not None
else:
assert account["parent_account"] is not None
# uniqueness for account_number
if account_number_exists(account["account_number"], exclude=account["name"]):
raise ValueError("account_number must be unique")""",
},
{
"name": "make_acc_dimensions_offsetting_entry",
"code": """def make_acc_dimensions_offsetting_entry(gl_map):
accounting_dimensions_to_offset = get_accounting_dimensions_for_offsetting_entry(
gl_map, gl_map[0].company
)
no_of_dimensions = len(accounting_dimensions_to_offset)
if no_of_dimensions == 0:
return""",
},
{
"name": "fiscal_year_accounting_period_and_ledger_entry",
"code": """def validate_transaction_in_fiscal_year(posting_date: str, company: str) -> None:
fy = get_fiscal_year(company=company, posting_date=posting_date)
if fy is None:
raise ValueError("Posting date must be within an active fiscal year")""",
},
]
def code_preview(code: str, max_lines: int = 8) -> str:
lines = code.strip("\n").splitlines()
shown = lines[:max_lines]
preview = "\n".join(shown)
if len(lines) > max_lines:
preview += "\n..."
return preview
def main() -> None:
print("Enterprise Comment API - RAG End-to-End Test (5 sets)")
print(f"URL: {URL}")
for i, case in enumerate(test_cases, start=1):
print("\n" + "=" * 90)
print(f"SET {i}/{len(test_cases)}: {case['name']}")
print("- Code (preview) -")
print(code_preview(case["code"]))
print("- Requesting API -")
resp = requests.post(URL, json={"code": case["code"]})
if resp.status_code != 200:
print(f"[ERROR] Status Code: {resp.status_code}")
print(resp.text)
continue
result = resp.json()
print("[RESPONSE]")
print(json.dumps(result, indent=2, ensure_ascii=True))
print("\n" + "=" * 90)
print("Done.")
if __name__ == "__main__":
main()