Skip to content

Commit

Permalink
Merge pull request #4 from nlopes/nlopes-run-tests-with-mysql
Browse files Browse the repository at this point in the history
chore(mysql): run tests in CI
  • Loading branch information
nlopes committed Sep 9, 2023
2 parents fa3e554 + af9b0cb commit 072b1ad
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 38 deletions.
68 changes: 35 additions & 33 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,58 @@ jobs:
nightly: true

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.toolchain }}
override: true
components: clippy, rustfmt
- name: Install rust tooling
run: |
rustup update ${{ matrix.toolchain }}
rustup component add --toolchain ${{ matrix.toolchain }} clippy rustfmt
- name: Print rust tooling information
run: |
rustup run ${{ matrix.toolchain }} rustc --version
rustup run ${{ matrix.toolchain }} cargo --version --verbose
rustup run ${{ matrix.toolchain }} cargo clippy --version
rustup run ${{ matrix.toolchain }} cargo fmt --version
- name: Cache cargo registry
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
key: rust_${{ matrix.toolchain }}-cargo-${{ hashFiles('**/Cargo.toml') }}

- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
run: rustup run ${{ matrix.toolchain }} cargo fmt --all -- --check

- name: Run test - sqlite | sqlx | runtime-async-std | macros
uses: actions-rs/cargo@v1
with:
command: test
args: --features sqlite,sqlx,runtime-async-std,macros --all-targets --verbose
run: rustup run ${{ matrix.toolchain }} cargo test --features sqlite,sqlx,runtime-async-std,macros --all-targets --verbose

- name: Run test - sqlite | diesel | runtime-async-std | macros
uses: actions-rs/cargo@v1
with:
command: test
args: --features sqlite,diesel,runtime-async-std,macros --all-targets --verbose
run: rustup run ${{ matrix.toolchain }} cargo test --features sqlite,diesel,runtime-async-std,macros --all-targets --verbose

- name: Run test - sqlite | diesel
uses: actions-rs/cargo@v1
with:
command: test
args: --features sqlite,diesel --all-targets --verbose
run: rustup run ${{ matrix.toolchain }} cargo test --features sqlite,diesel --all-targets --verbose

- name: Start containers for database tests
run: docker compose -f "docker-compose.yml" up -d

- name: Run test - mysql | diesel
run: rustup run ${{ matrix.toolchain }} cargo test --features mysql,diesel --all-targets --verbose

- name: Run test - mysql | sqlx | runtime-async-std
run: rustup run ${{ matrix.toolchain }} cargo test --features mysql,sqlx,runtime-async-std --all-targets --verbose

- name: Stop containers for database tests
if: always()
run: |
docker compose -f "docker-compose.yml" down --volumes
sudo rm -rf .data/{postgresql-15,mysql-8}
- name: Run doc tests
uses: actions-rs/cargo@v1
with:
command: test
args: --features sqlite,sqlx,runtime-async-std,macros --doc --verbose
run: rustup run ${{ matrix.toolchain }} cargo test --features sqlite,sqlx,runtime-async-std,macros --doc --verbose

- name: Run clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --features sqlite,sqlx,runtime-async-std,macros
run: rustup run ${{ matrix.toolchain }} cargo clippy --features sqlite,sqlx,runtime-async-std,macros -- -Dwarnings

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
.data/
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# IMPORTANT: if you change the data folders below, make sure you also change their cleanup
# inside the `.github/workflows/ci.yml` file
services:
postgres-15:
image: postgres:15-alpine
ports:
- "127.0.0.1:5432:5432"
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- .data/postgresql-15/:/var/lib/postgresql
mysql-8:
image: mysql:8
ports:
- "127.0.0.1:3306:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=true
volumes:
- .data/mysql-8/:/var/lib/mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- This file should undo anything in `up.sql`
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE diesel_users (
id VARCHAR(32) PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
5 changes: 5 additions & 0 deletions fixtures/sqlx/mysql/migrations/20230827160610_add_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE sqlx_users (
id VARCHAR(32) PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
39 changes: 38 additions & 1 deletion src/mysql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Implementation of the `mysql` feature

pub(crate) const DEFAULT_CONNECTION_URL: &str = "mysql://root:@localhost:3306";
pub(crate) const DEFAULT_CONNECTION_URL: &str = "mysql://root:@127.0.0.1:3306/mysql";

use percent_encoding::percent_decode_str;

Expand Down Expand Up @@ -159,3 +159,40 @@ async fn migrate<P: AsRef<std::path::Path>>(
.map(|_| ());
Ok(())
}

#[cfg(test)]
mod tests {
#[cfg(all(feature = "sqlx", feature = "mysql"))]
#[tokio::test]
async fn test_write_structure_sql() -> Result<(), crate::error::Error> {
let destination_path = std::env::temp_dir().join("sqlx-mysql-structure.sql");
let migrations_path = std::path::PathBuf::from("./fixtures/sqlx/mysql/migrations");
let _ = super::write_structure_sql(
super::DEFAULT_CONNECTION_URL,
migrations_path,
&destination_path,
)
.await?;
let contents = std::fs::read_to_string(destination_path)?;

assert!(contents.contains("CREATE TABLE `sqlx_users` (\n `id` varchar(32) NOT NULL,\n `email` text NOT NULL,\n `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;\n"));
Ok(())
}

#[cfg(all(feature = "diesel", feature = "mysql"))]
#[tokio::test]
async fn test_write_structure_sql() -> Result<(), crate::error::Error> {
let destination_path = std::env::temp_dir().join("diesel-mysql-structure.sql");
let migrations_path = std::path::PathBuf::from("./fixtures/diesel/mysql/migrations");
let _ = super::write_structure_sql(
super::DEFAULT_CONNECTION_URL,
migrations_path,
&destination_path,
)
.await?;
let contents = std::fs::read_to_string(destination_path)?;

assert!(contents.contains("CREATE TABLE `diesel_users` (\n `id` varchar(32) NOT NULL,\n `email` text NOT NULL,\n `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;\n"));
Ok(())
}
}
8 changes: 4 additions & 4 deletions src/mysql/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl MySqlConnectOptions {
///
/// # Example
///
/// ```rust
/// ```rust,ignore
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
/// let options = MySqlConnectOptions::new()
/// .ssl_mode(MySqlSslMode::Required);
Expand All @@ -177,7 +177,7 @@ impl MySqlConnectOptions {
///
/// # Example
///
/// ```rust
/// ```rust,ignore
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
/// let options = MySqlConnectOptions::new()
/// .ssl_mode(MySqlSslMode::VerifyCa)
Expand All @@ -192,7 +192,7 @@ impl MySqlConnectOptions {
///
/// # Example
///
/// ```rust
/// ```rust,ignore
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
/// let options = MySqlConnectOptions::new()
/// .ssl_mode(MySqlSslMode::VerifyCa)
Expand All @@ -207,7 +207,7 @@ impl MySqlConnectOptions {
///
/// # Example
///
/// ```rust
/// ```rust,ignore
/// # use sqlx_core::mysql::{MySqlSslMode, MySqlConnectOptions};
/// let options = MySqlConnectOptions::new()
/// .ssl_mode(MySqlSslMode::VerifyCa)
Expand Down

0 comments on commit 072b1ad

Please sign in to comment.