refactor
This commit is contained in:
1 parent
6f36db63a1
commit
250cdbab7f
42 files changed
+96363
-11699
No files matched your search
@@ -0,0 +1,84 @@
|
||||
"""
|
||||
تستهای قرارداد مشتری: افزودن، ویرایش و نگهبان حذف (عدم حذف مشتری دارای سابقه).
|
||||
"""
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
from admin_panel.models import Article, Customer, PhoneNumber
|
||||
|
||||
from .factories import make_customer, make_user
|
||||
|
||||
|
||||
class CustomerCrudTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.user = make_user('csUser', superuser=True)
|
||||
self.client.force_login(self.user)
|
||||
|
||||
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 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())
|
||||
Reference in new issue
Block a user