use redis::{Client, Commands}; use std::panic; pub fn run_test(prefix: &str, test: T) -> () where T: FnOnce() -> () + panic::UnwindSafe, { reset_database(prefix, &make_client("redis://localhost:6379")); let result = panic::catch_unwind(|| test()); assert!(result.is_ok()) } pub fn make_client(url: &str) -> Client { Client::open(url).expect("FAIL") } pub fn reset_database(prefix: &str, client: &Client) { let mut connection = client.get_connection().expect("FAIL"); let keys: redis::Iter = connection .scan_match(format!("{}:*", prefix)) .expect("scan failed"); let keys = keys.into_iter().collect::>(); keys.into_iter().for_each(|key| { connection .del(key.clone()) .expect(&format!("Failed to delete {}", key)) }); } #[allow(dead_code)] pub fn tombstone(version: u64) -> String { format!( r#"{{ "key": "$deleted", "version": {}, "deleted": true }}"#, version ) } #[allow(dead_code)] pub fn basic_flag(key: &str, version: u64) -> String { format!( r#"{{ "key": "{}", "version": {}, "on": false, "targets": [], "rules": [], "prerequisites": [], "fallthrough": {{"variation": 1}}, "offVariation": 0, "variations": [false, true], "clientSideAvailability": {{ "usingEnvironmentId": false, "usingMobileKey": false }}, "salt": "salty" }}"#, key, version ) } #[allow(dead_code)] pub fn basic_segment(key: &str, version: u64) -> String { format!( r#"{{ "key": "{}", "included": ["alice"], "excluded": [], "rules": [], "salt": "salty", "version": {} }}"#, key, version ) }