Skip to content

Commit

Permalink
testing module with mock redis connection and command
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Dyas committed May 30, 2022
1 parent 97de836 commit aa0e40f
Show file tree
Hide file tree
Showing 4 changed files with 426 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
@@ -1,3 +1,2 @@
[workspace]
members = ["redis"]

members = ["redis", "redis-test"]
24 changes: 24 additions & 0 deletions redis-test/Cargo.toml
@@ -0,0 +1,24 @@
[package]
name = "redis-test"
version = "0.1.0"
edition = "2021"
description = "Testing helpers for the `redis` crate"
homepage = "https://github.com/redis-rs/redis-rs"
repository = "https://github.com/redis-rs/redis-rs"
documentation = "https://docs.rs/redis-test"
license = "BSD-3-Clause"

[dependencies]
redis = { version = "0.21.5", path = "../redis" }

bytes = { version = "1", optional = true }
futures = { version = "0.3", optional = true }

[features]
aio = ["futures", "redis/aio"]
tokio-comp = ["redis/tokio-comp"]
async-std-comp = ["redis/async-std-comp"]

[dev-dependencies]
tokio = { version = "1", features = ["rt", "macros", "rt-multi-thread", "time"] }

26 changes: 26 additions & 0 deletions redis-test/src/lib.rs
@@ -0,0 +1,26 @@
//! Testing support
//!
//! This module provides `MockRedisConnection` which implements ConnectionLike and can be
//! used in the same place as any other type that behaves like a Redis connection. This is useful
//! for writing unit tests without needing a Redis server.
//!
//! # Example
//!
//! ```rust
//! use redis::{ConnectionLike, RedisError};
//! use redis_test::mock_conn::{MockCmd, MockRedisConnection};
//!
//! fn my_exists<C: ConnectionLike>(conn: &mut C, key: &str) -> Result<bool, RedisError> {
//! let exists: bool = redis::cmd("EXISTS").arg(key).query(conn)?;
//! Ok(exists)
//! }
//!
//! let mut mock_connection = MockRedisConnection::new(vec![
//! MockCmd::new(redis::cmd("EXISTS").arg("foo"), Ok("1")),
//! ]);
//!
//! let result = my_exists(&mut mock_connection, "foo").unwrap();
//! assert_eq!(result, true);
//! ```

pub mod mock_conn;

0 comments on commit aa0e40f

Please sign in to comment.