#!/usr/bin/env python3 """ Check if PACK_JSON exists and test its functions """ import oracledb import os import json 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 check_pack_json(): """Check if PACK_JSON package exists""" print("๐Ÿ” Checking PACK_JSON package...") try: with oracledb.connect(user=user, password=password, dsn=dsn) as conn: with conn.cursor() as cur: # Check if PACK_JSON exists cur.execute(""" SELECT object_name, object_type, status FROM user_objects WHERE object_name = 'PACK_JSON' ORDER BY object_type """) pack_json_objects = cur.fetchall() if pack_json_objects: print("โœ… PACK_JSON found:") for obj in pack_json_objects: print(f" - {obj[1]}: {obj[2]}") else: print("โŒ PACK_JSON not found") return False # Test PACK_JSON functions print("\n๐Ÿงช Testing PACK_JSON functions...") # Test JSON parsing test_json = json.dumps([ {"sku": "TEST01", "cantitate": 2, "pret": 10.5}, {"sku": "TEST02", "cantitate": 1, "pret": 25.0} ]) print(f"Test JSON: {test_json}") # Test parse_array try: cur.execute("SELECT * FROM TABLE(PACK_JSON.parse_array(?))", [test_json]) array_results = cur.fetchall() print(f"โœ… parse_array results: {len(array_results)} items") for i, item in enumerate(array_results): print(f" Item {i+1}: {item[0]}") # Test get_string and get_number on first item if i == 0: json_item = item[0] cur.execute("SELECT PACK_JSON.get_string(?, 'sku') FROM DUAL", [json_item]) sku = cur.fetchone()[0] print(f" sku: {sku}") cur.execute("SELECT PACK_JSON.get_number(?, 'cantitate') FROM DUAL", [json_item]) cantitate = cur.fetchone()[0] print(f" cantitate: {cantitate}") cur.execute("SELECT PACK_JSON.get_number(?, 'pret') FROM DUAL", [json_item]) pret = cur.fetchone()[0] print(f" pret: {pret}") except Exception as e: print(f"โŒ PACK_JSON test failed: {e}") return False print("โœ… PACK_JSON is working correctly") return True except Exception as e: print(f"โŒ Check failed: {e}") return False def test_simple_order_import(): """Test importa_comanda with simpler debugging""" print("\n๐Ÿงช Testing simple order import with debug...") try: with oracledb.connect(user=user, password=password, dsn=dsn) as conn: with conn.cursor() as cur: # Simple test with one article partner_id = -1 # Known test partner simple_json = '[{"sku": "CAFE100", "cantitate": 1, "pret": 25.0}]' order_number = 'TEST-SIMPLE-001' print(f"Testing with: {simple_json}") cur.execute(""" SELECT PACK_IMPORT_COMENZI.importa_comanda( :order_num, SYSDATE, :partner_id, :articles_json, NULL, NULL, 'Simple test order' ) FROM DUAL """, { 'order_num': order_number, 'partner_id': partner_id, 'articles_json': simple_json }) result = cur.fetchone()[0] cur.execute("SELECT PACK_IMPORT_COMENZI.get_last_error FROM DUAL") error = cur.fetchone()[0] print(f"Result: {result}") if error: print(f"Error: {error}") else: print("No error") except Exception as e: print(f"โŒ Test failed: {e}") def main(): print("๐Ÿ” PACK_JSON and Order Import Debug") print("=" * 50) json_exists = check_pack_json() if json_exists: test_simple_order_import() else: print("\nSkipping order test - PACK_JSON not available") if __name__ == "__main__": main()