Skip to content

Commit

Permalink
add per-db feature flags to tests (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
carllerche authored Oct 25, 2024
1 parent a95aa51 commit 096c64d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Start dynamodb-local
run: sudo docker run --name dynamodb -d -p 8000:8000 amazon/dynamodb-local:latest -jar DynamoDBLocal.jar -port 8000
- name: cargo test
run: cargo test --workspace
run: cargo test --workspace --all-features

gen-examples:
needs: check
Expand Down
19 changes: 14 additions & 5 deletions tests/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ version = "0.1.0"
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["sqlite"]
sqlite = [
"dep:toasty-sqlite",
]
dynamodb = [
"dep:toasty-dynamodb",
"dep:aws-config",
"dep:aws-sdk-dynamodb",
]

[dependencies]
async-trait = "0.1"
Expand All @@ -13,12 +22,12 @@ toasty-core = { path = "../../src/core" }
toasty-macros = { path = "../../src/macros" }

# Sqlite driver
toasty-sqlite = { path = "../../src/db/sqlite" }
toasty-sqlite = { path = "../../src/db/sqlite", optional = true }

# DyanmoDB driver
toasty-dynamodb = { path = "../../src/db/ddb" }
aws-config = "1"
aws-sdk-dynamodb = { version = "1.3.0", features = ["test-util"] }
toasty-dynamodb = { path = "../../src/db/ddb", optional = true }
aws-config = { version = "1", optional = true }
aws-sdk-dynamodb = { version = "1.3.0", features = ["test-util"], optional = true }

# Utilities
std-util = { path = "../../src/std-util" }
Expand Down
61 changes: 5 additions & 56 deletions tests/client/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,7 @@
use super::*;

pub struct SetupDynamoDb;

#[async_trait::async_trait]
impl Setup for SetupDynamoDb {
async fn setup(&self, schema: Schema) -> Db {
use aws_config::BehaviorVersion;
use aws_sdk_dynamodb::{config::Credentials, Client};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};

static CNT: AtomicUsize = AtomicUsize::new(0);

let prefix = format!("test_{}_", CNT.fetch_add(1, Relaxed));

let mut sdk_config = aws_config::defaults(BehaviorVersion::latest())
.credentials_provider(Credentials::for_tests());

if std::env::var("AWS_DEFAULT_REGION").is_err() {
sdk_config = sdk_config.region("local");
}
#[cfg(feature = "dynamodb")]
pub mod dynamodb;

if std::env::var("AWS_ENDPOINT_URL_DYNAMODB").is_err() {
sdk_config = sdk_config.endpoint_url("http://localhost:8000");
}
#[cfg(feature = "sqlite")]
pub mod sqlite;

let client = Client::new(&sdk_config.load().await);

let driver = toasty_dynamodb::DynamoDB::new(client, Some(prefix));
let db = toasty::Db::new(schema, driver).await;
db.reset_db().await.unwrap();
db
}

fn capability(&self) -> &Capability {
use toasty_core::driver::capability::KeyValue;

&Capability::KeyValue(KeyValue {
primary_key_ne_predicate: false,
})
}
}

pub struct SetupSqlite;

#[async_trait::async_trait]
impl Setup for SetupSqlite {
async fn setup(&self, schema: Schema) -> Db {
let driver = toasty_sqlite::Sqlite::in_memory();
let db = toasty::Db::new(schema, driver).await;
db.reset_db().await.unwrap();
db
}

fn capability(&self) -> &Capability {
&Capability::Sql
}
}
use super::*;
42 changes: 42 additions & 0 deletions tests/client/src/db/dynamodb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use super::*;

pub struct SetupDynamoDb;

#[async_trait::async_trait]
impl Setup for SetupDynamoDb {
async fn setup(&self, schema: Schema) -> Db {
use aws_config::BehaviorVersion;
use aws_sdk_dynamodb::{config::Credentials, Client};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};

static CNT: AtomicUsize = AtomicUsize::new(0);

let prefix = format!("test_{}_", CNT.fetch_add(1, Relaxed));

let mut sdk_config = aws_config::defaults(BehaviorVersion::latest())
.credentials_provider(Credentials::for_tests());

if std::env::var("AWS_DEFAULT_REGION").is_err() {
sdk_config = sdk_config.region("local");
}

if std::env::var("AWS_ENDPOINT_URL_DYNAMODB").is_err() {
sdk_config = sdk_config.endpoint_url("http://localhost:8000");
}

let client = Client::new(&sdk_config.load().await);

let driver = toasty_dynamodb::DynamoDB::new(client, Some(prefix));
let db = toasty::Db::new(schema, driver).await;
db.reset_db().await.unwrap();
db
}

fn capability(&self) -> &Capability {
use toasty_core::driver::capability::KeyValue;

&Capability::KeyValue(KeyValue {
primary_key_ne_predicate: false,
})
}
}
17 changes: 17 additions & 0 deletions tests/client/src/db/sqlite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use super::*;

pub struct SetupSqlite;

#[async_trait::async_trait]
impl Setup for SetupSqlite {
async fn setup(&self, schema: Schema) -> Db {
let driver = toasty_sqlite::Sqlite::in_memory();
let db = toasty::Db::new(schema, driver).await;
db.reset_db().await.unwrap();
db
}

fn capability(&self) -> &Capability {
&Capability::Sql
}
}
6 changes: 4 additions & 2 deletions tests/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ macro_rules! tests {
$f:ident
),+
) => {
#[cfg(feature = "dynamodb")]
mod dynamodb {
$(
#[tokio::test]
$( #[$attrs] )*
async fn $f() {
super::$f($crate::db::SetupDynamoDb).await;
super::$f($crate::db::dynamodb::SetupDynamoDb).await;
}
)*
}

#[cfg(feature = "sqlite")]
mod sqlite {
$(
#[tokio::test]
$( #[$attrs] )*
async fn $f() {
super::$f($crate::db::SetupSqlite).await;
super::$f($crate::db::sqlite::SetupSqlite).await;
}
)*
}
Expand Down

0 comments on commit 096c64d

Please sign in to comment.