openforce/tests/crm/test_models.py
2026-09-26 07:31:43 +00:00

152 lines
6.3 KiB
Python

from decimal import Decimal
from datetime import date
from django.test import TestCase
from django.core.exceptions import ValidationError
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from crm_core.models import Account, Contact, Lead, Opportunity, Activity
from crm_core.services import convert_lead
User = get_user_model()
class TestAccountModel(TestCase):
def setUp(self):
self.user = User.objects.create_user('accuser', 'acc@test.com', 'pass')
def test_account_creation(self):
acc = Account.objects.create(name='Acme Corp', industry='Technology', owner=self.user)
self.assertEqual(str(acc), 'Acme Corp')
self.assertEqual(acc.industry, 'Technology')
def test_account_type_choices(self):
acc = Account.objects.create(name='Beta LLC', type='Customer', owner=self.user)
self.assertEqual(acc.type, 'Customer')
def test_account_parent_relationship(self):
p = Account.objects.create(name='Parent Corp', owner=self.user)
c = Account.objects.create(name='Child Corp', parent=p, owner=self.user)
self.assertEqual(c.parent, p)
class TestContactModel(TestCase):
def setUp(self):
self.user = User.objects.create_user('conuser', 'con@test.com', 'pass')
self.acc = Account.objects.create(name='Acme Corp', owner=self.user)
def test_contact_str(self):
con = Contact.objects.create(first_name='John', last_name='Doe', email='john@acme.com', account=self.acc, owner=self.user)
self.assertEqual(str(con), 'John Doe (john@acme.com)')
def test_contact_email_format_validation(self):
con = Contact(first_name='Invalid', last_name='Email', email='not-an-email', account=self.acc)
with self.assertRaises(ValidationError):
con.full_clean()
class TestLeadModel(TestCase):
def setUp(self):
self.user = User.objects.create_user('leaduser', 'lead@test.com', 'pass')
def test_lead_default_status_is_new(self):
lead = Lead.objects.create(first_name='Alice', last_name='Smith', company='Initech', owner=self.user)
self.assertEqual(lead.status, 'New')
def test_lead_is_converted_defaults_false(self):
lead = Lead.objects.create(first_name='Bob', last_name='Jones', company='Stark', owner=self.user)
self.assertFalse(lead.is_converted)
def test_lead_str(self):
lead = Lead.objects.create(first_name='Alice', last_name='Smith', company='Initech', owner=self.user)
self.assertEqual(str(lead), 'Alice Smith - Initech')
def test_converted_lead_has_converted_account(self):
lead = Lead.objects.create(first_name='Charlie', last_name='Brown', company='Peanuts', owner=self.user)
acc, con, opp = convert_lead(lead)
lead.refresh_from_db()
self.assertTrue(lead.is_converted)
self.assertEqual(lead.converted_account, acc)
self.assertEqual(lead.converted_contact, con)
class TestOpportunityModel(TestCase):
def setUp(self):
self.user = User.objects.create_user('oppuser', 'opp@test.com', 'pass')
self.acc = Account.objects.create(name='Big Corp', owner=self.user)
def test_amount_non_negative(self):
opp = Opportunity(name='Bad Opp', account=self.acc, stage='Prospecting', amount=Decimal('-500.00'), close_date=date.today())
with self.assertRaises(ValidationError):
opp.full_clean()
def test_close_date_required(self):
opp = Opportunity(name='No Close', account=self.acc, stage='Prospecting', amount=Decimal('100.00'), close_date=None)
with self.assertRaises(ValidationError):
opp.full_clean()
def test_probability_range_0_to_100(self):
opp = Opportunity(name='High Prob', account=self.acc, stage='Prospecting', amount=Decimal('100.00'), probability=150, close_date=date.today())
with self.assertRaises(ValidationError):
opp.full_clean()
def test_stage_choices_exhaustive(self):
valid_stages = [
'Prospecting', 'Qualification', 'Needs Analysis', 'Value Proposition',
'Id. Decision Makers', 'Perception Analysis', 'Proposal/Price Quote',
'Negotiation/Review', 'Closed Won', 'Closed Lost'
]
for stg in valid_stages:
opp = Opportunity(name=f'Opp {stg}', account=self.acc, stage=stg, amount=Decimal('1000.00'), close_date=date.today(), owner=self.user)
opp.full_clean()
opp.save()
self.assertEqual(opp.stage, stg)
class TestLeadConversionTransaction(TestCase):
def setUp(self):
self.user = User.objects.create_user('transuser', 'trans@test.com', 'pass')
self.lead = Lead.objects.create(
first_name='Bruce', last_name='Wayne', company='Wayne Enterprises',
email='bruce@wayne.com', owner=self.user
)
def test_cannot_convert_already_converted_lead(self):
convert_lead(self.lead)
with self.assertRaises(ValidationError):
convert_lead(self.lead)
def test_conversion_creates_account_and_contact(self):
acc, con, opp = convert_lead(self.lead)
self.assertEqual(acc.name, 'Wayne Enterprises')
self.assertEqual(con.first_name, 'Bruce')
self.assertEqual(con.last_name, 'Wayne')
self.assertEqual(con.account, acc)
self.assertIsNone(opp)
def test_conversion_with_opportunity(self):
acc, con, opp = convert_lead(
self.lead,
create_opportunity=True,
opportunity_name='Batmobile Deal',
opportunity_amount=Decimal('500000.00'),
opportunity_close_date=date(2026, 12, 31)
)
self.assertIsNotNone(opp)
self.assertEqual(opp.name, 'Batmobile Deal')
self.assertEqual(opp.amount, Decimal('500000.00'))
self.assertEqual(opp.account, acc)
def test_activities_repointed_on_conversion(self):
lead_ct = ContentType.objects.get_for_model(Lead)
act = Activity.objects.create(
subject='Initial pitch',
activity_type='Call',
content_type=lead_ct,
object_id=self.lead.id,
assigned_to=self.user
)
acc, con, opp = convert_lead(self.lead)
act.refresh_from_db()
contact_ct = ContentType.objects.get_for_model(Contact)
self.assertEqual(act.content_type, contact_ct)
self.assertEqual(act.object_id, con.id)