Skip to content

Commit f586930

Browse files
committed
added redismodule_test.rs with ValkeyString::create_for_test
updated examples to use it Signed-off-by: dmitrypol <dmitrypol@users.noreply.github.com>
1 parent 17d8720 commit f586930

6 files changed

Lines changed: 425 additions & 14 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ mod tests {
9393
let mut ctx = MockContext::new();
9494
ctx.expect_get_client_id().times(1).returning(|| 42);
9595

96-
let reply = get_client_id(&ctx, vec![]).unwrap();
96+
let reply = get_client_id(&ctx, vec![ValkeyString::create_for_test("")]).unwrap();
9797
assert_eq!(reply, ValkeyValue::Integer(42));
9898
}
9999
}

examples/client.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ mod tests {
133133
use std::ptr::null_mut;
134134
use valkey_module::{MockContext, RedisModuleClientInfo};
135135

136-
fn null_valkey_string() -> ValkeyString {
137-
ValkeyString::from_redis_module_string(null_mut(), null_mut())
138-
}
139-
140136
fn module_client_info(id: u64) -> RedisModuleClientInfo {
141137
RedisModuleClientInfo {
142138
version: 1,
@@ -189,7 +185,8 @@ mod tests {
189185
#[test]
190186
fn test_set_client_name_wrong_arity() {
191187
let ctx = MockContext::new();
192-
let err = set_client_name(&ctx, vec![null_valkey_string()]).unwrap_err();
188+
let err = set_client_name(&ctx, vec![ValkeyString::create_for_test("client.set_name")])
189+
.unwrap_err();
193190
assert!(matches!(err, ValkeyError::WrongArity));
194191
}
195192

@@ -199,13 +196,25 @@ mod tests {
199196
ctx.expect_log_notice().times(1).return_const(());
200197
ctx.expect_set_client_name_by_id()
201198
.times(1)
202-
.returning(|_, _| Status::Ok);
199+
.returning(|_, client_name| {
200+
assert_eq!(client_name.try_as_str().unwrap(), "my-client");
201+
Status::Ok
202+
});
203203
ctx.expect_set_client_name()
204204
.times(1)
205-
.returning(|_| Status::Ok);
205+
.returning(|client_name| {
206+
assert_eq!(client_name.try_as_str().unwrap(), "my-client");
207+
Status::Ok
208+
});
206209

207-
let reply =
208-
set_client_name(&ctx, vec![null_valkey_string(), null_valkey_string()]).unwrap();
210+
let reply = set_client_name(
211+
&ctx,
212+
vec![
213+
ValkeyString::create_for_test("client.set_name"),
214+
ValkeyString::create_for_test("my-client"),
215+
],
216+
)
217+
.unwrap();
209218
assert_eq!(reply, ValkeyValue::Integer(Status::Ok as i64));
210219
}
211220

@@ -252,7 +261,7 @@ mod tests {
252261
#[test]
253262
fn test_deauth_client_by_id_wrong_arity() {
254263
let ctx = MockContext::new();
255-
let err = deauth_client_by_id(&ctx, vec![null_valkey_string()]).unwrap_err();
264+
let err = deauth_client_by_id(&ctx, vec![ValkeyString::create_for_test("")]).unwrap_err();
256265
assert!(matches!(err, ValkeyError::WrongArity));
257266
}
258267

@@ -265,7 +274,7 @@ mod tests {
265274
#[test]
266275
fn test_config_get_wrong_arity() {
267276
let ctx = MockContext::new();
268-
let err = config_get(&ctx, vec![null_valkey_string()]).unwrap_err();
277+
let err = config_get(&ctx, vec![ValkeyString::create_for_test("")]).unwrap_err();
269278
assert!(matches!(err, ValkeyError::WrongArity));
270279
}
271280

examples/crontab.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22
use std::str::FromStr;
33
use std::sync::{LazyLock, RwLock};
44
use valkey_module::alloc::ValkeyAlloc;
5-
use valkey_module::{valkey_module, Context, Status, ValkeyString};
5+
use valkey_module::{valkey_module, Context, ContextTrait, Status, ValkeyString};
66
use valkey_module_macros::cron_event_handler;
77

88
// struct to hold environment-specific configs, based on the environment name passed in via MODULE LOAD
@@ -86,7 +86,7 @@ fn cron_event_handler(ctx: &Context, _hz: u64) {
8686
}
8787
}
8888

89-
fn initialize(ctx: &Context, args: &[ValkeyString]) -> Status {
89+
fn initialize(ctx: &impl ContextTrait, args: &[ValkeyString]) -> Status {
9090
// if arg passed in MODULE LOAD use it to set env_name
9191
let env_name = match args.get(0) {
9292
Some(tmp) => tmp.to_string(),
@@ -113,3 +113,41 @@ valkey_module! {
113113
commands: [
114114
],
115115
}
116+
117+
#[cfg(test)]
118+
mod tests {
119+
use super::*;
120+
use std::sync::Mutex;
121+
use valkey_module::MockContext;
122+
123+
static TEST_LOCK: Mutex<()> = Mutex::new(());
124+
125+
#[test]
126+
fn initialize_uses_dev_environment_config() {
127+
let _guard = TEST_LOCK.lock().unwrap();
128+
let mut ctx = MockContext::new();
129+
ctx.expect_log_notice().times(1).return_const(());
130+
let args = vec![ValkeyString::create_for_test("dev")];
131+
132+
let status = initialize(&ctx, &args);
133+
let config = ENV_CONFIG.read().unwrap();
134+
135+
assert_eq!(status, Status::Ok);
136+
assert_eq!(config.cron_fn1_fn2, "*/5 * * * * * *");
137+
assert_eq!(config.cron_fn3, "*/10 * * * * * *");
138+
}
139+
140+
#[test]
141+
fn initialize_uses_default_config_without_args() {
142+
let _guard = TEST_LOCK.lock().unwrap();
143+
let mut ctx = MockContext::new();
144+
ctx.expect_log_notice().times(1).return_const(());
145+
146+
let test = initialize(&ctx, &[]);
147+
let config = ENV_CONFIG.read().unwrap();
148+
149+
assert_eq!(test, Status::Ok);
150+
assert_eq!(config.cron_fn1_fn2, "*/15 * * * * * *");
151+
assert_eq!(config.cron_fn3, "*/30 * * * * * *");
152+
}
153+
}

examples/hello.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,45 @@ valkey_module! {
3030
["hello.mul", hello_mul, "", 0, 0, 0],
3131
],
3232
}
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use super::*;
37+
use valkey_module::{Context, ValkeyValue};
38+
39+
fn test_args(values: &[&str]) -> Vec<ValkeyString> {
40+
values
41+
.iter()
42+
.map(|value| ValkeyString::create_for_test(*value))
43+
.collect()
44+
}
45+
46+
#[test]
47+
fn hello_mul_returns_inputs_and_product() {
48+
let reply = hello_mul(&Context::dummy(), test_args(&["hello.mul", "2", "3", "4"])).unwrap();
49+
50+
assert_eq!(
51+
reply,
52+
ValkeyValue::Array(vec![
53+
ValkeyValue::Integer(2),
54+
ValkeyValue::Integer(3),
55+
ValkeyValue::Integer(4),
56+
ValkeyValue::Integer(24),
57+
])
58+
);
59+
}
60+
61+
#[test]
62+
fn hello_mul_returns_wrong_arity_without_numbers() {
63+
let err = hello_mul(&Context::dummy(), test_args(&["hello.mul"])).unwrap_err();
64+
65+
assert!(matches!(err, ValkeyError::WrongArity));
66+
}
67+
68+
#[test]
69+
fn hello_mul_rejects_invalid_integer() {
70+
let err = hello_mul(&Context::dummy(), test_args(&["hello.mul", "2", "xx"])).unwrap_err();
71+
72+
assert!(matches!(err, ValkeyError::Str("Couldn't parse as integer")));
73+
}
74+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub mod native_types;
1111
pub mod raw;
1212
pub mod rediserror;
1313
mod redismodule;
14+
#[cfg(any(test, feature = "test-mocks"))]
15+
mod redismodule_test;
1416
pub mod redisraw;
1517
pub mod redisvalue;
1618
pub mod stream;

0 commit comments

Comments
 (0)