test: 补充订单业务测试和数据库验证
This commit is contained in:
1 parent
7dfa603ba8
commit
50f405932b
13 files changed
+421
-20
No files matched your search
@@ -0,0 +1,109 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { BadRequestException, NotFoundException } from "@nestjs/common";
|
||||
import { BookingService } from "../src/booking.service.js";
|
||||
|
||||
const brandId = "brand-1";
|
||||
const customerId = "customer-1";
|
||||
const service = {
|
||||
id: "service-1",
|
||||
name: "套餐",
|
||||
bookingEnabled: true,
|
||||
priceAmount: 2800,
|
||||
};
|
||||
|
||||
function createPrisma(overrides: Record<string, unknown> = {}): any {
|
||||
return {
|
||||
service: {
|
||||
findMany: async () => [service],
|
||||
},
|
||||
booking: {
|
||||
create: async ({ data }: any) => ({
|
||||
bookingNo: "AC-test",
|
||||
status: "received",
|
||||
estimatedAmount: data.estimatedAmount,
|
||||
createdAt: new Date(),
|
||||
}),
|
||||
findFirst: async () => null,
|
||||
update: async ({ data }: any) => ({ status: data.status }),
|
||||
findMany: async () => [],
|
||||
count: async () => 0,
|
||||
},
|
||||
tag: { findMany: async () => [] },
|
||||
bookingTag: { deleteMany: {}, createMany: {} },
|
||||
$transaction: async () => undefined,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
test("创建普通套餐订单时保存价格快照和预计金额", async () => {
|
||||
const prisma = createPrisma();
|
||||
const result = await new BookingService(prisma).create(brandId, customerId, {
|
||||
requestType: "package",
|
||||
customerName: "陈同学",
|
||||
wechatId: "wx-test",
|
||||
contactPreference: "wechat",
|
||||
items: [{ serviceId: service.id, quantity: 2 }],
|
||||
});
|
||||
assert.equal(result.estimatedAmount, 5600);
|
||||
assert.equal(result.status, "received");
|
||||
});
|
||||
|
||||
test("普通套餐不能在没有明细时创建", async () => {
|
||||
await assert.rejects(
|
||||
() =>
|
||||
new BookingService(createPrisma()).create(brandId, customerId, {
|
||||
requestType: "package",
|
||||
customerName: "陈同学",
|
||||
phone: "13800000000",
|
||||
contactPreference: "phone",
|
||||
}),
|
||||
(error: unknown) => error instanceof BadRequestException,
|
||||
);
|
||||
});
|
||||
|
||||
test("客户只能取消自己的 received 订单", async () => {
|
||||
const prisma = createPrisma({
|
||||
booking: {
|
||||
findFirst: async () => ({ id: "booking-1", status: "confirmed" }),
|
||||
update: async () => ({ status: "cancelled" }),
|
||||
},
|
||||
});
|
||||
await assert.rejects(
|
||||
() => new BookingService(prisma).cancelForCustomer(brandId, customerId, "booking-1"),
|
||||
(error: unknown) => error instanceof BadRequestException,
|
||||
);
|
||||
});
|
||||
|
||||
test("取消不存在或不属于当前客户的订单返回不存在", async () => {
|
||||
await assert.rejects(
|
||||
() => new BookingService(createPrisma()).cancelForCustomer(brandId, customerId, "missing"),
|
||||
(error: unknown) => error instanceof NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
test("订单状态只能按定义的状态机流转", async () => {
|
||||
const prisma = createPrisma({
|
||||
booking: {
|
||||
findFirst: async () => ({ id: "booking-1", status: "received" }),
|
||||
update: async ({ data }: any) => data,
|
||||
},
|
||||
});
|
||||
const bookings = new BookingService(prisma);
|
||||
assert.equal((await bookings.updateStatus(brandId, "booking-1", "contacting")).status, "contacting");
|
||||
await assert.rejects(
|
||||
() => bookings.updateStatus(brandId, "booking-1", "completed"),
|
||||
(error: unknown) => error instanceof BadRequestException,
|
||||
);
|
||||
});
|
||||
|
||||
test("更新订单标签时拒绝其他品牌的标签", async () => {
|
||||
const prisma = createPrisma({
|
||||
booking: { findFirst: async () => ({ id: "booking-1", brandId }) },
|
||||
tag: { findMany: async () => [] },
|
||||
});
|
||||
await assert.rejects(
|
||||
() => new BookingService(prisma).updateTags(brandId, "booking-1", ["foreign-tag"]),
|
||||
(error: unknown) => error instanceof BadRequestException,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"noEmit": true,
|
||||
"rootDir": ".."
|
||||
},
|
||||
"include": ["./**/*.ts"]
|
||||
}
|
||||
Reference in new issue
Block a user