Skip to content

Commit b0dc9cb

Browse files
committed
Refactor testing suite
1 parent 44fafd4 commit b0dc9cb

6 files changed

+45
-111
lines changed

tests/derive_from_redis_value.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,3 @@ pub fn it_should_fail_if_input_is_missing() {
7979
panic!("UTF-8 parsing should fail.");
8080
}
8181
}
82-
83-
#[derive(Debug, PartialEq, Deserialize, FromRedisValue)]
84-
struct Pair<K, V> {
85-
key: K,
86-
value: V,
87-
}
88-
89-
#[test]
90-
pub fn it_should_deserialize_struct_with_multiple_generics() {
91-
let expected = Pair {
92-
key: 42u32,
93-
value: "answer".to_string(),
94-
};
95-
let json = "{\"key\":42,\"value\":\"answer\"}";
96-
let val = Value::BulkString(json.as_bytes().into());
97-
let result = Pair::<u32, String>::from_redis_value(&val);
98-
assert_eq!(result, Ok(expected));
99-
}

tests/derive_from_redis_value_redis_yaml.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,3 @@ addresses:
4040
let result = User::from_redis_value(&val);
4141
assert_eq!(result, Ok(user));
4242
}
43-
44-
// new test at the end of tests/derive_from_redis_value_redis_yaml.rs
45-
46-
#[derive(Debug, PartialEq, Deserialize, FromRedisValue)]
47-
#[redis_serializer(serde_yaml)]
48-
struct Pair<K, V> {
49-
key: K,
50-
value: V,
51-
}
52-
53-
#[test]
54-
pub fn it_should_deserialize_struct_with_multiple_generics_with_yaml() {
55-
let expected = Pair {
56-
key: 42u32,
57-
value: "answer".to_string(),
58-
};
59-
let yaml = "key: 42\nvalue: answer\n";
60-
let val = Value::BulkString(yaml.as_bytes().into());
61-
let result = Pair::<u32, String>::from_redis_value(&val);
62-
assert_eq!(result, Ok(expected));
63-
}

tests/derive_to_redis_args.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,3 @@ pub fn it_should_implement_the_to_redis_args_trait() {
2929
let bytes = user.to_redis_args();
3030
assert_eq!(bytes[0], "{\"id\":1,\"name\":\"Ziggy\",\"addresses\":[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]}".as_bytes());
3131
}
32-
33-
#[derive(Debug, Serialize, ToRedisArgs)]
34-
struct Pair<K, V> {
35-
first: K,
36-
second: V,
37-
}
38-
39-
#[test]
40-
pub fn it_should_implement_to_redis_args_for_multiple_generics() {
41-
let pair = Pair {
42-
first: 100u8,
43-
second: "data".to_string(),
44-
};
45-
let bytes = pair.to_redis_args();
46-
assert_eq!(
47-
bytes[0],
48-
"{\"first\":100,\"second\":\"data\"}".as_bytes()
49-
);
50-
}

tests/derive_to_redis_args_redis_yaml.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,3 @@ addresses:
4040
.as_bytes()
4141
);
4242
}
43-
44-
#[derive(Debug, Serialize, ToRedisArgs)]
45-
#[redis_serializer(serde_yaml)]
46-
struct Pair<K, V> {
47-
first: K,
48-
second: V,
49-
}
50-
51-
#[test]
52-
pub fn it_should_implement_to_redis_args_for_multiple_generics_with_yaml() {
53-
let pair = Pair {
54-
first: 100u8,
55-
second: "data".to_string(),
56-
};
57-
let bytes = pair.to_redis_args();
58-
assert_eq!(
59-
std::str::from_utf8(&bytes[0]).unwrap(),
60-
"first: 100\nsecond: data\n"
61-
);
62-
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use redis::{FromRedisValue, ToRedisArgs, Value};
2+
use redis_macros::{FromRedisValue, Json, ToRedisArgs};
3+
use serde::{Deserialize, Serialize};
4+
5+
#[derive(Debug, PartialEq, Serialize, Deserialize, FromRedisValue, ToRedisArgs)]
6+
struct Pair<K, V> {
7+
key: K,
8+
value: V,
9+
}
10+
11+
#[test]
12+
pub fn it_should_deserialize_struct_with_multiple_generics() {
13+
let pair = Pair {
14+
key: 42u32,
15+
value: "answer".to_string(),
16+
};
17+
let bytes = pair.to_redis_args();
18+
let json = "{\"key\":42,\"value\":\"answer\"}".as_bytes();
19+
assert_eq!(bytes[0], json);
20+
21+
let val = Value::BulkString(json.into());
22+
let result = Pair::<u32, String>::from_redis_value(&val);
23+
assert_eq!(result, Ok(pair));
24+
}
25+
26+
#[derive(Debug, PartialEq, Deserialize)]
27+
struct PairWithoutTrait<K, V> {
28+
key: K,
29+
value: V,
30+
}
31+
32+
#[test]
33+
pub fn it_should_deserialize_json_wrapper_with_multiple_generics() {
34+
let expected = PairWithoutTrait {
35+
key: 10u16,
36+
value: "ok".to_string(),
37+
};
38+
let val = Value::BulkString("[{\"key\":10,\"value\":\"ok\"}]".as_bytes().into());
39+
let result = Json::<PairWithoutTrait<u16, String>>::from_redis_value(&val);
40+
if let Ok(Json(parsed)) = result {
41+
assert_eq!(parsed, expected);
42+
} else {
43+
panic!("Generic JSON deserialization should succeed.");
44+
}
45+
}

tests/json_wrapper.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -100,36 +100,3 @@ pub fn it_should_fail_if_input_is_missing() {
100100
panic!("Value Nil should fail.");
101101
}
102102
}
103-
104-
#[derive(Debug, PartialEq, Deserialize)]
105-
struct Pair<K, V> {
106-
key: K,
107-
value: V,
108-
}
109-
110-
#[test]
111-
pub fn it_should_deserialize_json_wrapper_with_multiple_generics() {
112-
let expected = Pair {
113-
key: 10u16,
114-
value: "ok".to_string(),
115-
};
116-
let val = Value::BulkString(
117-
"[{\"key\":10,\"value\":\"ok\"}]".as_bytes().into(),
118-
);
119-
let result = Json::<Pair<u16, String>>::from_redis_value(&val);
120-
if let Ok(Json(parsed)) = result {
121-
assert_eq!(parsed, expected);
122-
} else {
123-
panic!("Generic JSON deserialization should succeed.");
124-
}
125-
}
126-
127-
#[test]
128-
pub fn it_should_fail_json_wrapper_with_mismatched_types() {
129-
// key is a string and value is a number, should fail deserializing Pair<u8, String>
130-
let val = Value::BulkString(
131-
"[{\"key\":\"bad\",\"value\":123}]".as_bytes().into(),
132-
);
133-
let result = Json::<Pair<u8, String>>::from_redis_value(&val);
134-
assert!(result.is_err());
135-
}

0 commit comments

Comments
 (0)