dy-dashboard/tests/test_brand_management_delete.py
2026-10-12 10:18:15 +08:00

62 lines
2.4 KiB
Python

"""品牌信息管理删除功能回归测试。"""
import unittest
from unittest import mock
import test_wsgi # noqa: F401
from app import app
from routes import products
class BrandManagementDeleteTest(unittest.TestCase):
def setUp(self):
app.config.update(TESTING=True)
self.client = app.test_client()
with self.client.session_transaction() as session:
session.update(user_id=1, username='brand-delete-test', role='super_admin')
def test_delete_manual_brand(self):
with mock.patch.object(products, 'operation_required', return_value=True), \
mock.patch.object(products, 'query', side_effect=[
[{'cnt': 0}],
[{'brand': '误添加品牌', 'category': '国内'}],
0,
]) as query, \
mock.patch.object(products, 'add_operation_log') as operation_log:
response = self.client.post(
'/api/brand-management/delete', json={'brand': ' 误添加品牌 '}
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json()['code'], 0)
self.assertIn('DELETE FROM brand_information', query.call_args_list[2].args[0])
operation_log.assert_called_once()
def test_reject_delete_when_brand_is_used_by_products(self):
with mock.patch.object(products, 'operation_required', return_value=True), \
mock.patch.object(products, 'query', return_value=[{'cnt': 3}]) as query, \
mock.patch.object(products, 'add_operation_log') as operation_log:
response = self.client.post(
'/api/brand-management/delete', json={'brand': '在用品牌'}
)
body = response.get_json()
self.assertEqual(body['code'], 409)
self.assertEqual(body['product_count'], 3)
self.assertEqual(query.call_count, 1)
operation_log.assert_not_called()
def test_reject_delete_without_edit_permission(self):
with mock.patch.object(products, 'operation_required', return_value=False), \
mock.patch.object(products, 'query') as query:
response = self.client.post(
'/api/brand-management/delete', json={'brand': '无权限品牌'}
)
self.assertEqual(response.get_json()['code'], 403)
query.assert_not_called()
if __name__ == '__main__':
unittest.main()