1 unstable release
new 0.1.0 | Mar 5, 2025 |
---|
#1761 in Database interfaces
129 downloads per month
Used in tiberius_row
14KB
150 lines
Tiberius DB Tester
English
A Rust library for SQL Server database testing based on the Tiberius driver.
Features
- Automatically creates randomly named temporary test databases
- Supports executing SQL migration scripts to initialize the database
- Automatically closes all connections and cleans up database resources after testing
- Simple and easy-to-use API, suitable for integration testing scenarios
Installation
Add the dependency to your Cargo.toml
:
[dependencies]
tiberius_db_tester = "0.1.0"
Usage
Basic Usage
use tiberius_db_tester::DBTester;
#[tokio::test]
async fn my_database_test() {
// Create a test database and execute migration script
let dbt = DBTester::new(
"localhost", // hostname
1433, // port
"sa", // username
"YourPassword", // password
"migrations/init.sql" // migration script path
);
// Get database client
let mut client = dbt.get_client().await;
// Execute test query
let query = tiberius::Query::new("SELECT * FROM your_table");
let result = query.query(&mut client).await.unwrap();
let rows = result.into_first_result().await.unwrap();
// Assert
assert_eq!(rows.len(), 2);
// After the test ends, dbt will be automatically destroyed and the database will be cleaned up
}
Migration Script Example
Migration scripts are regular SQL files, for example:
-- migrations/init.sql
CREATE TABLE test (
id INT PRIMARY KEY,
name NVARCHAR(100) NOT NULL
);
INSERT INTO test (id, name) VALUES (1, 'Test 1');
INSERT INTO test (id, name) VALUES (2, 'Test 2');
How It Works
DBTester::new()
creates a randomly named database (format:testdb_uuid
)- Executes the specified SQL migration script to initialize the database structure and test data
- Provides the
get_client()
method to get a configured database connection - When the
DBTester
instance is destroyed, it:- Switches to the
master
database - Uses
ALTER DATABASE SET SINGLE_USER WITH ROLLBACK IMMEDIATE
to close all existing connections - Deletes the test database
- Switches to the
Notes
- Requires a SQL Server user with permissions to create and delete databases
- Ensure the migration script path is correct and the script content is valid
- Pay attention to resource management when using in multi-threaded environments
Dependencies
- tiberius: SQL Server driver for Rust
- tokio: Asynchronous runtime
- tokio-util: Provides compatibility layer
- uuid: Generates unique identifiers
License
MIT
Contributions
Welcome to submit issue reports and pull requests!
中文
一个用于SQL Server数据库测试的Rust工具库,基于Tiberius驱动。
功能特点
- 自动创建随机命名的临时测试数据库
- 支持执行SQL迁移脚本初始化数据库
- 测试完成后自动关闭所有连接并清理数据库资源
- 简单易用的API,适合集成测试场景
安装
在Cargo.toml
中添加依赖:
[dependencies]
tiberius_db_tester = "0.1.0"
使用方法
基本用法
use tiberius_db_tester::DBTester;
#[tokio::test]
async fn my_database_test() {
// 创建测试数据库并执行迁移脚本
let dbt = DBTester::new(
"localhost", // 主机名
1433, // 端口
"sa", // 用户名
"YourPassword", // 密码
"migrations/init.sql" // 迁移脚本路径
);
// 获取数据库客户端
let mut client = dbt.get_client().await;
// 执行测试查询
let query = tiberius::Query::new("SELECT * FROM your_table");
let result = query.query(&mut client).await.unwrap();
let rows = result.into_first_result().await.unwrap();
// 进行断言
assert_eq!(rows.len(), 2);
// 测试结束后,dbt会被自动销毁,数据库也会被清理
}
迁移脚本示例
迁移脚本是普通的SQL文件,例如:
-- migrations/init.sql
CREATE TABLE test (
id INT PRIMARY KEY,
name NVARCHAR(100) NOT NULL
);
INSERT INTO test (id, name) VALUES (1, '测试1');
INSERT INTO test (id, name) VALUES (2, '测试2');
工作原理
DBTester::new()
创建一个随机命名的数据库(格式为testdb_uuid
)- 执行指定的SQL迁移脚本来初始化数据库结构和测试数据
- 提供
get_client()
方法来获取已配置好的数据库连接 - 当
DBTester
实例被销毁时,它会:- 切换到
master
数据库 - 使用
ALTER DATABASE SET SINGLE_USER WITH ROLLBACK IMMEDIATE
关闭所有现有连接 - 删除测试数据库
- 切换到
注意事项
- 需要提供具有创建和删除数据库权限的SQL Server用户
- 确保迁移脚本路径正确,且脚本内容有效
- 在多线程环境中使用时需要注意资源管理
依赖项
- tiberius: SQL Server的Rust驱动
- tokio: 异步运行时
- tokio-util: 提供兼容层
- uuid: 生成唯一标识符
许可证
MIT
贡献
欢迎提交问题报告和拉取请求!
Dependencies
~8–16MB
~248K SLoC