from django.test import TestCase from django.core.exceptions import ValidationError from .models import ( Merchant, MerchantTypeEnum, WareHouse, WarehouseTypeEnum, WareHouseModeEnum, EmployeeType, Employee, EmployeeStatusEnum ) class WarehouseTypeTestCase(TestCase): """测试仓库类型字段""" def setUp(self): """设置测试数据""" self.merchant = Merchant.objects.create( name='测试商户', type=MerchantTypeEnum.STORE ) def test_warehouse_default_type(self): """测试仓库类型的默认值""" warehouse = WareHouse.objects.create( merchant=self.merchant, name='默认仓库', ) self.assertEqual(warehouse.type, WarehouseTypeEnum.WHOLE) def test_warehouse_type_whole(self): """测试创建整仓""" warehouse = WareHouse.objects.create( merchant=self.merchant, name='整仓1', type=WarehouseTypeEnum.WHOLE ) self.assertEqual(warehouse.type, WarehouseTypeEnum.WHOLE) self.assertEqual(warehouse.get_type_display(), '整仓') def test_warehouse_type_scattered(self): """测试创建散仓""" warehouse = WareHouse.objects.create( merchant=self.merchant, name='散仓1', type=WarehouseTypeEnum.SCATTERED ) self.assertEqual(warehouse.type, WarehouseTypeEnum.SCATTERED) self.assertEqual(warehouse.get_type_display(), '散仓') def test_warehouse_type_enum_values(self): """测试仓库类型枚举的所有值""" self.assertEqual(WarehouseTypeEnum.WHOLE.value, 1) self.assertEqual(WarehouseTypeEnum.WHOLE.label, '整仓') self.assertEqual(WarehouseTypeEnum.SCATTERED.value, 2) self.assertEqual(WarehouseTypeEnum.SCATTERED.label, '散仓') def test_warehouse_with_all_fields(self): """测试带有所有字段的仓库""" warehouse = WareHouse.objects.create( merchant=self.merchant, name='完整仓库', type=WarehouseTypeEnum.SCATTERED, mode=WareHouseModeEnum.RESTRICT_IN_OUT, location='测试地址', area='测试区域', contact='张三', mobile='13800138000', description='测试描述' ) self.assertEqual(warehouse.type, WarehouseTypeEnum.SCATTERED) self.assertEqual(warehouse.mode, WareHouseModeEnum.RESTRICT_IN_OUT) self.assertEqual(warehouse.name, '完整仓库') class EmployeeTypeTestCase(TestCase): """测试员工职位类型""" def setUp(self): """设置测试数据""" self.merchant = Merchant.objects.create( name='测试商户', type=MerchantTypeEnum.STORE ) def test_create_employee_type(self): """测试创建员工职位类型""" emp_type = EmployeeType.objects.create( merchant=self.merchant, title='测试职位', description='职位描述' ) self.assertEqual(emp_type.title, '测试职位') self.assertEqual(emp_type.description, '职位描述') self.assertEqual(str(emp_type), '测试职位') def test_employee_type_unique_together(self): """测试商户+职位名称的唯一性约束""" EmployeeType.objects.create( merchant=self.merchant, title='打纸' ) # 同一商户不能创建重复职位 with self.assertRaises(Exception): EmployeeType.objects.create( merchant=self.merchant, title='打纸' ) def test_employee_with_position(self): """测试员工关联职位""" emp_type = EmployeeType.objects.create( merchant=self.merchant, title='滚筒工' ) employee = Employee.objects.create( merchant=self.merchant, name='张三', position=emp_type, status=EmployeeStatusEnum.ACTIVE ) self.assertEqual(employee.position, emp_type) self.assertEqual(employee.job_type, '滚筒工') # 测试 job_type 属性 def test_employee_without_position(self): """测试员工没有职位时 job_type 返回空字符串""" employee = Employee.objects.create( merchant=self.merchant, name='李四', status=EmployeeStatusEnum.ACTIVE ) self.assertIsNone(employee.position) self.assertEqual(employee.job_type, '') def test_employee_type_reverse_field(self): """测试预留字段""" emp_type = EmployeeType.objects.create( merchant=self.merchant, title='仓库管理员', reverse='预留数据' ) self.assertEqual(emp_type.reverse, '预留数据') def test_multiple_employees_same_position(self): """测试多个员工可以有相同职位""" emp_type = EmployeeType.objects.create( merchant=self.merchant, title='打纸工' ) emp1 = Employee.objects.create( merchant=self.merchant, name='员工1', position=emp_type ) emp2 = Employee.objects.create( merchant=self.merchant, name='员工2', position=emp_type ) self.assertEqual(emp1.job_type, '打纸工') self.assertEqual(emp2.job_type, '打纸工') self.assertEqual(emp_type.employees.count(), 2)