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 ;
112use crate :: evalutate:: { EvalDetail , Repository } ;
213use crate :: sync:: Synchronizer ;
314use crate :: user:: FPUser ;
4- use crate :: { FPDetail , FPError , SdkAuthorization } ;
15+ use crate :: { FPDetail , FPError , SdkAuthorization , Toggle } ;
516#[ cfg( feature = "event" ) ]
617use feature_probe_event_std:: event:: AccessEvent ;
718#[ cfg( feature = "event" ) ]
@@ -14,22 +25,8 @@ use feature_probe_event_tokio::event::AccessEvent;
1425use feature_probe_event_tokio:: recorder:: unix_timestamp;
1526#[ cfg( feature = "event_tokio" ) ]
1627use 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" ) ]
2929use reqwest:: Client ;
30- #[ cfg( feature = "internal" ) ]
31- use std:: collections:: HashMap ;
32- use std:: fmt:: Debug ;
3330
3431#[ derive( Debug , Default , Clone ) ]
3532pub 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) ;
0 commit comments