TestMySql

Struct TestMySql 

Source
pub struct TestMySql {
    pub server_url: String,
    pub dbname: String,
}

Fields§

§server_url: String§dbname: String

Implementations§

Source§

impl TestMySql

Source

pub fn new<S>(database_url: String, migrations: S) -> Self
where S: MigrationSource<'static> + Send + Sync + 'static,

Examples found in repository?
examples/mysql_example.rs (lines 7-10)
5async fn main() -> anyhow::Result<()> {
6    // Create a test database with migrations
7    let tdb = TestMySql::new(
8        "mysql://root:password@127.0.0.1:3307".to_string(),
9        Path::new("./fixtures/mysql_migrations"),
10    );
11
12    println!("Created test database: {}", tdb.dbname);
13    println!("Database URL: {}", tdb.url());
14
15    // Get a connection pool
16    let pool = tdb.get_pool().await;
17
18    // Insert a test record
19    sqlx::query("INSERT INTO todos (title) VALUES (?)")
20        .bind("Test MySQL Todo")
21        .execute(&pool)
22        .await?;
23
24    // Query the record back
25    let (id, title): (i32, String) = sqlx::query_as("SELECT id, title FROM todos WHERE title = ?")
26        .bind("Test MySQL Todo")
27        .fetch_one(&pool)
28        .await?;
29
30    println!("Retrieved todo: id={id}, title={title}");
31
32    // Test CSV loading
33    let csv_data = "title\nLoaded from CSV\nAnother CSV entry";
34    tdb.load_csv_data("todos", csv_data).await?;
35
36    // Count all todos
37    let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM todos")
38        .fetch_one(&pool)
39        .await?;
40
41    println!("Total todos in database: {}", count.0);
42
43    // The database will be automatically dropped when tdb goes out of scope
44    println!("Test completed successfully! Database will be cleaned up automatically.");
45
46    Ok(())
47}
Source

pub fn server_url(&self) -> String

Source

pub fn url(&self) -> String

Examples found in repository?
examples/mysql_example.rs (line 13)
5async fn main() -> anyhow::Result<()> {
6    // Create a test database with migrations
7    let tdb = TestMySql::new(
8        "mysql://root:password@127.0.0.1:3307".to_string(),
9        Path::new("./fixtures/mysql_migrations"),
10    );
11
12    println!("Created test database: {}", tdb.dbname);
13    println!("Database URL: {}", tdb.url());
14
15    // Get a connection pool
16    let pool = tdb.get_pool().await;
17
18    // Insert a test record
19    sqlx::query("INSERT INTO todos (title) VALUES (?)")
20        .bind("Test MySQL Todo")
21        .execute(&pool)
22        .await?;
23
24    // Query the record back
25    let (id, title): (i32, String) = sqlx::query_as("SELECT id, title FROM todos WHERE title = ?")
26        .bind("Test MySQL Todo")
27        .fetch_one(&pool)
28        .await?;
29
30    println!("Retrieved todo: id={id}, title={title}");
31
32    // Test CSV loading
33    let csv_data = "title\nLoaded from CSV\nAnother CSV entry";
34    tdb.load_csv_data("todos", csv_data).await?;
35
36    // Count all todos
37    let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM todos")
38        .fetch_one(&pool)
39        .await?;
40
41    println!("Total todos in database: {}", count.0);
42
43    // The database will be automatically dropped when tdb goes out of scope
44    println!("Test completed successfully! Database will be cleaned up automatically.");
45
46    Ok(())
47}
Source

pub async fn get_pool(&self) -> MySqlPool

Examples found in repository?
examples/mysql_example.rs (line 16)
5async fn main() -> anyhow::Result<()> {
6    // Create a test database with migrations
7    let tdb = TestMySql::new(
8        "mysql://root:password@127.0.0.1:3307".to_string(),
9        Path::new("./fixtures/mysql_migrations"),
10    );
11
12    println!("Created test database: {}", tdb.dbname);
13    println!("Database URL: {}", tdb.url());
14
15    // Get a connection pool
16    let pool = tdb.get_pool().await;
17
18    // Insert a test record
19    sqlx::query("INSERT INTO todos (title) VALUES (?)")
20        .bind("Test MySQL Todo")
21        .execute(&pool)
22        .await?;
23
24    // Query the record back
25    let (id, title): (i32, String) = sqlx::query_as("SELECT id, title FROM todos WHERE title = ?")
26        .bind("Test MySQL Todo")
27        .fetch_one(&pool)
28        .await?;
29
30    println!("Retrieved todo: id={id}, title={title}");
31
32    // Test CSV loading
33    let csv_data = "title\nLoaded from CSV\nAnother CSV entry";
34    tdb.load_csv_data("todos", csv_data).await?;
35
36    // Count all todos
37    let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM todos")
38        .fetch_one(&pool)
39        .await?;
40
41    println!("Total todos in database: {}", count.0);
42
43    // The database will be automatically dropped when tdb goes out of scope
44    println!("Test completed successfully! Database will be cleaned up automatically.");
45
46    Ok(())
47}
Source

pub async fn load_csv( &self, table: &str, _fields: &[&str], filename: &Path, ) -> Result<()>

Source

pub async fn load_csv_data(&self, table: &str, csv: &str) -> Result<()>

Examples found in repository?
examples/mysql_example.rs (line 34)
5async fn main() -> anyhow::Result<()> {
6    // Create a test database with migrations
7    let tdb = TestMySql::new(
8        "mysql://root:password@127.0.0.1:3307".to_string(),
9        Path::new("./fixtures/mysql_migrations"),
10    );
11
12    println!("Created test database: {}", tdb.dbname);
13    println!("Database URL: {}", tdb.url());
14
15    // Get a connection pool
16    let pool = tdb.get_pool().await;
17
18    // Insert a test record
19    sqlx::query("INSERT INTO todos (title) VALUES (?)")
20        .bind("Test MySQL Todo")
21        .execute(&pool)
22        .await?;
23
24    // Query the record back
25    let (id, title): (i32, String) = sqlx::query_as("SELECT id, title FROM todos WHERE title = ?")
26        .bind("Test MySQL Todo")
27        .fetch_one(&pool)
28        .await?;
29
30    println!("Retrieved todo: id={id}, title={title}");
31
32    // Test CSV loading
33    let csv_data = "title\nLoaded from CSV\nAnother CSV entry";
34    tdb.load_csv_data("todos", csv_data).await?;
35
36    // Count all todos
37    let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM todos")
38        .fetch_one(&pool)
39        .await?;
40
41    println!("Total todos in database: {}", count.0);
42
43    // The database will be automatically dropped when tdb goes out of scope
44    println!("Test completed successfully! Database will be cleaned up automatically.");
45
46    Ok(())
47}

Trait Implementations§

Source§

impl Debug for TestMySql

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TestMySql

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for TestMySql

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,