Skip to content

Commit 806d0ca

Browse files
committed
feat: support caller unit testing
1 parent bab880b commit 806d0ca

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

src/evalutate.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,19 @@ impl Toggle {
258258
}
259259
sids
260260
}
261+
262+
pub fn new_for_test(key: String, val: Value) -> Self {
263+
Self {
264+
key,
265+
enabled: true,
266+
default_serve: Serve::Select(0),
267+
disabled_serve: Serve::Select(0),
268+
variations: vec![val],
269+
version: 0,
270+
for_client: false,
271+
rules: vec![],
272+
}
273+
}
261274
}
262275

263276
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]

src/feature_probe.rs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
use parking_lot::RwLock;
2+
use serde_json::Value;
3+
use std::collections::HashMap;
4+
use std::fmt::Debug;
5+
use std::sync::Arc;
6+
use std::time::Duration;
7+
use tracing::info;
8+
use url::Url;
9+
10+
#[cfg(feature = "internal")]
11+
use crate::evalutate::Segment;
112
use crate::evalutate::{EvalDetail, Repository};
213
use crate::sync::Synchronizer;
314
use crate::user::FPUser;
4-
use crate::{FPDetail, FPError, SdkAuthorization};
15+
use crate::{FPDetail, FPError, SdkAuthorization, Toggle};
516
#[cfg(feature = "event")]
617
use feature_probe_event_std::event::AccessEvent;
718
#[cfg(feature = "event")]
@@ -14,22 +25,8 @@ use feature_probe_event_tokio::event::AccessEvent;
1425
use feature_probe_event_tokio::recorder::unix_timestamp;
1526
#[cfg(feature = "event_tokio")]
1627
use feature_probe_event_tokio::recorder::EventRecorder;
17-
use parking_lot::RwLock;
18-
use serde_json::Value;
19-
use std::sync::Arc;
20-
use std::time::Duration;
21-
use tracing::info;
22-
use url::Url;
23-
24-
#[cfg(feature = "internal")]
25-
use crate::evalutate::Segment;
26-
#[cfg(feature = "internal")]
27-
use crate::evalutate::Toggle;
2828
#[cfg(feature = "use_tokio")]
2929
use reqwest::Client;
30-
#[cfg(feature = "internal")]
31-
use std::collections::HashMap;
32-
use std::fmt::Debug;
3330

3431
#[derive(Debug, Default, Clone)]
3532
pub struct FeatureProbe {
@@ -54,6 +51,25 @@ impl FeatureProbe {
5451
}
5552
}
5653

54+
pub fn new_for_test(toggle: &str, value: Value) -> Self {
55+
let mut toggles = HashMap::new();
56+
toggles.insert(toggle.to_owned(), value);
57+
FeatureProbe::new_for_tests(toggles)
58+
}
59+
60+
pub fn new_for_tests(toggles: HashMap<String, Value>) -> Self {
61+
let mut repo = Repository::default();
62+
for (key, val) in toggles {
63+
repo.toggles
64+
.insert(key.clone(), Toggle::new_for_test(key, val));
65+
}
66+
67+
Self {
68+
repo: Arc::new(RwLock::new(repo)),
69+
..Default::default()
70+
}
71+
}
72+
5773
pub fn bool_value(&self, toggle: &str, user: &FPUser, default: bool) -> bool {
5874
self.generic_detail(toggle, user, default, |v| v.as_bool())
5975
.value
@@ -374,6 +390,20 @@ mod tests {
374390
assert_eq!(d.rule_index, None);
375391
}
376392

393+
#[test]
394+
fn test_for_ut() {
395+
let fp = FeatureProbe::new_for_test("toggle_1", Value::Bool(false));
396+
let u = FPUser::new("key");
397+
assert_eq!(fp.bool_value("toggle_1", &u, true), false);
398+
399+
let mut toggles: HashMap<String, Value> = HashMap::new();
400+
toggles.insert("toggle_2".to_owned(), json!(12.5));
401+
toggles.insert("toggle_3".to_owned(), json!("value"));
402+
let fp = FeatureProbe::new_for_tests(toggles);
403+
assert_eq!(fp.number_value("toggle_2", &u, 20.0), 12.5);
404+
assert_eq!(fp.string_value("toggle_3", &u, "val".to_owned()), "value");
405+
}
406+
377407
fn load_local_json(file: &str) -> Result<Repository, FPError> {
378408
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
379409
path.push(file);

src/sync.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ impl Inner {
129129
//TODO: report failure
130130
match ureq::get(self.toggles_url.as_str())
131131
.set(
132-
"authorization",
132+
"Authorization",
133133
self.auth.to_str().expect("already valid header value"),
134134
)
135-
.set("user-agent", &*crate::USER_AGENT)
135+
.set("User-Agent", &*crate::USER_AGENT)
136136
.timeout(self.refresh_interval)
137137
.call()
138138
{
@@ -211,7 +211,6 @@ mod tests {
211211
) -> Json<Repository> {
212212
assert_eq!(sdk_key, "sdk-key");
213213
assert!(user_agent.to_string().len() > 0);
214-
println!(">>> {}", user_agent);
215214
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
216215
path.push("resources/fixtures/repo.json");
217216
let json_str = fs::read_to_string(path).unwrap();

0 commit comments

Comments
 (0)