mod common; use common::run_test; use uuid::Uuid; use launchdarkly_server_sdk::{DataKind, PersistentDataStoreFactory, SerializedItem}; use launchdarkly_server_sdk_redis::RedisPersistentDataStoreFactory; #[test] fn can_get_an_existing_item() { let prefix = Uuid::new_v4().hyphenated().to_string(); run_test(&prefix, || { let mut factory = RedisPersistentDataStoreFactory::new(); factory.prefix(&prefix); let mut store = factory.create_persistent_data_store().expect("FAIL"); let item = SerializedItem { version: 1, deleted: false, serialized_item: "test-item".into(), }; assert!(store.upsert(DataKind::Flag, "test-item", item).is_ok()); let retrieved_item = store.flag("test-item").unwrap(); assert!(retrieved_item.is_some()); assert_eq!("test-item", &retrieved_item.unwrap().serialized_item); }) } #[test] fn handles_nonexistent_item() { let prefix = Uuid::new_v4().hyphenated().to_string(); run_test(&prefix, || { let mut factory = RedisPersistentDataStoreFactory::new(); factory.prefix(&prefix); let store = factory.create_persistent_data_store().expect("FAIL"); let retrieved_item = store.flag("test-item").unwrap(); assert!(retrieved_item.is_none()); }) } #[test] fn all_flags_returns_every_inserted_flag() { let prefix = Uuid::new_v4().hyphenated().to_string(); run_test(&prefix, || { let mut factory = RedisPersistentDataStoreFactory::new(); factory.prefix(&prefix); let mut store = factory.create_persistent_data_store().expect("FAIL"); for i in 0..3 { let item = SerializedItem { version: i, deleted: false, serialized_item: i.to_string(), }; assert!(store.upsert(DataKind::Flag, &i.to_string(), item).is_ok()); } let flags = store.all_flags().unwrap(); assert_eq!(3, flags.values().count()); assert!(flags.contains_key("0")); assert!(flags.contains_key("1")); assert!(flags.contains_key("2")); }) }