Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
test_import_hooks.py · 8.0 KB · 217 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """Test import functionality with hook execution""" |
| 3 | |
| 4 | import asyncio |
| 5 | import aiohttp |
| 6 | import csv |
| 7 | import json |
| 8 | import io |
| 9 | import sys |
| 10 | from pathlib import Path |
| 11 | |
| 12 | BASE_URL = "http://localhost:8005" |
| 13 | |
| 14 | # Global flag to track if hooks were called |
| 15 | hooks_called = [] |
| 16 | |
| 17 | async def test_csv_import_with_hooks(): |
| 18 | """Test CSV import with hooks enabled""" |
| 19 | print("\n🧪 Testing CSV import WITH hooks...") |
| 20 | |
| 21 | # Create CSV data |
| 22 | csv_data = """name,email,phone,credit_limit,status |
| 23 | Test User 1,test1@example.com,555-0001,1000,active |
| 24 | Test User 2,test2@example.com,555-0002,2000,pending |
| 25 | Invalid Email,not-an-email,,3000,active""" |
| 26 | |
| 27 | async with aiohttp.ClientSession() as session: |
| 28 | # Clear existing data first |
| 29 | await session.delete(f"{BASE_URL}/api/customers/bulk") |
| 30 | |
| 31 | # Import with hooks enabled (default) |
| 32 | form = aiohttp.FormData() |
| 33 | form.add_field('file', csv_data, filename='test.csv', content_type='text/csv') |
| 34 | |
| 35 | async with session.post(f"{BASE_URL}/api/customers/import/csv", data=form) as resp: |
| 36 | result = await resp.json() |
| 37 | print(f"✅ Import with hooks: {result}") |
| 38 | assert result['hooks_executed'] == True, "Hooks should be executed" |
| 39 | # With validation hooks, invalid email should be skipped |
| 40 | if result.get('skipped_records', 0) > 0: |
| 41 | print(f" - Skipped {result['skipped_records']} records due to validation") |
| 42 | |
| 43 | async def test_csv_import_without_hooks(): |
| 44 | """Test CSV import with hooks disabled""" |
| 45 | print("\n🧪 Testing CSV import WITHOUT hooks...") |
| 46 | |
| 47 | # Same CSV data |
| 48 | csv_data = """name,email,phone,credit_limit,status |
| 49 | No Hook User 1,nohook1@example.com,555-1001,1500,active |
| 50 | No Hook User 2,nohook2@example.com,555-1002,2500,pending""" |
| 51 | |
| 52 | async with aiohttp.ClientSession() as session: |
| 53 | # Import with hooks disabled |
| 54 | form = aiohttp.FormData() |
| 55 | form.add_field('file', csv_data, filename='test.csv', content_type='text/csv') |
| 56 | |
| 57 | async with session.post( |
| 58 | f"{BASE_URL}/api/customers/import/csv?run_hooks=false", |
| 59 | data=form |
| 60 | ) as resp: |
| 61 | result = await resp.json() |
| 62 | print(f"✅ Import without hooks: {result}") |
| 63 | assert result['hooks_executed'] == False, "Hooks should not be executed" |
| 64 | |
| 65 | async def test_json_import_with_hooks(): |
| 66 | """Test JSON import with hooks enabled""" |
| 67 | print("\n🧪 Testing JSON import WITH hooks...") |
| 68 | |
| 69 | # Create JSON backup data |
| 70 | json_data = { |
| 71 | "resource": "customers", |
| 72 | "exported_at": "2025-09-19T18:00:00", |
| 73 | "records": [ |
| 74 | { |
| 75 | "name": "JSON User 1", |
| 76 | "email": "json1@example.com", |
| 77 | "phone": "555-2001", |
| 78 | "credit_limit": "3000.00", |
| 79 | "status": "active" |
| 80 | }, |
| 81 | { |
| 82 | "name": "JSON User 2", |
| 83 | "email": "json2@example.com", |
| 84 | "phone": "555-2002", |
| 85 | "credit_limit": "4000.00", |
| 86 | "status": "inactive" |
| 87 | } |
| 88 | ] |
| 89 | } |
| 90 | |
| 91 | async with aiohttp.ClientSession() as session: |
| 92 | # Import with hooks enabled (default) |
| 93 | form = aiohttp.FormData() |
| 94 | form.add_field('file', json.dumps(json_data), filename='test.json', content_type='application/json') |
| 95 | |
| 96 | async with session.post(f"{BASE_URL}/api/customers/import/json", data=form) as resp: |
| 97 | result = await resp.json() |
| 98 | print(f"✅ JSON import with hooks: {result}") |
| 99 | assert result['hooks_executed'] == True, "Hooks should be executed" |
| 100 | |
| 101 | async def test_json_import_skip_validation(): |
| 102 | """Test JSON import with validation skipped but other hooks enabled""" |
| 103 | print("\n🧪 Testing JSON import with skip_validation=true...") |
| 104 | |
| 105 | # JSON data with potentially invalid email |
| 106 | json_data = { |
| 107 | "resource": "customers", |
| 108 | "exported_at": "2025-09-19T18:00:00", |
| 109 | "records": [ |
| 110 | { |
| 111 | "name": "Skip Validation User", |
| 112 | "email": "invalid-email-format", # Normally would fail validation |
| 113 | "phone": "555-3001", |
| 114 | "credit_limit": "5000.00", |
| 115 | "status": "active" |
| 116 | } |
| 117 | ] |
| 118 | } |
| 119 | |
| 120 | async with aiohttp.ClientSession() as session: |
| 121 | # Import with validation skipped |
| 122 | form = aiohttp.FormData() |
| 123 | form.add_field('file', json.dumps(json_data), filename='test.json', content_type='application/json') |
| 124 | |
| 125 | async with session.post( |
| 126 | f"{BASE_URL}/api/customers/import/json?skip_validation=true", |
| 127 | data=form |
| 128 | ) as resp: |
| 129 | result = await resp.json() |
| 130 | print(f"✅ JSON import skip validation: {result}") |
| 131 | assert result['hooks_executed'] == True, "Hooks should still be executed" |
| 132 | # Non-validation hooks still run, just validation is skipped |
| 133 | |
| 134 | async def test_get_imported_records(): |
| 135 | """Verify imported records""" |
| 136 | print("\n🧪 Verifying imported records...") |
| 137 | |
| 138 | async with aiohttp.ClientSession() as session: |
| 139 | async with session.get(f"{BASE_URL}/api/customers") as resp: |
| 140 | data = await resp.json() |
| 141 | print(f"✅ Total records in database: {data['total']}") |
| 142 | |
| 143 | # Check for records from different import methods |
| 144 | names = [r['name'] for r in data['records']] |
| 145 | |
| 146 | csv_with_hooks = any('Test User' in n for n in names) |
| 147 | csv_without_hooks = any('No Hook User' in n for n in names) |
| 148 | json_with_hooks = any('JSON User' in n for n in names) |
| 149 | json_skip_validation = any('Skip Validation User' in n for n in names) |
| 150 | |
| 151 | print(f" - CSV import with hooks: {'✓' if csv_with_hooks else '✗'}") |
| 152 | print(f" - CSV import without hooks: {'✓' if csv_without_hooks else '✗'}") |
| 153 | print(f" - JSON import with hooks: {'✓' if json_with_hooks else '✗'}") |
| 154 | print(f" - JSON import skip validation: {'✓' if json_skip_validation else '✗'}") |
| 155 | |
| 156 | async def test_export_and_reimport(): |
| 157 | """Test export and re-import cycle""" |
| 158 | print("\n🧪 Testing export/re-import cycle...") |
| 159 | |
| 160 | async with aiohttp.ClientSession() as session: |
| 161 | # Export current data as CSV |
| 162 | async with session.get(f"{BASE_URL}/api/customers/export/csv") as resp: |
| 163 | csv_content = await resp.text() |
| 164 | print(f"✅ Exported {len(csv_content.splitlines())-1} records as CSV") |
| 165 | |
| 166 | # Export current data as JSON |
| 167 | async with session.get(f"{BASE_URL}/api/customers/export/json") as resp: |
| 168 | json_backup = await resp.json() |
| 169 | record_count = len(json_backup['records']) |
| 170 | print(f"✅ Exported {record_count} records as JSON backup") |
| 171 | |
| 172 | # Clear database |
| 173 | await session.delete(f"{BASE_URL}/api/customers/bulk") |
| 174 | print(f"✅ Cleared database") |
| 175 | |
| 176 | # Re-import JSON backup with hooks |
| 177 | form = aiohttp.FormData() |
| 178 | form.add_field('file', json.dumps(json_backup), filename='backup.json', content_type='application/json') |
| 179 | |
| 180 | async with session.post(f"{BASE_URL}/api/customers/import/json", data=form) as resp: |
| 181 | result = await resp.json() |
| 182 | print(f"✅ Re-imported backup: {result['imported_records']} records") |
| 183 | assert result['imported_records'] == record_count, "Should restore all records" |
| 184 | |
| 185 | async def main(): |
| 186 | """Run all tests""" |
| 187 | print("=" * 60) |
| 188 | print("🚀 Testing Import/Export with Hook Execution") |
| 189 | print("=" * 60) |
| 190 | |
| 191 | try: |
| 192 | # Test CSV imports |
| 193 | await test_csv_import_with_hooks() |
| 194 | await test_csv_import_without_hooks() |
| 195 | |
| 196 | # Test JSON imports |
| 197 | await test_json_import_with_hooks() |
| 198 | await test_json_import_skip_validation() |
| 199 | |
| 200 | # Verify imported records |
| 201 | await test_get_imported_records() |
| 202 | |
| 203 | # Test export/re-import cycle |
| 204 | await test_export_and_reimport() |
| 205 | |
| 206 | print("\n" + "=" * 60) |
| 207 | print("✅ All import/export tests passed!") |
| 208 | print("=" * 60) |
| 209 | |
| 210 | except Exception as e: |
| 211 | print(f"\n❌ Test failed: {e}") |
| 212 | import traceback |
| 213 | traceback.print_exc() |
| 214 | sys.exit(1) |
| 215 | |
| 216 | if __name__ == "__main__": |
| 217 | asyncio.run(main()) |