pub struct TestMySql {
pub server_url: String,
pub dbname: String,
}Fields§
§server_url: String§dbname: StringImplementations§
Source§impl TestMySql
impl TestMySql
Sourcepub fn new<S>(database_url: String, migrations: S) -> Self
pub fn new<S>(database_url: String, migrations: S) -> Self
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}pub fn server_url(&self) -> String
Sourcepub fn url(&self) -> String
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}Sourcepub async fn get_pool(&self) -> MySqlPool
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}pub async fn load_csv( &self, table: &str, _fields: &[&str], filename: &Path, ) -> Result<()>
Sourcepub async fn load_csv_data(&self, table: &str, csv: &str) -> Result<()>
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§
Auto Trait Implementations§
impl Freeze for TestMySql
impl RefUnwindSafe for TestMySql
impl Send for TestMySql
impl Sync for TestMySql
impl Unpin for TestMySql
impl UnwindSafe for TestMySql
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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