1
0
forked from erp-dev/erp

feat: big

This commit is contained in:
2026-07-01 11:51:13 +08:00
parent 7c9b77afb0
commit 5170700234
28 changed files with 933 additions and 37 deletions

View File

@@ -142,4 +142,54 @@ class CostEntryModelTests(TestCase):
occurred_at=date(2026, 6, 1),
)
self.assertFalse(bool(entry.image1))
self.assertFalse(bool(entry.image2))
self.assertFalse(bool(entry.image2))
def test_formula_amount_calculated_on_create(self):
entry = cost_models.CostEntry.objects.create(
merchant=self.merchant,
category=self.category,
amount=None,
unit_amount=Decimal('120.00'),
quantity=Decimal('2.5'),
unit_name='人天',
occurred_at=date(2026, 6, 1),
)
self.assertEqual(entry.amount, Decimal('300.00'))
self.assertEqual(entry.unit_name, '人天')
def test_formula_amount_recalculated_on_update(self):
entry = cost_models.CostEntry.objects.create(
merchant=self.merchant,
category=self.category,
amount=Decimal('300.00'),
unit_amount=Decimal('100.00'),
quantity=Decimal('3'),
occurred_at=date(2026, 6, 1),
)
entry.quantity = Decimal('4')
entry.amount = Decimal('999.00')
entry.save()
entry.refresh_from_db()
self.assertEqual(entry.amount, Decimal('400.00'))
def test_formula_partial_fields_raise_validation_error(self):
with self.assertRaises(ValidationError) as ctx:
cost_models.CostEntry.objects.create(
merchant=self.merchant,
category=self.category,
amount=None,
unit_amount=Decimal('100.00'),
quantity=None,
occurred_at=date(2026, 6, 1),
)
self.assertIn('quantity', ctx.exception.message_dict)
def test_manual_amount_required_without_formula(self):
with self.assertRaises(ValidationError) as ctx:
cost_models.CostEntry.objects.create(
merchant=self.merchant,
category=self.category,
amount=None,
occurred_at=date(2026, 6, 1),
)
self.assertIn('amount', ctx.exception.message_dict)

View File

@@ -171,6 +171,33 @@ class CreateCostEntryTests(TestCase):
self.assertEqual(entry.source_id, '')
self.assertEqual(entry.remarks, '')
def test_create_cost_entry_with_formula_fields(self):
"""传入 unit_amount + quantity 时自动计算最终 amount"""
entry = cost_services.create_cost_entry(
merchant=self.merchant,
category=self.category,
unit_amount=Decimal('180.00'),
quantity=Decimal('2'),
unit_name='人天',
occurred_at=date(2026, 6, 4),
)
self.assertEqual(entry.amount, Decimal('360.00'))
self.assertEqual(entry.unit_amount, Decimal('180.0000'))
self.assertEqual(entry.quantity, Decimal('2.0000'))
self.assertEqual(entry.unit_name, '人天')
def test_create_cost_entry_formula_overrides_amount(self):
"""倍数型支出以公式结果作为最终统计金额"""
entry = cost_services.create_cost_entry(
merchant=self.merchant,
category=self.category,
amount=Decimal('999.00'),
unit_amount=Decimal('120.00'),
quantity=Decimal('3'),
occurred_at=date(2026, 6, 4),
)
self.assertEqual(entry.amount, Decimal('360.00'))
class MockCostProvider:
"""测试用 Provider"""
@@ -222,6 +249,33 @@ class CollectFromProviderTests(TestCase):
self.assertEqual(cost_models.CostEntry.objects.count(), 2)
self.assertEqual(cost_models.CostCategory.objects.count(), 2)
def test_collect_creates_formula_entries(self):
"""Provider 可以提供 unit_amount + quantity由 cost 模块计算 amount"""
entries = [
CostEntryInput(
category_key='temp_worker', category_name='临时工工资',
amount=None, unit_amount=Decimal('200.00'), quantity=Decimal('3'),
unit_name='人天', occurred_at=date(2026, 6, 3),
source_module='test', source_id='T001',
),
]
provider = MockCostProvider(entries)
result = cost_services.collect_from_provider(
provider=provider,
merchant=self.merchant,
start_date=date(2026, 6, 1),
end_date=date(2026, 6, 30),
)
self.assertEqual(result['created_count'], 1)
entry = cost_models.CostEntry.objects.get()
self.assertEqual(entry.amount, Decimal('600.00'))
self.assertEqual(entry.unit_amount, Decimal('200.0000'))
self.assertEqual(entry.quantity, Decimal('3.0000'))
self.assertEqual(entry.unit_name, '人天')
def test_collect_returns_not_list_raises(self):
"""Provider 返回非 list 抛 TypeError"""
class BadProvider: