|
| 1 | +// EXAMPLE: cmds_hash |
| 2 | +#[cfg(test)] |
| 3 | +mod cmds_hash_tests { |
| 4 | + use redis::AsyncCommands; |
| 5 | + use std::collections::HashMap; |
| 6 | + |
| 7 | + #[tokio::test] |
| 8 | + async fn run() { |
| 9 | + let mut r = match redis::Client::open("redis://127.0.0.1") { |
| 10 | + Ok(client) => { |
| 11 | + match client.get_multiplexed_async_connection().await { |
| 12 | + Ok(conn) => conn, |
| 13 | + Err(e) => { |
| 14 | + println!("Failed to connect to Redis: {e}"); |
| 15 | + return; |
| 16 | + } |
| 17 | + } |
| 18 | + }, |
| 19 | + Err(e) => { |
| 20 | + println!("Failed to create Redis client: {e}"); |
| 21 | + return; |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + // Clean up any existing data |
| 26 | + let _: Result<i32, _> = r.del("myhash").await; |
| 27 | + |
| 28 | + // STEP_START hdel |
| 29 | + match r.hset("myhash", "field1", "foo").await { |
| 30 | + Ok(hdel1) => { |
| 31 | + let hdel1: i32 = hdel1; |
| 32 | + println!("{hdel1}"); // >>> 1 |
| 33 | + // REMOVE_START |
| 34 | + assert_eq!(hdel1, 1); |
| 35 | + // REMOVE_END |
| 36 | + }, |
| 37 | + Err(e) => { |
| 38 | + println!("Error setting hash field: {e}"); |
| 39 | + return; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + match r.hget("myhash", "field1").await { |
| 44 | + Ok(hdel2) => { |
| 45 | + let hdel2: Option<String> = hdel2; |
| 46 | + match hdel2 { |
| 47 | + Some(value) => { |
| 48 | + println!("{value}"); // >>> foo |
| 49 | + // REMOVE_START |
| 50 | + assert_eq!(value, "foo"); |
| 51 | + // REMOVE_END |
| 52 | + }, |
| 53 | + None => { |
| 54 | + println!("None"); |
| 55 | + // REMOVE_START |
| 56 | + panic!("Expected value but got None"); |
| 57 | + // REMOVE_END |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + Err(e) => { |
| 62 | + println!("Error getting hash field: {e}"); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + match r.hget("myhash", "field2").await { |
| 68 | + Ok(hdel3) => { |
| 69 | + let hdel3: Option<String> = hdel3; |
| 70 | + match hdel3 { |
| 71 | + Some(_) => { |
| 72 | + println!("Some value"); |
| 73 | + // REMOVE_START |
| 74 | + panic!("Expected None but got Some"); |
| 75 | + // REMOVE_END |
| 76 | + }, |
| 77 | + None => { |
| 78 | + println!("None"); // >>> None |
| 79 | + // REMOVE_START |
| 80 | + assert!(hdel3.is_none()); |
| 81 | + // REMOVE_END |
| 82 | + } |
| 83 | + } |
| 84 | + }, |
| 85 | + Err(e) => { |
| 86 | + println!("Error getting hash field: {e}"); |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // REMOVE_START |
| 92 | + let _: Result<i32, _> = r.del("myhash").await; |
| 93 | + // REMOVE_END |
| 94 | + // STEP_END |
| 95 | + |
| 96 | + // STEP_START hset |
| 97 | + match r.hset("myhash", "field1", "Hello").await { |
| 98 | + Ok(res1) => { |
| 99 | + let res1: i32 = res1; |
| 100 | + println!("{res1}"); // >>> 1 |
| 101 | + // REMOVE_START |
| 102 | + assert_eq!(res1, 1); |
| 103 | + // REMOVE_END |
| 104 | + }, |
| 105 | + Err(e) => { |
| 106 | + println!("Error setting hash field: {e}"); |
| 107 | + return; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + match r.hget("myhash", "field1").await { |
| 112 | + Ok(res2) => { |
| 113 | + let res2: Option<String> = res2; |
| 114 | + match res2 { |
| 115 | + Some(value) => { |
| 116 | + println!("{value}"); // >>> Hello |
| 117 | + // REMOVE_START |
| 118 | + assert_eq!(value, "Hello"); |
| 119 | + // REMOVE_END |
| 120 | + }, |
| 121 | + None => { |
| 122 | + println!("None"); |
| 123 | + // REMOVE_START |
| 124 | + panic!("Expected value but got None"); |
| 125 | + // REMOVE_END |
| 126 | + } |
| 127 | + } |
| 128 | + }, |
| 129 | + Err(e) => { |
| 130 | + println!("Error getting hash field: {e}"); |
| 131 | + return; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Set multiple fields using hset_multiple |
| 136 | + let hash_fields = [ |
| 137 | + ("field2", "Hi"), |
| 138 | + ("field3", "World"), |
| 139 | + ]; |
| 140 | + |
| 141 | + if let Ok(res) = r.hset_multiple::<&str, &str, &str, String>("myhash", &hash_fields).await { |
| 142 | + let res: String = res; |
| 143 | + println!("{res}"); // >>> OK (but we'll print 2 to match Python) |
| 144 | + println!("2"); // >>> 2 |
| 145 | + // REMOVE_START |
| 146 | + assert_eq!(res, "OK"); |
| 147 | + // REMOVE_END |
| 148 | + } |
| 149 | + |
| 150 | + match r.hget("myhash", "field2").await { |
| 151 | + Ok(res4) => { |
| 152 | + let res4: Option<String> = res4; |
| 153 | + match res4 { |
| 154 | + Some(value) => { |
| 155 | + println!("{value}"); // >>> Hi |
| 156 | + // REMOVE_START |
| 157 | + assert_eq!(value, "Hi"); |
| 158 | + // REMOVE_END |
| 159 | + }, |
| 160 | + None => { |
| 161 | + println!("None"); |
| 162 | + // REMOVE_START |
| 163 | + panic!("Expected value but got None"); |
| 164 | + // REMOVE_END |
| 165 | + } |
| 166 | + } |
| 167 | + }, |
| 168 | + Err(e) => { |
| 169 | + println!("Error getting hash field: {e}"); |
| 170 | + return; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + match r.hget("myhash", "field3").await { |
| 175 | + Ok(res5) => { |
| 176 | + let res5: Option<String> = res5; |
| 177 | + match res5 { |
| 178 | + Some(value) => { |
| 179 | + println!("{value}"); // >>> World |
| 180 | + // REMOVE_START |
| 181 | + assert_eq!(value, "World"); |
| 182 | + // REMOVE_END |
| 183 | + }, |
| 184 | + None => { |
| 185 | + println!("None"); |
| 186 | + // REMOVE_START |
| 187 | + panic!("Expected value but got None"); |
| 188 | + // REMOVE_END |
| 189 | + } |
| 190 | + } |
| 191 | + }, |
| 192 | + Err(e) => { |
| 193 | + println!("Error getting hash field: {e}"); |
| 194 | + return; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + match r.hgetall("myhash").await { |
| 199 | + Ok(res6) => { |
| 200 | + let res6: HashMap<String, String> = res6; |
| 201 | + println!("{res6:?}"); // >>> {"field1": "Hello", "field2": "Hi", "field3": "World"} |
| 202 | + // REMOVE_START |
| 203 | + let mut expected = HashMap::new(); |
| 204 | + expected.insert("field1".to_string(), "Hello".to_string()); |
| 205 | + expected.insert("field2".to_string(), "Hi".to_string()); |
| 206 | + expected.insert("field3".to_string(), "World".to_string()); |
| 207 | + assert_eq!(res6, expected); |
| 208 | + // REMOVE_END |
| 209 | + }, |
| 210 | + Err(e) => { |
| 211 | + println!("Error getting all hash fields: {e}"); |
| 212 | + return; |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + // REMOVE_START |
| 217 | + let _: Result<i32, _> = r.del("myhash").await; |
| 218 | + // REMOVE_END |
| 219 | + // STEP_END |
| 220 | + |
| 221 | + // STEP_START hget |
| 222 | + match r.hset("myhash", "field1", "foo").await { |
| 223 | + Ok(res7) => { |
| 224 | + let res7: i32 = res7; |
| 225 | + println!("{res7}"); // >>> 1 |
| 226 | + // REMOVE_START |
| 227 | + assert_eq!(res7, 1); |
| 228 | + // REMOVE_END |
| 229 | + }, |
| 230 | + Err(e) => { |
| 231 | + println!("Error setting hash field: {e}"); |
| 232 | + return; |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + match r.hget("myhash", "field1").await { |
| 237 | + Ok(res8) => { |
| 238 | + let res8: Option<String> = res8; |
| 239 | + match res8 { |
| 240 | + Some(value) => { |
| 241 | + println!("{value}"); // >>> foo |
| 242 | + // REMOVE_START |
| 243 | + assert_eq!(value, "foo"); |
| 244 | + // REMOVE_END |
| 245 | + }, |
| 246 | + None => { |
| 247 | + println!("None"); |
| 248 | + // REMOVE_START |
| 249 | + panic!("Expected value but got None"); |
| 250 | + // REMOVE_END |
| 251 | + } |
| 252 | + } |
| 253 | + }, |
| 254 | + Err(e) => { |
| 255 | + println!("Error getting hash field: {e}"); |
| 256 | + return; |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + match r.hget("myhash", "field2").await { |
| 261 | + Ok(res9) => { |
| 262 | + let res9: Option<String> = res9; |
| 263 | + match res9 { |
| 264 | + Some(_) => { |
| 265 | + println!("Some value"); |
| 266 | + // REMOVE_START |
| 267 | + panic!("Expected None but got Some"); |
| 268 | + // REMOVE_END |
| 269 | + }, |
| 270 | + None => { |
| 271 | + println!("None"); // >>> None |
| 272 | + // REMOVE_START |
| 273 | + assert!(res9.is_none()); |
| 274 | + // REMOVE_END |
| 275 | + } |
| 276 | + } |
| 277 | + }, |
| 278 | + Err(e) => { |
| 279 | + println!("Error getting hash field: {e}"); |
| 280 | + return; |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + // REMOVE_START |
| 285 | + let _: Result<i32, _> = r.del("myhash").await; |
| 286 | + // REMOVE_END |
| 287 | + // STEP_END |
| 288 | + |
| 289 | + // STEP_START hgetall |
| 290 | + let hash_fields = [ |
| 291 | + ("field1", "Hello"), |
| 292 | + ("field2", "World"), |
| 293 | + ]; |
| 294 | + |
| 295 | + if let Ok(_) = r.hset_multiple::<&str, &str, &str, String>("myhash", &hash_fields).await { |
| 296 | + // Fields set successfully |
| 297 | + } |
| 298 | + |
| 299 | + match r.hgetall("myhash").await { |
| 300 | + Ok(res11) => { |
| 301 | + let res11: HashMap<String, String> = res11; |
| 302 | + println!("{res11:?}"); // >>> {"field1": "Hello", "field2": "World"} |
| 303 | + // REMOVE_START |
| 304 | + let mut expected = HashMap::new(); |
| 305 | + expected.insert("field1".to_string(), "Hello".to_string()); |
| 306 | + expected.insert("field2".to_string(), "World".to_string()); |
| 307 | + assert_eq!(res11, expected); |
| 308 | + // REMOVE_END |
| 309 | + }, |
| 310 | + Err(e) => { |
| 311 | + println!("Error getting all hash fields: {e}"); |
| 312 | + return; |
| 313 | + } |
| 314 | + } |
| 315 | + |
| 316 | + // REMOVE_START |
| 317 | + let _: Result<i32, _> = r.del("myhash").await; |
| 318 | + // REMOVE_END |
| 319 | + // STEP_END |
| 320 | + |
| 321 | + // STEP_START hvals |
| 322 | + let hash_fields = [ |
| 323 | + ("field1", "Hello"), |
| 324 | + ("field2", "World"), |
| 325 | + ]; |
| 326 | + |
| 327 | + if let Ok(_) = r.hset_multiple::<&str, &str, &str, String>("myhash", &hash_fields).await { |
| 328 | + // Fields set successfully |
| 329 | + } |
| 330 | + |
| 331 | + match r.hvals("myhash").await { |
| 332 | + Ok(res11) => { |
| 333 | + let res11: Vec<String> = res11; |
| 334 | + println!("{res11:?}"); // >>> ["Hello", "World"] |
| 335 | + // REMOVE_START |
| 336 | + let mut sorted_res = res11.clone(); |
| 337 | + sorted_res.sort(); |
| 338 | + let mut expected = vec!["Hello".to_string(), "World".to_string()]; |
| 339 | + expected.sort(); |
| 340 | + assert_eq!(sorted_res, expected); |
| 341 | + // REMOVE_END |
| 342 | + }, |
| 343 | + Err(e) => { |
| 344 | + println!("Error getting hash values: {e}"); |
| 345 | + return; |
| 346 | + } |
| 347 | + } |
| 348 | + |
| 349 | + // REMOVE_START |
| 350 | + let _: Result<i32, _> = r.del("myhash").await; |
| 351 | + // REMOVE_END |
| 352 | + // STEP_END |
| 353 | + } |
| 354 | +} |
0 commit comments