81 lines
3.3 KiB
Python
81 lines
3.3 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(body['usage_counts'], {'未分类': 3})
|
|
self.assertIn('产品信息管理基础表', body['msg'])
|
|
self.assertEqual(query.call_count, 1)
|
|
operation_log.assert_not_called()
|
|
|
|
def test_delete_usage_message_lists_product_type_breakdown(self):
|
|
with mock.patch.object(products, 'operation_required', return_value=True), \
|
|
mock.patch.object(products, 'query', return_value=[
|
|
{'product_type': '包材', 'cnt': 5990},
|
|
{'product_type': '其他', 'cnt': 66},
|
|
{'product_type': '成品', 'cnt': 3},
|
|
]):
|
|
response = self.client.post(
|
|
'/api/brand-management/delete', json={'brand': '无'}
|
|
)
|
|
|
|
body = response.get_json()
|
|
self.assertEqual(body['code'], 409)
|
|
self.assertEqual(body['product_count'], 6059)
|
|
self.assertEqual(body['usage_counts']['包材'], 5990)
|
|
self.assertIn('包材5990条', body['msg'])
|
|
|
|
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()
|