Skip to content

Commit 3e040b7

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 3e040b7

7 files changed

Lines changed: 537 additions & 17 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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,8 @@ valkey_module! {
130130
#[cfg(test)]
131131
mod tests {
132132
use super::*;
133-
use std::ptr::null_mut;
134133
use valkey_module::{MockContext, RedisModuleClientInfo};
135134

136-
fn null_valkey_string() -> ValkeyString {
137-
ValkeyString::from_redis_module_string(null_mut(), null_mut())
138-
}
139-
140135
fn module_client_info(id: u64) -> RedisModuleClientInfo {
141136
RedisModuleClientInfo {
142137
version: 1,
@@ -189,7 +184,8 @@ mod tests {
189184
#[test]
190185
fn test_set_client_name_wrong_arity() {
191186
let ctx = MockContext::new();
192-
let err = set_client_name(&ctx, vec![null_valkey_string()]).unwrap_err();
187+
let err = set_client_name(&ctx, vec![ValkeyString::create_for_test("client.set_name")])
188+
.unwrap_err();
193189
assert!(matches!(err, ValkeyError::WrongArity));
194190
}
195191

@@ -199,13 +195,25 @@ mod tests {
199195
ctx.expect_log_notice().times(1).return_const(());
200196
ctx.expect_set_client_name_by_id()
201197
.times(1)
202-
.returning(|_, _| Status::Ok);
198+
.returning(|_, client_name| {
199+
assert_eq!(client_name.try_as_str().unwrap(), "my-client");
200+
Status::Ok
201+
});
203202
ctx.expect_set_client_name()
204203
.times(1)
205-
.returning(|_| Status::Ok);
204+
.returning(|client_name| {
205+
assert_eq!(client_name.try_as_str().unwrap(), "my-client");
206+
Status::Ok
207+
});
206208

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

@@ -252,7 +260,7 @@ mod tests {
252260
#[test]
253261
fn test_deauth_client_by_id_wrong_arity() {
254262
let ctx = MockContext::new();
255-
let err = deauth_client_by_id(&ctx, vec![null_valkey_string()]).unwrap_err();
263+
let err = deauth_client_by_id(&ctx, vec![ValkeyString::create_for_test("")]).unwrap_err();
256264
assert!(matches!(err, ValkeyError::WrongArity));
257265
}
258266

@@ -265,7 +273,7 @@ mod tests {
265273
#[test]
266274
fn test_config_get_wrong_arity() {
267275
let ctx = MockContext::new();
268-
let err = config_get(&ctx, vec![null_valkey_string()]).unwrap_err();
276+
let err = config_get(&ctx, vec![ValkeyString::create_for_test("")]).unwrap_err();
269277
assert!(matches!(err, ValkeyError::WrongArity));
270278
}
271279

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)