openforce/tests/test_rollup_engine.py
Goose cbcc16d7e1
Some checks failed
Build, Test, and Deploy / build-and-deploy (push) Failing after 40s
feat(formulas): implement roll-up summary engine and frontend formula editor (Sprint 4.5C & 4.5D)
- Add custom_objects/rollup_engine.py supporting COUNT, SUM, MIN, MAX aggregations across child relationships with optional JSON filter criteria.
- Wire post_save and post_delete reverse relation signal handlers in custom_objects/signals.py for Opportunity, Contact, and Activity relationships.
- Add frontend formula editor & roll-up configurator in CustomObjectsView.vue with insert helpers (fields, operators, functions) and Check Syntax validation.
- Add validateFormula action to frontend/src/stores/crm.js.
- Add unit and integration tests in tests/test_rollup_engine.py with 100% pass rate.
2026-09-27 20:31:27 +00:00

84 lines
3.3 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