-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_propertyradar_import.py
More file actions
187 lines (157 loc) · 7.95 KB
/
test_propertyradar_import.py
File metadata and controls
187 lines (157 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
"""
Quick test script to verify PropertyRadar CSV import functionality.
Run with: docker-compose exec web python test_propertyradar_import.py
"""
import sys
import os
from io import BytesIO
from werkzeug.datastructures import FileStorage
# Add app directory to path
sys.path.insert(0, '/app')
from app import create_app
from extensions import db
from services.propertyradar_import_service import PropertyRadarImportService
from services.csv_import_service import CSVImportService
from repositories.property_repository import PropertyRepository
from repositories.contact_repository import ContactRepository
from repositories.csv_import_repository import CSVImportRepository
from repositories.contact_csv_import_repository import ContactCSVImportRepository
from repositories.campaign_list_repository import CampaignListRepository
from repositories.campaign_list_member_repository import CampaignListMemberRepository
from services.contact_service_refactored import ContactService
from crm_database import Property, Contact, PropertyContact
def test_propertyradar_import():
"""Test importing PropertyRadar CSV with all fields"""
# Create Flask app context
app = create_app()
with app.app_context():
print("\n" + "="*60)
print("PROPERTYRADAR CSV IMPORT TEST")
print("="*60)
# Initialize repositories
property_repo = PropertyRepository(db.session)
contact_repo = ContactRepository(db.session)
csv_import_repo = CSVImportRepository(db.session)
contact_csv_import_repo = ContactCSVImportRepository(db.session)
campaign_list_repo = CampaignListRepository(db.session)
campaign_list_member_repo = CampaignListMemberRepository(db.session)
# Initialize contact service (check what parameters it actually needs)
try:
# Try with just the contact repository
contact_service = ContactService(contact_repo)
except Exception as e:
print(f"Note: ContactService initialization: {e}")
# Try alternative initialization
contact_service = None
# Initialize CSV import service
csv_service = CSVImportService(
csv_import_repository=csv_import_repo,
contact_csv_import_repository=contact_csv_import_repo,
campaign_list_repository=campaign_list_repo,
campaign_list_member_repository=campaign_list_member_repo,
contact_repository=contact_repo,
contact_service=contact_service
)
# Read the CSV file
csv_path = '/app/csvs/short-csv.csv'
if not os.path.exists(csv_path):
print(f"❌ CSV file not found: {csv_path}")
print("Please copy the file with: docker cp csvs/short-csv.csv crm_web_app:/app/csvs/short-csv.csv")
return False
print(f"✅ Found CSV file: {csv_path}")
# Create FileStorage object from CSV content
with open(csv_path, 'rb') as f:
csv_content = f.read()
file_storage = FileStorage(
stream=BytesIO(csv_content),
filename='short-csv.csv',
content_type='text/csv'
)
# Count initial records
initial_properties = db.session.query(Property).count()
initial_contacts = db.session.query(Contact).count()
initial_associations = db.session.query(PropertyContact).count()
print(f"\nInitial counts:")
print(f" Properties: {initial_properties}")
print(f" Contacts: {initial_contacts}")
print(f" Associations: {initial_associations}")
# Import the CSV using the CSV service (which should delegate to PropertyRadar)
print("\n📥 Importing CSV...")
result = csv_service.import_csv(file_storage, list_name="PropertyRadar Test Import")
# Check results
if result['success']:
print(f"✅ Import successful!")
print(f" Imported: {result['imported']}")
print(f" Updated: {result['updated']}")
print(f" List ID: {result['list_id']}")
print(f" Message: {result['message']}")
else:
print(f"❌ Import failed!")
print(f" Message: {result['message']}")
print(f" Errors: {result['errors'][:5]}") # Show first 5 errors
return False
# Count final records
final_properties = db.session.query(Property).count()
final_contacts = db.session.query(Contact).count()
final_associations = db.session.query(PropertyContact).count()
print(f"\nFinal counts:")
print(f" Properties: {final_properties} (+{final_properties - initial_properties})")
print(f" Contacts: {final_contacts} (+{final_contacts - initial_contacts})")
print(f" Associations: {final_associations} (+{final_associations - initial_associations})")
# Verify some specific data
print("\n🔍 Verifying imported data...")
# Check first property
first_property = db.session.query(Property).filter_by(
address='455 MIDDLE ST',
zip_code='02184'
).first()
if first_property:
print(f"\n✅ Found property: {first_property.address}")
print(f" City: {first_property.city}")
print(f" ZIP: {first_property.zip_code}")
print(f" Type: {first_property.property_type}")
print(f" Year Built: {first_property.year_built}")
print(f" Square Feet: {first_property.square_feet}")
print(f" Bedrooms: {first_property.bedrooms}")
print(f" Bathrooms: {first_property.bathrooms}")
print(f" Est Value: ${first_property.estimated_value:,.0f}" if first_property.estimated_value else " Est Value: N/A")
print(f" Est Equity: ${first_property.estimated_equity:,.0f}" if first_property.estimated_equity else " Est Equity: N/A")
print(f" Owner Occupied: {first_property.owner_occupied}")
print(f" APN: {first_property.apn}")
# Check associated contacts
associations = db.session.query(PropertyContact).filter_by(property_id=first_property.id).all()
print(f"\n Associated Contacts: {len(associations)}")
for assoc in associations:
contact = db.session.query(Contact).get(assoc.contact_id)
if contact:
print(f" - {contact.first_name} {contact.last_name}")
print(f" Phone: {contact.phone}")
print(f" Email: {contact.email}")
print(f" Primary: {assoc.is_primary}")
else:
print("❌ Could not find first property (455 MIDDLE ST)")
# Check for dual contacts (property with both primary and secondary)
print("\n🔍 Checking dual contact import...")
property_with_dual = db.session.query(Property).filter_by(
address='455 MIDDLE ST' # This row has both JON and AIMEE
).first()
if property_with_dual:
assocs = db.session.query(PropertyContact).filter_by(
property_id=property_with_dual.id
).all()
if len(assocs) >= 2:
print(f"✅ Found property with {len(assocs)} contacts")
for assoc in assocs:
contact = db.session.query(Contact).get(assoc.contact_id)
if contact:
print(f" - {contact.first_name} {contact.last_name} (Primary: {assoc.is_primary})")
else:
print(f"⚠️ Property has only {len(assocs)} contact(s), expected 2")
print("\n" + "="*60)
print("TEST COMPLETE")
print("="*60)
return True
if __name__ == '__main__':
success = test_propertyradar_import()
sys.exit(0 if success else 1)