def test_with_real_database(): """Test using the same database as your Flask app (PostgreSQL)""" import sys import os # Add project root to path project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, project_root) try: from app import create_app from app.models import Job, User, Category from app.extensions import db app = create_app() with app.app_context(): print(f"๐Ÿ—„๏ธ Connected to: {app.config['SQLALCHEMY_DATABASE_URI'][:50]}...") # Check jobs jobs = Job.query.limit(5).all() print(f"๐Ÿ“Š Found {len(jobs)} jobs in database:") for job in jobs: print(f" ๐Ÿ—๏ธ ID: {job.id} - {job.title} - {job.location}") # Check users users = User.query.limit(5).all() print(f"๐Ÿ‘ฅ Found {len(users)} users:") for user in users: print(f" ๐Ÿ‘ค ID: {user.id} - {user.first_name} {user.last_name} ({user.role})") # Check categories categories = Category.query.limit(5).all() print(f"๐Ÿ—๏ธ Found {len(categories)} categories:") for cat in categories: print(f" ๐Ÿ“‹ {cat.name} - {cat.whs_risk_level}") if jobs and users: print("โœ… Database has data - ready for real contract generation!") # Try generating a contract with real data job = jobs[0] contractor = User.query.filter_by(role='contractor').first() or users[0] worker = User.query.filter_by(role='worker').first() or (users[1] if len(users) > 1 else users[0]) print(f"\n๐Ÿงช Testing contract generation:") print(f" Job: {job.title}") print(f" Contractor: {contractor.first_name} {contractor.last_name}") print(f" Worker: {worker.first_name} {worker.last_name}") # Create dummy application class DummyApp: def __init__(self): self.id = 999 app_data = DummyApp() # Test contract generation generator = ContractGenerator() try: # Check if job has hourly_rate field if hasattr(job, 'hourly_rate') and job.hourly_rate: contract_data = generator.generate_contract_data(job, contractor, worker, app_data) filename = f"real_contract_{job.id}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.html" filepath = generator.save_contract_html(contract_data, filename) print(f"โœ… REAL CONTRACT GENERATED: {filepath}") print(f"๐Ÿ’ฐ Rate: ${contract_data['hourly_rate']}/hour") return filepath else: print("โŒ Job missing hourly_rate field") print(f" Available fields: {[c.name for c in job.__table__.columns]}") # Try using budget fields as backup if hasattr(job, 'budget_min') and job.budget_min: print(f"๐Ÿ’ก Using budget range: ${job.budget_min} - ${job.budget_max}") # Could calculate hourly rate from budget except Exception as e: print(f"โŒ Contract generation error: {e}") else: print("โŒ Database is empty - need to create some test data") except ImportError as e: print(f"โŒ Import error: {e}") print("๐Ÿ’ก Make sure you're running from the workspace root directory") except Exception as e: print(f"โŒ Database connection error: {e}") print("๐Ÿ’ก Check your PostgreSQL connection") def create_test_data(): """Create some test data in your PostgreSQL database""" import sys import os project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, project_root) try: from app import create_app from app.models import Job, User, Category from app.extensions import db app = create_app() with app.app_context(): # Check if we have categories category_count = Category.query.count() if category_count == 0: print("โŒ No categories found - run populate_categories.py first") return # Create test contractor contractor = User.query.filter_by(role='contractor').first() if not contractor: contractor = User( email='test.contractor@rateright.com', first_name='John', last_name='Builder', role='contractor', phone_number='0412345678', location='Sydney, NSW', abn_number='12345678901' ) contractor.set_password('password123') db.session.add(contractor) # Create test worker worker = User.query.filter_by(role='worker').first() if not worker: worker = User( email='test.worker@rateright.com', first_name='Mike', last_name='Carpenter', role='worker', phone_number='0487654321', location='Parramatta, NSW', abn_number='98765432109' ) worker.set_password('password123') db.session.add(worker) # Create test job job_count = Job.query.count() if job_count == 0: category = Category.query.first() job = Job( title='Test Carpentry Job', description='Build custom kitchen cabinets for residential project', contractor_id=contractor.id, category_id=category.id, location='Parramatta, NSW', budget_min=800, budget_max=1200, status='open', white_card_required=True, insurance_required=True ) db.session.add(job) db.session.commit() print("โœ… Test data created successfully!") except Exception as e: print(f"โŒ Error creating test data: {e}")