#!/usr/bin/env python3 """ Test PACK_IMPORT_COMENZI.importa_comanda with error handling """ import oracledb import os import json from datetime import datetime from dotenv import load_dotenv # Load environment load_dotenv('.env') # Oracle configuration user = os.environ['ORACLE_USER'] password = os.environ['ORACLE_PASSWORD'] dsn = os.environ['ORACLE_DSN'] # Initialize Oracle client try: instantclient_path = os.environ.get('INSTANTCLIENTPATH', '/opt/oracle/instantclient_23_9') oracledb.init_oracle_client(lib_dir=instantclient_path) except Exception as e: pass def test_import_comanda(): """Test complete order import with detailed error checking""" print("๐Ÿงช Testing PACK_IMPORT_COMENZI.importa_comanda...") try: with oracledb.connect(user=user, password=password, dsn=dsn) as conn: with conn.cursor() as cur: # Create test partner with unique name to avoid -1 return import time unique_suffix = int(time.time()) % 10000 partner_name = f'Test Import Partner {unique_suffix}' print(f"\n๐Ÿ‘ค Creating test partner: {partner_name}") # Use PL/SQL block to avoid DML in SELECT issue partner_var = cur.var(oracledb.NUMBER) cur.execute(""" DECLARE v_partner_id NUMBER; BEGIN v_partner_id := PACK_IMPORT_PARTENERI.cauta_sau_creeaza_partener( NULL, :partner_name, 'JUD:Bucuresti;BUCURESTI;Str. Importului;123', '0722123456', 'test.import@email.com' ); :result := v_partner_id; END; """, {'partner_name': partner_name, 'result': partner_var}) partner_id = partner_var.getvalue() print(f"Partner creation result: {partner_id}") # Check for errors if partner_id is -1 if partner_id == -1: cur.execute("SELECT PACK_IMPORT_PARTENERI.get_last_error FROM DUAL") partner_error = cur.fetchone()[0] print(f"โŒ Partner creation error: {partner_error}") print("โš ๏ธ Trying alternative approach with company CUI...") # Try with a company CUI to force creation unique_cui = f'RO{1000000 + unique_suffix}' company_var = cur.var(oracledb.NUMBER) cur.execute(""" DECLARE v_partner_id NUMBER; BEGIN v_partner_id := PACK_IMPORT_PARTENERI.cauta_sau_creeaza_partener( :cui, :partner_name, 'JUD:Bucuresti;BUCURESTI;Str. Importului;123', '0722123456', 'test.import@email.com' ); :result := v_partner_id; END; """, {'cui': unique_cui, 'partner_name': f'{partner_name} SRL', 'result': company_var}) partner_id = company_var.getvalue() print(f"Company creation result: {partner_id}") if partner_id == -1: cur.execute("SELECT PACK_IMPORT_PARTENERI.get_last_error FROM DUAL") partner_error = cur.fetchone()[0] print(f"โŒ Company creation error: {partner_error}") return # Exit if we can't create any partner print(f"โœ… Using Partner ID: {partner_id}") # Test articles JSON articles_json = json.dumps([ {"sku": "CAFE100", "cantitate": 2, "pret": 45.0}, {"sku": "SET01", "cantitate": 1, "pret": 150.0} ]) print(f"\n๐Ÿ“ฆ Testing order import...") print(f"Articles JSON: {articles_json}") # Clear any previous errors cur.execute("SELECT PACK_IMPORT_COMENZI.get_last_error FROM DUAL") prev_error = cur.fetchone()[0] if prev_error: print(f"Previous error (will be cleared): {prev_error}") cur.callproc("PACK_IMPORT_COMENZI.clear_error") # Try to import order order_number = 'TEST-IMPORT-' + datetime.now().strftime('%Y%m%d-%H%M%S') print(f"Order number: {order_number}") # Use PL/SQL block instead of SELECT to allow DML operations result_var = cur.var(oracledb.NUMBER) cur.execute(""" DECLARE v_order_id NUMBER; BEGIN v_order_id := PACK_IMPORT_COMENZI.importa_comanda( :order_num, SYSDATE, :partner_id, :articles_json, NULL, NULL, 'P1-004 test order import' ); :result := v_order_id; END; """, { 'order_num': order_number, 'partner_id': partner_id, 'articles_json': articles_json, 'result': result_var }) order_id = result_var.getvalue() print(f"Order import result: {order_id}") # Check for errors cur.execute("SELECT PACK_IMPORT_COMENZI.get_last_error FROM DUAL") last_error = cur.fetchone()[0] if order_id > 0: print(f"โœ… Order imported successfully! ID: {order_id}") # Verify order was created cur.execute(""" SELECT c.numar, c.data_comanda, p.denumire, c.observatii FROM comenzi c JOIN parteneri p ON c.id_partener = p.id_partener WHERE c.id_comanda = :order_id """, {'order_id': order_id}) order_info = cur.fetchone() if order_info: print(f"Order details:") print(f" Number: {order_info[0]}") print(f" Date: {order_info[1]}") print(f" Partner: {order_info[2]}") print(f" Notes: {order_info[3]}") # Check order items cur.execute(""" SELECT a.codmat, dc.cantitate, dc.pret_unitar, dc.valoare FROM det_comenzi dc JOIN articole a ON dc.id_articol = a.id_articol WHERE dc.id_comanda = :order_id ORDER BY dc.id_det_comanda """, {'order_id': order_id}) items = cur.fetchall() print(f"\nOrder items ({len(items)} total):") total_value = 0 for item in items: item_value = float(item[3]) if item[3] else 0 total_value += item_value print(f" - {item[0]}: Qty={item[1]}, Price={item[2]}, Value={item[3]}") print(f"Total order value: {total_value}") else: print(f"โŒ Order import failed: {order_id}") if last_error: print(f"Error details: {last_error}") else: print("No specific error message available") conn.commit() except Exception as e: print(f"โŒ Test failed: {e}") def main(): print("๐Ÿงช PACK_IMPORT_COMENZI.importa_comanda Detailed Test") print("=" * 60) test_import_comanda() if __name__ == "__main__": main()