129 lines
5.5 KiB
Python
129 lines
5.5 KiB
Python
"""
|
|
تستهای قرارداد مشتری: افزودن، ویرایش و نگهبان حذف (عدم حذف مشتری دارای سابقه).
|
|
"""
|
|
from django.test import TestCase
|
|
from django.urls import reverse
|
|
|
|
from admin_panel.models import Article, Customer, PhoneNumber
|
|
|
|
from .factories import login_superuser, make_customer, make_user
|
|
|
|
|
|
class CustomerCrudTests(TestCase):
|
|
|
|
def setUp(self):
|
|
self.user = login_superuser(self.client, 'csUser')
|
|
|
|
def test_add_customer_creates_phones(self):
|
|
url = reverse('admin_panel:customersAdd')
|
|
response = self.client.post(url, {
|
|
'an_name': 'علی رضایی',
|
|
'an_address': 'تهران',
|
|
'installDate': '1400/01/01',
|
|
'idnCode': '1234567890',
|
|
'phone_titles': ['موبایل', 'ثابت', ''],
|
|
'phones': ['09120000000', '02112345678', ''],
|
|
})
|
|
|
|
self.assertIn(response.status_code, (200, 302))
|
|
customer = Customer.objects.get(name_family='علی رضایی')
|
|
self.assertEqual(customer.phone1, '09120000000')
|
|
self.assertEqual(customer.phone2, '02112345678')
|
|
# ردیف خالی نباید ذخیره شود
|
|
self.assertEqual(PhoneNumber.objects.filter(customer=customer).count(), 2)
|
|
|
|
def test_edit_customer_replaces_phones(self):
|
|
customer = make_customer('مشتری ویرایش', phones=['09120000001'])
|
|
url = reverse('admin_panel:customerEdit', kwargs={'c_id': customer.id})
|
|
|
|
response = self.client.post(url, {
|
|
'an_name': 'مشتری ویرایش شده',
|
|
'an_address': 'شیراز',
|
|
'installDate': '1401/02/03',
|
|
'idnCode': '999',
|
|
'laghv_bazdid': 'true',
|
|
'phone_titles': ['موبایل جدید'],
|
|
'phones': ['09120009999'],
|
|
})
|
|
self.assertIn(response.status_code, (200, 302))
|
|
|
|
customer.refresh_from_db()
|
|
self.assertEqual(customer.name_family, 'مشتری ویرایش شده')
|
|
self.assertTrue(customer.laghv_bazdid)
|
|
phones = list(PhoneNumber.objects.filter(customer=customer))
|
|
self.assertEqual(len(phones), 1)
|
|
self.assertEqual(phones[0].phone_number, '09120009999')
|
|
|
|
|
|
class InactiveCustomerHiddenTests(TestCase):
|
|
"""مشتری غیرفعال (is_active=False) در فهرست و در جستجو دیده نمیشود."""
|
|
|
|
def setUp(self):
|
|
self.user = login_superuser(self.client, 'hideUser')
|
|
self.active = make_customer('خطیبی فعال', phones=['09120000041'])
|
|
self.inactive = make_customer('خطیبی غیرفعال', is_active=False,
|
|
phones=['09120000042'])
|
|
# جستجوی تلفن از ستون phone1 میخواند (فیلد قدیمی مشتری).
|
|
Customer.objects.filter(pk=self.active.pk).update(phone1='09120000041')
|
|
Customer.objects.filter(pk=self.inactive.pk).update(phone1='09120000042')
|
|
|
|
def test_inactive_absent_from_default_listing(self):
|
|
html = self.client.get(reverse('admin_panel:customers')).content.decode('utf-8')
|
|
self.assertIn('خطیبی فعال', html)
|
|
self.assertNotIn('خطیبی غیرفعال', html)
|
|
|
|
def test_inactive_absent_from_search_by_name(self):
|
|
html = self.client.get(
|
|
reverse('admin_panel:customers'),
|
|
{'searchVal': 'خطیبی', 'searchOn': 'name'},
|
|
).content.decode('utf-8')
|
|
self.assertIn('خطیبی فعال', html)
|
|
self.assertNotIn('خطیبی غیرفعال', html)
|
|
|
|
def test_inactive_absent_from_search_by_phone(self):
|
|
html = self.client.get(
|
|
reverse('admin_panel:customers'),
|
|
{'searchVal': '0912000004', 'searchOn': 'phone'},
|
|
).content.decode('utf-8')
|
|
self.assertIn('09120000041', html)
|
|
self.assertNotIn('09120000042', html)
|
|
|
|
def test_inactive_absent_from_path_search_view(self):
|
|
# مسیر <slug:> فقط نویسههای لاتین میپذیرد.
|
|
make_customer('khatibi-active')
|
|
make_customer('khatibi-inactive', is_active=False)
|
|
html = self.client.get(
|
|
reverse('admin_panel:customersSearch',
|
|
kwargs={'searchVal': 'khatibi', 'searchOn': 'name'}),
|
|
).content.decode('utf-8')
|
|
self.assertIn('khatibi-active', html)
|
|
self.assertNotIn('khatibi-inactive', html)
|
|
|
|
|
|
class CustomerDeleteGuardTests(TestCase):
|
|
|
|
def setUp(self):
|
|
self.user = make_user('delUser', superuser=True)
|
|
self.client.force_login(self.user)
|
|
|
|
def test_customer_without_history_is_deactivated(self):
|
|
customer = make_customer('مشتری خام')
|
|
url = reverse('admin_panel:customerDel', kwargs={'c_id': customer.id})
|
|
|
|
self.client.get(url)
|
|
|
|
customer.refresh_from_db()
|
|
# حذف نرم: رکورد باقی میماند ولی غیرفعال میشود
|
|
self.assertFalse(customer.is_active)
|
|
|
|
def test_customer_with_financial_history_is_never_deleted(self):
|
|
customer = make_customer('مشتری دارای سابقه')
|
|
Article.objects.create(customer=customer, debit=1000, credit=0, desc='سابقه مالی')
|
|
|
|
url = reverse('admin_panel:customerDel', kwargs={'c_id': customer.id})
|
|
self.client.get(url)
|
|
|
|
customer.refresh_from_db()
|
|
self.assertTrue(customer.is_active, msg="مشتری دارای سابقه مالی نباید حذف/غیرفعال شود.")
|
|
self.assertTrue(Customer.objects.filter(id=customer.id).exists())
|