Skip to content

Commit 1428bea

Browse files
committed
Clean up tests following is_keys_position_request merge
1 parent 6ea51de commit 1428bea

File tree

4 files changed

+94
-13
lines changed

4 files changed

+94
-13
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ readme = "README.md"
1313
name = "hello"
1414
crate-type = ["cdylib"]
1515

16+
[[example]]
17+
name = "keys_pos"
18+
crate-type = ["cdylib"]
19+
1620
[[example]]
1721
name = "data_type"
1822
crate-type = ["cdylib"]

examples/hello.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
#[macro_use]
22
extern crate redis_module;
33

4-
use redis_module::{parse_integer, Context, RedisError, RedisResult, RedisValue};
4+
use redis_module::{parse_integer, Context, RedisError, RedisResult};
55

6-
fn hello_mul(ctx: &Context, args: Vec<String>) -> RedisResult {
6+
fn hello_mul(_: &Context, args: Vec<String>) -> RedisResult {
77
if args.len() < 2 {
88
return Err(RedisError::WrongArity);
99
}
1010

11-
if ctx.is_keys_position_request() {
12-
for i in 1..args.len() {
13-
if i % 2 == 0 {
14-
ctx.key_at_pos(i as i32);
15-
}
16-
}
17-
return Ok(RedisValue::NoReply);
18-
}
19-
2011
let nums = args
2112
.into_iter()
2213
.skip(1)
@@ -38,7 +29,7 @@ redis_module! {
3829
version: 1,
3930
data_types: [],
4031
commands: [
41-
["hello.mul", hello_mul, "getkeys-api", 1, 1, 1],
32+
["hello.mul", hello_mul, "", 0, 0, 0],
4233
],
4334
}
4435

examples/keys_pos.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#[macro_use]
2+
extern crate redis_module;
3+
4+
use redis_module::{Context, RedisError, RedisResult, RedisValue};
5+
6+
fn keys_pos(ctx: &Context, args: Vec<String>) -> RedisResult {
7+
// Number of args (excluding command name) must be even
8+
if (args.len() - 1) % 2 != 0 {
9+
return Err(RedisError::WrongArity);
10+
}
11+
12+
if ctx.is_keys_position_request() {
13+
for i in 1..args.len() {
14+
if (i - 1) % 2 == 0 {
15+
ctx.key_at_pos(i as i32);
16+
}
17+
}
18+
return Ok(RedisValue::NoReply);
19+
}
20+
21+
let reply: Vec<_> = args.iter().skip(1).step_by(2).collect();
22+
23+
return Ok(reply.into());
24+
}
25+
26+
//////////////////////////////////////////////////////
27+
28+
redis_module! {
29+
name: "keys_pos",
30+
version: 1,
31+
data_types: [],
32+
commands: [
33+
["keys_pos", keys_pos, "getkeys-api", 1, 1, 1],
34+
],
35+
}
36+
37+
//////////////////////////////////////////////////////
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use super::*;
42+
use redis_module::RedisValue;
43+
44+
fn into_string_vec(args: &[&str]) -> Vec<String> {
45+
args.iter().map(|v| String::from(*v)).collect()
46+
}
47+
48+
fn into_redisvalue_vec(args: &[&str]) -> Vec<RedisValue> {
49+
args.iter()
50+
.map(|&s| RedisValue::BulkString(s.to_string()))
51+
.collect()
52+
}
53+
54+
#[test]
55+
fn test_keys_pos() {
56+
let result = keys_pos(
57+
&Context::dummy(),
58+
into_string_vec(&["keys_pos", "a", "1", "b", "2"]),
59+
);
60+
61+
match result {
62+
Ok(RedisValue::Array(v)) => {
63+
assert_eq!(v, into_redisvalue_vec(&["a", "b"]));
64+
}
65+
_ => assert!(false, "Bad result: {:?}", result),
66+
}
67+
}
68+
69+
#[test]
70+
fn test_keys_pos_bad_args() {
71+
let result = keys_pos(
72+
&Context::dummy(),
73+
into_string_vec(&["keys_pos", "a", "1", "b"]),
74+
);
75+
76+
match result {
77+
Err(RedisError::WrongArity) => (),
78+
_ => assert!(false, "Bad result: {:?}", result),
79+
}
80+
}
81+
}

src/context.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::raw;
77
use crate::LogLevel;
88
use crate::{RedisError, RedisResult, RedisString, RedisValue};
99

10-
/// Redis is a structure that's designed to give us a high-level interface to
10+
/// `Context` is a structure that's designed to give us a high-level interface to
1111
/// the Redis module API by abstracting away the raw C FFI calls.
1212
pub struct Context {
1313
ctx: *mut raw::RedisModuleCtx,
@@ -57,6 +57,11 @@ impl Context {
5757
}
5858

5959
pub fn is_keys_position_request(&self) -> bool {
60+
// HACK: Used for tests, where the context is null
61+
if self.ctx.is_null() {
62+
return false;
63+
}
64+
6065
let result = unsafe { raw::RedisModule_IsKeysPositionRequest.unwrap()(self.ctx) };
6166

6267
result != 0

0 commit comments

Comments
 (0)