128 lines
4.9 KiB
Python
128 lines
4.9 KiB
Python
import pytest
|
|
from django.contrib.auth import get_user_model
|
|
from crm_core.models import Account, Opportunity, Contact, Activity
|
|
from custom_objects.models import CustomFieldDefinition
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
class TestRollupEngine:
|
|
def test_account_opportunity_rollups(self):
|
|
u = User.objects.create_user('rollup_user', 'ru@example.com', 'pass')
|
|
acc = Account.objects.create(name='Acme Corp', owner=u)
|
|
|
|
# Create Roll-Up: Total Opportunity Amount (SUM)
|
|
CustomFieldDefinition.objects.create(
|
|
entity_name='Account',
|
|
name='total_opp_amount__c',
|
|
label='Total Opp Amount',
|
|
field_type='rollup',
|
|
rollup_summary_object='Opportunity',
|
|
rollup_type='SUM',
|
|
rollup_field='amount'
|
|
)
|
|
|
|
# Create Roll-Up: Opportunity Count (COUNT)
|
|
CustomFieldDefinition.objects.create(
|
|
entity_name='Account',
|
|
name='opp_count__c',
|
|
label='Opportunity Count',
|
|
field_type='rollup',
|
|
rollup_summary_object='Opportunity',
|
|
rollup_type='COUNT'
|
|
)
|
|
|
|
# Create child opportunities
|
|
opp1 = Opportunity.objects.create(name='Opp 1', account=acc, amount=10000, stage='Prospecting', close_date='2026-12-31', owner=u)
|
|
acc.refresh_from_db()
|
|
|
|
assert acc.custom_fields.get('opp_count__c') == 1
|
|
assert float(acc.custom_fields.get('total_opp_amount__c')) == 10000.0
|
|
|
|
# Add second opportunity
|
|
opp2 = Opportunity.objects.create(name='Opp 2', account=acc, amount=25000, stage='Qualification', close_date='2026-12-31', owner=u)
|
|
acc.refresh_from_db()
|
|
|
|
assert acc.custom_fields.get('opp_count__c') == 2
|
|
assert float(acc.custom_fields.get('total_opp_amount__c')) == 35000.0
|
|
|
|
# Delete an opportunity
|
|
opp1.delete()
|
|
acc.refresh_from_db()
|
|
|
|
assert acc.custom_fields.get('opp_count__c') == 1
|
|
assert float(acc.custom_fields.get('total_opp_amount__c')) == 25000.0
|
|
|
|
def test_contact_opportunity_rollups(self):
|
|
u = User.objects.create_user('rollup_user2', 'ru2@example.com', 'pass')
|
|
acc = Account.objects.create(name='Acme Corp 2', owner=u)
|
|
contact = Contact.objects.create(first_name='John', last_name='Doe', email='john@example.com', account=acc, owner=u)
|
|
|
|
# Create Roll-Up: Total Opportunity Amount on Account with stage filter
|
|
CustomFieldDefinition.objects.create(
|
|
entity_name='Account',
|
|
name='won_opp_amount__c',
|
|
label='Won Opp Amount',
|
|
field_type='rollup',
|
|
rollup_summary_object='Opportunity',
|
|
rollup_type='SUM',
|
|
rollup_field='amount',
|
|
rollup_filter={'stage': 'Closed Won'}
|
|
)
|
|
|
|
opp1 = Opportunity.objects.create(name='Opp Won', account=acc, amount=5000, stage='Closed Won', close_date='2026-12-31', owner=u)
|
|
opp2 = Opportunity.objects.create(name='Opp Lost', account=acc, amount=3000, stage='Closed Lost', close_date='2026-12-31', owner=u)
|
|
acc.refresh_from_db()
|
|
|
|
assert float(acc.custom_fields.get('won_opp_amount__c')) == 5000.0
|
|
|
|
# Update opp2 to Closed Won
|
|
opp2.stage = 'Closed Won'
|
|
opp2.save()
|
|
acc.refresh_from_db()
|
|
assert float(acc.custom_fields.get('won_opp_amount__c')) == 8000.0
|
|
|
|
def test_custom_record_parent_rollup_and_early_exit(self):
|
|
from custom_objects.models import CustomObjectDefinition, CustomRecord
|
|
u = User.objects.create_user('ru_user3', 'ru3@example.com', 'p')
|
|
acc = Account.objects.create(name='Custom Child Parent Acc', owner=u)
|
|
|
|
child_cobj = CustomObjectDefinition.objects.create(
|
|
name='InvoiceItem__c',
|
|
label='Invoice Item',
|
|
created_by=u
|
|
)
|
|
|
|
# Roll-up on Account from CustomRecord child
|
|
CustomFieldDefinition.objects.create(
|
|
entity_name='Account',
|
|
name='total_invoiced__c',
|
|
label='Total Invoiced',
|
|
field_type='rollup',
|
|
rollup_summary_object='InvoiceItem__c',
|
|
rollup_type='SUM',
|
|
rollup_field='amount',
|
|
rollup_filter={'status': 'paid'}
|
|
)
|
|
|
|
item1 = CustomRecord.objects.create(
|
|
custom_object=child_cobj,
|
|
owner=u,
|
|
data={'account_id': acc.id, 'amount': 150.50, 'status': 'paid'}
|
|
)
|
|
|
|
acc.refresh_from_db()
|
|
assert float(acc.custom_fields.get('total_invoiced__c')) == 150.50
|
|
|
|
# Update unrelated field on child (should early exit and not trigger lock/save)
|
|
item1.data['notes'] = 'Unrelated update'
|
|
item1.save()
|
|
acc.refresh_from_db()
|
|
assert float(acc.custom_fields.get('total_invoiced__c')) == 150.50
|
|
|
|
# Update relevant field
|
|
item1.data['amount'] = 200.00
|
|
item1.save()
|
|
acc.refresh_from_db()
|
|
assert float(acc.custom_fields.get('total_invoiced__c')) == 200.00
|