mssql_testing/
fixtures.rs

1//! Test fixture utilities.
2
3/// Test database fixture for setting up and tearing down test data.
4pub struct TestFixture {
5    /// Database name.
6    pub database: String,
7    /// Tables created by this fixture.
8    pub tables: Vec<String>,
9}
10
11impl TestFixture {
12    /// Create a new test fixture.
13    #[must_use]
14    pub fn new(database: impl Into<String>) -> Self {
15        Self {
16            database: database.into(),
17            tables: Vec::new(),
18        }
19    }
20
21    /// Add a table to the fixture.
22    #[must_use]
23    pub fn with_table(mut self, table: impl Into<String>) -> Self {
24        self.tables.push(table.into());
25        self
26    }
27
28    /// Generate SQL to create the test database.
29    #[must_use]
30    pub fn create_database_sql(&self) -> String {
31        format!(
32            "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '{db}')
33             CREATE DATABASE [{db}]",
34            db = self.database
35        )
36    }
37
38    /// Generate SQL to drop the test database.
39    #[must_use]
40    pub fn drop_database_sql(&self) -> String {
41        format!(
42            "IF EXISTS (SELECT * FROM sys.databases WHERE name = '{db}')
43             DROP DATABASE [{db}]",
44            db = self.database
45        )
46    }
47}