集成测试
集成测试侧重于测试程序中各个独立部分如何协同工作。在使用数据库的应用程序上下文中,集成测试通常需要一个可用的数据库,并且包含适合待测试场景的数据。
一种模拟真实世界环境的方法是使用 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
来销毁容器、其数据库和任何测试数据。