From 1cdbcceddbec0029007e104bdce5d2ee7a59ab7d Mon Sep 17 00:00:00 2001 From: Billy Chan <30400950+billy1624@users.noreply.github.com> Date: Thu, 22 Jul 2021 19:23:21 +0800 Subject: [PATCH] Testing different db on various async runtime (#53) * Testing different db on various async runtime * Test on various runtime * No explicit cargo build step * Cargo build with features * Hotfix * With actix_rt * Only tests "runtime-async-std-native-tls" for now --- .github/workflows/rust.yml | 95 +++++++++++++++++++++++++++++++++- Cargo.toml | 34 +++++++++--- examples/cli/Cargo.toml | 2 +- examples/codegen/Cargo.toml | 2 +- examples/sqlx-mysql/Cargo.toml | 2 +- sea-orm-codegen/Cargo.toml | 32 ++++++++++-- src/executor/insert.rs | 6 +-- src/executor/query.rs | 2 + tests/bakery_chain_tests.rs | 5 +- tests/basic.rs | 5 +- tests/pg_tests.rs | 5 +- 11 files changed, 170 insertions(+), 20 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fb06d160..56d302fd 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -15,6 +15,86 @@ jobs: test: name: Unit Test runs-on: ubuntu-20.04 + strategy: + matrix: + # runtime: [async-std-native-tls, async-std-rustls, actix-native-tls, actix-rustls, tokio-native-tls, tokio-rustls] + runtime: [async-std-native-tls] + steps: + - uses: actions/checkout@v2 + + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-test-${{ matrix.runtime }}-${{ hashFiles('**/Cargo.lock') }} + + - uses: actions-rs/cargo@v1 + with: + command: build + args: > + --all + --features default,runtime-${{ matrix.runtime }} + + - uses: actions-rs/cargo@v1 + with: + command: test + args: > + --all + --features default,runtime-${{ matrix.runtime }} + + sqlite: + name: SQLite + runs-on: ubuntu-20.04 + strategy: + matrix: + # runtime: [async-std-native-tls, async-std-rustls, actix-native-tls, actix-rustls, tokio-native-tls, tokio-rustls] + runtime: [async-std-native-tls] + steps: + - uses: actions/checkout@v2 + + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-sqlite-${{ matrix.runtime }}-${{ hashFiles('**/Cargo.lock') }} + + - uses: actions-rs/cargo@v1 + with: + command: build + args: > + --all + --features default,runtime-${{ matrix.runtime }} + + - uses: actions-rs/cargo@v1 + with: + command: test + args: > + --all + --features default,sqlx-sqlite,runtime-${{ matrix.runtime }} + + postgres: + name: Postgres + runs-on: ubuntu-20.04 + strategy: + matrix: + # runtime: [async-std-native-tls, async-std-rustls, actix-native-tls, actix-rustls, tokio-native-tls, tokio-rustls] + runtime: [async-std-native-tls] services: postgres: image: postgres:11 @@ -38,11 +118,24 @@ jobs: toolchain: stable override: true + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-postgres-${{ matrix.runtime }}-${{ hashFiles('**/Cargo.lock') }} + - uses: actions-rs/cargo@v1 with: command: build + args: > + --all + --features default,runtime-${{ matrix.runtime }} - uses: actions-rs/cargo@v1 with: command: test - args: --all + args: > + --all + --features default,sqlx-postgres,runtime-${{ matrix.runtime }} diff --git a/Cargo.toml b/Cargo.toml index c58edf78..274563d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,9 +46,11 @@ serde_json = { version = "^1", optional = true } [dev-dependencies] async-std = { version = "^1.9", features = [ "attributes" ] } +tokio = { version = "^1.6", features = ["full"] } +actix-rt = { version = "2.2.0" } maplit = { version = "^1" } rust_decimal_macros = { version = "^1" } -sea-orm = { path = ".", features = ["sqlx-postgres", "sqlx-sqlite", "sqlx-json", "sqlx-chrono", "sqlx-decimal", "runtime-async-std-native-tls", "debug-print"] } +sea-orm = { path = ".", features = ["sqlx-json", "sqlx-chrono", "sqlx-decimal", "debug-print"] } [features] debug-print = [] @@ -66,9 +68,27 @@ sqlx-decimal = [ "sqlx/decimal", "with-rust_decimal" ] sqlx-mysql = [ "sqlx-dep", "sea-query/sqlx-mysql", "sqlx/mysql" ] sqlx-postgres = [ "sqlx-dep", "sea-query/sqlx-postgres", "sqlx/postgres" ] sqlx-sqlite = [ "sqlx-dep", "sea-query/sqlx-sqlite", "sqlx/sqlite" ] -runtime-actix-native-tls = [ "sqlx/runtime-actix-native-tls" ] -runtime-async-std-native-tls = [ "sqlx/runtime-async-std-native-tls" ] -runtime-tokio-native-tls = [ "sqlx/runtime-tokio-native-tls" ] -runtime-actix-rustls = [ "sqlx/runtime-actix-rustls" ] -runtime-async-std-rustls = [ "sqlx/runtime-async-std-rustls" ] -runtime-tokio-rustls = [ "sqlx/runtime-tokio-rustls" ] +runtime-async-std = [] +runtime-async-std-native-tls = [ "sqlx/runtime-async-std-native-tls", "runtime-async-std" ] +runtime-async-std-rustls = [ "sqlx/runtime-async-std-rustls", "runtime-async-std" ] +runtime-actix = [] +runtime-actix-native-tls = [ "sqlx/runtime-actix-native-tls", "runtime-actix" ] +runtime-actix-rustls = [ "sqlx/runtime-actix-rustls", "runtime-actix" ] +runtime-tokio = [] +runtime-tokio-native-tls = [ "sqlx/runtime-tokio-native-tls", "runtime-tokio" ] +runtime-tokio-rustls = [ "sqlx/runtime-tokio-rustls", "runtime-tokio" ] + +[[test]] +name = "sqlite-basic" +path = "tests/basic.rs" +required-features = ["sqlx-sqlite"] + +[[test]] +name = "sqlite" +path = "tests/bakery_chain_tests.rs" +required-features = ["sqlx-sqlite"] + +[[test]] +name = "postgres" +path = "tests/pg_tests.rs" +required-features = ["sqlx-postgres"] diff --git a/examples/cli/Cargo.toml b/examples/cli/Cargo.toml index 979e847a..75185d3e 100644 --- a/examples/cli/Cargo.toml +++ b/examples/cli/Cargo.toml @@ -5,5 +5,5 @@ edition = "2018" publish = false [dependencies] -sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls", "debug-print" ] } +sea-orm = { path = "../../" } strum = { version = "^0.20", features = [ "derive" ] } diff --git a/examples/codegen/Cargo.toml b/examples/codegen/Cargo.toml index a058f0f0..9b78ad0e 100644 --- a/examples/codegen/Cargo.toml +++ b/examples/codegen/Cargo.toml @@ -6,7 +6,7 @@ publish = false [dependencies] async-std = { version = "^1.9", features = [ "attributes" ] } -sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls", "debug-print" ] } +sea-orm = { path = "../../" } sea-orm-codegen = { path = "../../sea-orm-codegen" } sea-query = { version = "^0.12" } strum = { version = "^0.20", features = [ "derive" ] } diff --git a/examples/sqlx-mysql/Cargo.toml b/examples/sqlx-mysql/Cargo.toml index 4b39809c..7954fb69 100644 --- a/examples/sqlx-mysql/Cargo.toml +++ b/examples/sqlx-mysql/Cargo.toml @@ -6,7 +6,7 @@ publish = false [dependencies] async-std = { version = "^1.9", features = [ "attributes" ] } -sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls", "debug-print", "sqlx-json", "macros" ], default-features = false } +sea-orm = { path = "../../" } serde_json = { version = "^1" } futures = { version = "^0.3" } async-stream = { version = "^0.3" } diff --git a/sea-orm-codegen/Cargo.toml b/sea-orm-codegen/Cargo.toml index 100314c9..94433fc2 100644 --- a/sea-orm-codegen/Cargo.toml +++ b/sea-orm-codegen/Cargo.toml @@ -16,9 +16,9 @@ name = "sea_orm_codegen" path = "src/lib.rs" [dependencies] -sea-schema = { version = "^0.2", default-features = false, features = [ "sqlx-mysql", "runtime-async-std-native-tls", "discovery", "writer" ] } +sea-schema = { version = "^0.2", default-features = false, features = [ "sqlx-mysql", "discovery", "writer" ] } sea-query = { version = "^0.12" } -sqlx = { version = "^0.5", features = [ "mysql", "runtime-async-std-native-tls" ] } +sqlx = { version = "^0.5", default-features = false, features = [ "mysql" ] } syn = { version = "^1", default-features = false, features = [ "derive", "parsing", "proc-macro", "printing" ] } quote = "^1" heck = "^0.3" @@ -26,4 +26,30 @@ proc-macro2 = "^1" [dev-dependencies] async-std = { version = "^1.9", features = [ "attributes" ] } -sea-orm = { path = "../", features = ["mock", "sqlx-json", "sqlx-chrono", "runtime-async-std-native-tls"] } +sea-orm = { path = "../" } + +[features] +runtime-actix-native-tls = [ + "sqlx/runtime-actix-native-tls", + "sea-schema/runtime-actix-native-tls", +] +runtime-async-std-native-tls = [ + "sqlx/runtime-async-std-native-tls", + "sea-schema/runtime-async-std-native-tls", +] +runtime-tokio-native-tls = [ + "sqlx/runtime-tokio-native-tls", + "sea-schema/runtime-tokio-native-tls", +] +runtime-actix-rustls = [ + "sqlx/runtime-actix-rustls", + "sea-schema/runtime-actix-rustls", +] +runtime-async-std-rustls = [ + "sqlx/runtime-async-std-rustls", + "sea-schema/runtime-async-std-rustls", +] +runtime-tokio-rustls = [ + "sqlx/runtime-tokio-rustls", + "sea-schema/runtime-tokio-rustls", +] diff --git a/src/executor/insert.rs b/src/executor/insert.rs index f8d60137..9414fea6 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -1,6 +1,4 @@ -use crate::{ - error::*, ActiveModelTrait, DatabaseConnection, EntityTrait, Insert, Iterable, Statement, -}; +use crate::{error::*, ActiveModelTrait, DatabaseConnection, Insert, Statement}; use sea_query::InsertStatement; use std::future::Future; @@ -18,6 +16,7 @@ impl Insert where A: ActiveModelTrait, { + #[allow(unused_mut)] pub fn exec( self, db: &DatabaseConnection, @@ -26,6 +25,7 @@ where let mut query = self.query; #[cfg(feature = "sqlx-postgres")] if let DatabaseConnection::SqlxPostgresPoolConnection(_) = db { + use crate::{EntityTrait, Iterable}; use sea_query::{Alias, Expr, Query}; for key in ::PrimaryKey::iter() { query.returning( diff --git a/src/executor/query.rs b/src/executor/query.rs index 4a2c8776..63c60bd2 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -133,6 +133,7 @@ macro_rules! try_getable_unsigned { row.try_get(column.as_str()) .map_err(crate::sqlx_error_to_query_err) } + #[cfg(feature = "sqlx-postgres")] QueryResultRow::SqlxPostgres(_) => { panic!("{} unsupported by sqlx-postgres", stringify!($type)) } @@ -160,6 +161,7 @@ macro_rules! try_getable_unsigned { Err(_) => Ok(None), } } + #[cfg(feature = "sqlx-postgres")] QueryResultRow::SqlxPostgres(_) => { panic!("{} unsupported by sqlx-postgres", stringify!($type)) } diff --git a/tests/bakery_chain_tests.rs b/tests/bakery_chain_tests.rs index c58be9eb..7c9bb5d5 100644 --- a/tests/bakery_chain_tests.rs +++ b/tests/bakery_chain_tests.rs @@ -6,8 +6,11 @@ pub use bakery_chain::*; mod crud; mod schema; -#[async_std::test] // cargo test --test bakery_chain_tests -- --nocapture +#[cfg_attr(feature = "runtime-async-std", async_std::main)] +#[cfg_attr(feature = "runtime-actix", actix_rt::main)] +#[cfg_attr(feature = "runtime-tokio", tokio::main)] +#[cfg(feature = "sqlx-sqlite")] async fn main() { let db: DbConn = setup::setup().await; setup_schema(&db).await; diff --git a/tests/basic.rs b/tests/basic.rs index faa02a63..20256ad4 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -2,8 +2,11 @@ use sea_orm::{entity::*, error::*, sea_query, tests_cfg::*, DbBackend, DbConn, S mod setup; -#[async_std::test] // cargo test --test basic -- --nocapture +#[cfg_attr(feature = "runtime-async-std", async_std::main)] +#[cfg_attr(feature = "runtime-actix", actix_rt::main)] +#[cfg_attr(feature = "runtime-tokio", tokio::main)] +#[cfg(feature = "sqlx-sqlite")] async fn main() { let db: DbConn = setup::setup().await; diff --git a/tests/pg_tests.rs b/tests/pg_tests.rs index f3ccc471..b8a6c5d2 100644 --- a/tests/pg_tests.rs +++ b/tests/pg_tests.rs @@ -8,7 +8,10 @@ pub use bakery_chain::*; use sea_query::{ColumnDef, TableCreateStatement}; // cargo test --test pg_tests -- --nocapture -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::main)] +#[cfg_attr(feature = "runtime-actix", actix_rt::main)] +#[cfg_attr(feature = "runtime-tokio", tokio::main)] +#[cfg(feature = "sqlx-postgres")] async fn main() { let base_url = "postgres://root:root@localhost"; let db_name = "bakery_chain_schema_crud_tests";