集成测试
集成测试侧重于测试程序各独立部分如何协同工作。在使用数据库的应用程序中,集成测试通常需要数据库可用并包含便于测试场景所需的数据。
一种模拟真实世界环境的方法是使用 Docker 来封装数据库和一些测试数据。这样可以在测试时启动和销毁,从而作为独立于生产数据库的环境运行。
注意:这篇博客文章提供了关于如何设置集成测试环境以及如何针对真实数据库编写集成测试的全面指南,为希望探索此主题的人提供了宝贵的见解。
先决条件
本指南假设您的机器上已安装 Docker 和 Docker Compose,并且您的项目中已设置 Jest
。
本指南将全程使用以下电子商务 schema。这与文档其他部分使用的传统 User
和 Post
模型有所不同,主要是因为您不太可能针对您的博客运行集成测试。
电子商务 schema
// Can have 1 customer
// Can have many order details
model CustomerOrder {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
customer Customer @relation(fields: [customerId], references: [id])
customerId Int
orderDetails OrderDetails[]
}
// Can have 1 order
// Can have many products
model OrderDetails {
id Int @id @default(autoincrement())
products Product @relation(fields: [productId], references: [id])
productId Int
order CustomerOrder @relation(fields: [orderId], references: [id])
orderId Int
total Decimal
quantity Int
}
// Can have many order details
// Can have 1 category
model Product {
id Int @id @default(autoincrement())
name String
description String
price Decimal
sku Int
orderDetails OrderDetails[]
category Category @relation(fields: [categoryId], references: [id])
categoryId Int
}
// Can have many products
model Category {
id Int @id @default(autoincrement())
name String
products Product[]
}
// Can have many orders
model Customer {
id Int @id @default(autoincrement())
email String @unique
address String?
name String?
orders CustomerOrder[]
}
本指南使用单例模式设置 Prisma Client。请参阅单例模式文档了解如何进行设置。
将 Docker 添加到您的项目
在您的机器上安装 Docker 和 Docker compose 后,您可以在项目中使用它们。
- 首先在项目根目录下创建
docker-compose.yml
文件。您将在此处添加一个 Postgres 镜像并指定环境凭据。
# Set the version of docker compose to use
version: '3.9'
# The containers that compose the project
services:
db:
image: postgres:13
restart: always
container_name: integration-tests-prisma
ports:
- '5433:5432'
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
POSTGRES_DB: tests
注意:此处使用的 compose 版本(
3.9
)在撰写本文时是最新版本,如果您正在按照本指南操作,请务必使用相同版本以保持一致性。
docker-compose.yml
文件定义了以下内容
- Postgres 镜像(
postgres
)和版本标签(:13
)。如果您的本地没有此镜像,将下载它。 - 端口
5433
映射到内部(Postgres 默认)端口5432
。这将是数据库在外部公开的端口号。 - 设置数据库用户凭据并为数据库命名。
- 要连接到容器中的数据库,请使用
docker-compose.yml
文件中定义的凭据创建一个新的连接字符串。例如
DATABASE_URL="postgresql://prisma:prisma@localhost:5433/tests"
上述 .env.test
文件是多 .env
文件设置的一部分。查阅使用多个 .env 文件部分,了解如何使用多个 .env
文件设置项目。
- 要在分离状态下创建容器,以便您可以继续使用终端选项卡,请运行以下命令
docker compose up -d
-
接下来,您可以通过在容器内部执行
psql
命令来检查数据库是否已创建。记下容器 ID。docker ps
显示CLI结果CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1322e42d833f postgres:13 "docker-entrypoint.s…" 2 seconds ago Up 1 second 0.0.0.0:5433->5432/tcp integration-tests-prisma
注意:容器 ID 对于每个容器都是唯一的,您将看到一个不同的 ID。
-
使用上一步的容器 ID,在容器中运行
psql
,使用创建的用户登录并检查数据库是否已创建docker exec -it 1322e42d833f psql -U prisma tests
显示CLI结果tests=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
postgres | prisma | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | prisma | UTF8 | en_US.utf8 | en_US.utf8 | =c/prisma +
| | | | | prisma=CTc/prisma
template1 | prisma | UTF8 | en_US.utf8 | en_US.utf8 | =c/prisma +
| | | | | prisma=CTc/prisma
tests | prisma | UTF8 | en_US.utf8 | en_US.utf8 |
(4 rows)
集成测试
集成测试将在专用测试环境中针对数据库运行,而不是在生产或开发环境中运行。
操作流程
运行此类测试的流程如下
- 启动容器并创建数据库
- 迁移 schema
- 运行测试
- 销毁容器
每个测试套件在所有测试运行之前都会为数据库填充数据。套件中的所有测试完成后,所有表中的数据将被删除并终止连接。
要测试的函数
您正在测试的电子商务应用程序有一个创建订单的函数。此函数执行以下操作
- 接受关于创建订单的客户输入
- 接受关于订购产品的输入
- 检查客户是否已有账户
- 检查产品是否有库存
- 如果产品不存在,则返回“缺货”消息
- 如果客户在数据库中不存在,则创建账户
- 创建订单
下面可以看到此类函数可能的样子示例
import prisma from '../client'
export interface Customer {
id?: number
name?: string
email: string
address?: string
}
export interface OrderInput {
customer: Customer
productId: number
quantity: number
}
/**
* Creates an order with customer.
* @param input The order parameters
*/
export async function createOrder(input: OrderInput) {
const { productId, quantity, customer } = input
const { name, email, address } = customer
// Get the product
const product = await prisma.product.findUnique({
where: {
id: productId,
},
})
// If the product is null its out of stock, return error.
if (!product) return new Error('Out of stock')
// If the customer is new then create the record, otherwise connect via their unique email
await prisma.customerOrder.create({
data: {
customer: {
connectOrCreate: {
create: {
name,
email,
address,
},
where: {
email,
},
},
},
orderDetails: {
create: {
total: product.price,
quantity,
products: {
connect: {
id: product.id,
},
},
},
},
},
})
}
测试套件
以下测试将检查 createOrder
函数是否按预期工作。它们将测试
- 使用新客户创建新订单
- 使用现有客户创建订单
- 如果产品不存在,显示“缺货”错误消息
在测试套件运行之前,数据库会填充数据。测试套件完成后,使用 deleteMany
清空数据库中的数据。
在您预先知道 schema 结构的情况下,使用 deleteMany
可能就足够了。这是因为操作需要按照模型关系设置的方式以正确的顺序执行。
然而,这不如采用更通用的解决方案(该方案遍历您的模型并对其执行 truncate 操作)那样具有良好的扩展性。对于这些场景以及使用原始 SQL 查询的示例,请参阅使用原始 SQL / TRUNCATE
删除所有数据
import prisma from '../src/client'
import { createOrder, Customer, OrderInput } from '../src/functions/index'
beforeAll(async () => {
// create product categories
await prisma.category.createMany({
data: [{ name: 'Wand' }, { name: 'Broomstick' }],
})
console.log('✨ 2 categories successfully created!')
// create products
await prisma.product.createMany({
data: [
{
name: 'Holly, 11", phoenix feather',
description: 'Harry Potters wand',
price: 100,
sku: 1,
categoryId: 1,
},
{
name: 'Nimbus 2000',
description: 'Harry Potters broom',
price: 500,
sku: 2,
categoryId: 2,
},
],
})
console.log('✨ 2 products successfully created!')
// create the customer
await prisma.customer.create({
data: {
name: 'Harry Potter',
email: 'harry@hogwarts.io',
address: '4 Privet Drive',
},
})
console.log('✨ 1 customer successfully created!')
})
afterAll(async () => {
const deleteOrderDetails = prisma.orderDetails.deleteMany()
const deleteProduct = prisma.product.deleteMany()
const deleteCategory = prisma.category.deleteMany()
const deleteCustomerOrder = prisma.customerOrder.deleteMany()
const deleteCustomer = prisma.customer.deleteMany()
await prisma.$transaction([
deleteOrderDetails,
deleteProduct,
deleteCategory,
deleteCustomerOrder,
deleteCustomer,
])
await prisma.$disconnect()
})
it('should create 1 new customer with 1 order', async () => {
// The new customers details
const customer: Customer = {
id: 2,
name: 'Hermione Granger',
email: 'hermione@hogwarts.io',
address: '2 Hampstead Heath',
}
// The new orders details
const order: OrderInput = {
customer,
productId: 1,
quantity: 1,
}
// Create the order and customer
await createOrder(order)
// Check if the new customer was created by filtering on unique email field
const newCustomer = await prisma.customer.findUnique({
where: {
email: customer.email,
},
})
// Check if the new order was created by filtering on unique email field of the customer
const newOrder = await prisma.customerOrder.findFirst({
where: {
customer: {
email: customer.email,
},
},
})
// Expect the new customer to have been created and match the input
expect(newCustomer).toEqual(customer)
// Expect the new order to have been created and contain the new customer
expect(newOrder).toHaveProperty('customerId', 2)
})
it('should create 1 order with an existing customer', async () => {
// The existing customers email
const customer: Customer = {
email: 'harry@hogwarts.io',
}
// The new orders details
const order: OrderInput = {
customer,
productId: 1,
quantity: 1,
}
// Create the order and connect the existing customer
await createOrder(order)
// Check if the new order was created by filtering on unique email field of the customer
const newOrder = await prisma.customerOrder.findFirst({
where: {
customer: {
email: customer.email,
},
},
})
// Expect the new order to have been created and contain the existing customer with an id of 1 (Harry Potter from the seed script)
expect(newOrder).toHaveProperty('customerId', 1)
})
it("should show 'Out of stock' message if productId doesn't exit", async () => {
// The existing customers email
const customer: Customer = {
email: 'harry@hogwarts.io',
}
// The new orders details
const order: OrderInput = {
customer,
productId: 3,
quantity: 1,
}
// The productId supplied doesn't exit so the function should return an "Out of stock" message
await expect(createOrder(order)).resolves.toEqual(new Error('Out of stock'))
})
运行测试
此设置隔离了真实世界场景,以便您可以在受控环境中根据真实数据测试应用程序的功能。
您可以在项目的 package.json
文件中添加一些脚本,用于设置数据库和运行测试,然后手动销毁容器。
如果测试对您不起作用,您需要确保测试数据库已正确设置并就绪,如这篇博客中所述。
"scripts": {
"docker:up": "docker compose up -d",
"docker:down": "docker compose down",
"test": "yarn docker:up && yarn prisma migrate deploy && jest -i"
},
test
脚本执行以下操作
- 运行
docker compose up -d
以使用 Postgres 镜像和数据库创建容器。 - 将
./prisma/migrations/
目录中找到的迁移应用于数据库,这会在容器的数据库中创建表。 - 执行测试。
确认无误后,您可以运行 yarn docker:down
来销毁容器、其数据库和任何测试数据。