69 lines
2.9 KiB
Python
69 lines
2.9 KiB
Python
import pytest
|
|
from django.contrib.auth.models import User
|
|
from django.test import Client
|
|
|
|
from finance.models import Dataset, Fact, Membership, Organization
|
|
|
|
|
|
@pytest.fixture
|
|
def workspace(tmp_path, settings):
|
|
settings.MEDIA_ROOT = tmp_path
|
|
org = Organization.objects.create(name="Finance test")
|
|
user = User.objects.create_user("finance-admin", password="Finance-TestPassword991!")
|
|
member = Membership.objects.create(user=user, organization=org, role="admin")
|
|
other_org = Organization.objects.create(name="Other organization")
|
|
other_user = User.objects.create_user("finance-other", password="Finance-TestPassword991!")
|
|
other_member = Membership.objects.create(user=other_user, organization=other_org, role="admin")
|
|
mapping = {
|
|
"sheet": "CSV",
|
|
"header": 1,
|
|
"columns": [
|
|
{"index": 0, "name": "期间", "role": "period"},
|
|
{"index": 1, "name": "科室", "role": "dimension", "key": "department"},
|
|
{"index": 2, "name": "产品一级分类", "role": "dimension", "key": "product"},
|
|
{"index": 3, "name": "实际收入", "role": "revenue"},
|
|
{"index": 4, "name": "成本", "role": "cost"},
|
|
{"index": 5, "name": "毛利贡献", "role": "gross"},
|
|
{"index": 6, "name": "客户编码", "role": "customer"},
|
|
{"index": 7, "name": "应收金额", "role": "list_amount"},
|
|
],
|
|
"roi_formula": "profit_over_cost",
|
|
"self_operated_products": ["医疗服务"],
|
|
}
|
|
ds = Dataset.objects.create(
|
|
organization=org,
|
|
created_by=user,
|
|
name="finance.csv",
|
|
sha256="a" * 64,
|
|
status="ready",
|
|
mapping=mapping,
|
|
periods=["2025-08", "2026-08"],
|
|
)
|
|
facts = [
|
|
("2025-08", "儿科", "医疗服务", 100, 40, 60, "C1", 120),
|
|
("2025-08", "儿科", "检查", 50, 20, 30, "C2", 60),
|
|
("2026-08", "儿科", "医疗服务", 80, 30, 50, "C1", 100),
|
|
("2026-08", "儿科", "医疗服务", 20, 10, 10, "C3", 25),
|
|
("2026-08", "外科", "检查", -10, 5, -15, "C4", 0),
|
|
]
|
|
for index, (period, department, product, revenue, cost, gross, customer, list_amount) in enumerate(
|
|
facts, 2
|
|
):
|
|
Fact.objects.create(
|
|
dataset=ds,
|
|
period=period,
|
|
dimensions={"department": department, "product": product},
|
|
revenue=revenue,
|
|
cost=cost,
|
|
gross=gross,
|
|
customer=customer,
|
|
list_amount=list_amount,
|
|
source_row=index,
|
|
raw=[period, department, product, revenue, cost, gross, customer, list_amount],
|
|
)
|
|
client = Client()
|
|
assert client.login(username=user.username, password="Finance-TestPassword991!")
|
|
other_client = Client()
|
|
assert other_client.login(username=other_user.username, password="Finance-TestPassword991!")
|
|
return ds, member, client, other_member, other_client
|