@@ -90,6 +90,13 @@ def _validate_last_impressions(self, client, *to_validate):
9090 as_tup_set = set ((i .feature_name , i .matching_key , i .treatment ) for i in impressions )
9191 assert as_tup_set == set (to_validate )
9292
93+ def _validate_last_events (self , client , * to_validate ):
94+ """Validate the last N impressions are present disregarding the order."""
95+ event_storage = client ._factory ._get_storage ('events' )
96+ events = event_storage .pop_many (len (to_validate ))
97+ as_tup_set = set ((i .key , i .traffic_type_name , i .event_type_id , i .value , str (i .properties )) for i in events )
98+ assert as_tup_set == set (to_validate )
99+
93100 def test_get_treatment (self ):
94101 """Test client.get_treatment()."""
95102 try :
@@ -269,6 +276,21 @@ def test_get_treatments_with_config(self):
269276 ('sample_feature' , 'invalidKey' , 'off' ),
270277 )
271278
279+ def test_track (self ):
280+ """Test client.track()."""
281+ try :
282+ client = self .factory .client ()
283+ except :
284+ pass
285+ assert (client .track ('user1' , 'user' , 'conversion' , 1 , {"prop1" : "value1" }))
286+ assert (not client .track (None , 'user' , 'conversion' ))
287+ assert (not client .track ('user1' , None , 'conversion' ))
288+ assert (not client .track ('user1' , 'user' , None ))
289+ self ._validate_last_events (
290+ client ,
291+ ('user1' , 'user' , 'conversion' , 1 , "{'prop1': 'value1'}" )
292+ )
293+
272294 def test_manager_methods (self ):
273295 """Test manager.split/splits."""
274296 try :
@@ -358,6 +380,13 @@ def _validate_last_impressions(self, client, *to_validate):
358380 as_tup_set = set ((i .feature_name , i .matching_key , i .treatment ) for i in impressions )
359381 assert as_tup_set == set (to_validate )
360382
383+ def _validate_last_events (self , client , * to_validate ):
384+ """Validate the last N impressions are present disregarding the order."""
385+ event_storage = client ._factory ._get_storage ('events' )
386+ events = event_storage .pop_many (len (to_validate ))
387+ as_tup_set = set ((i .key , i .traffic_type_name , i .event_type_id , i .value , str (i .properties )) for i in events )
388+ assert as_tup_set == set (to_validate )
389+
361390 def test_get_treatment (self ):
362391 """Test client.get_treatment()."""
363392 client = self .factory .client ()
@@ -529,6 +558,17 @@ def test_manager_methods(self):
529558 assert len (manager .split_names ()) == 7
530559 assert len (manager .splits ()) == 7
531560
561+ def test_track (self ):
562+ """Test client.track()."""
563+ client = self .factory .client ()
564+ assert (client .track ('user1' , 'user' , 'conversion' , 1 , {"prop1" : "value1" }))
565+ assert (not client .track (None , 'user' , 'conversion' ))
566+ assert (not client .track ('user1' , None , 'conversion' ))
567+ assert (not client .track ('user1' , 'user' , None ))
568+ self ._validate_last_events (
569+ client ,
570+ ('user1' , 'user' , 'conversion' , 1 , "{'prop1': 'value1'}" )
571+ )
532572
533573class RedisIntegrationTests (object ):
534574 """Redis storage-based integration tests."""
@@ -581,6 +621,20 @@ def setup_method(self):
581621 telemetry_init_producer = telemetry_producer .get_telemetry_init_producer (),
582622 ) # pylint:disable=attribute-defined-outside-init
583623
624+ def _validate_last_events (self , client , * to_validate ):
625+ """Validate the last N impressions are present disregarding the order."""
626+ event_storage = client ._factory ._get_storage ('events' )
627+ redis_client = event_storage ._redis
628+ events_raw = [
629+ json .loads (redis_client .lpop (event_storage ._EVENTS_KEY_TEMPLATE ))
630+ for _ in to_validate
631+ ]
632+ as_tup_set = set (
633+ (i ['e' ]['key' ], i ['e' ]['trafficTypeName' ], i ['e' ]['eventTypeId' ], i ['e' ]['value' ], str (i ['e' ]['properties' ]))
634+ for i in events_raw
635+ )
636+ assert as_tup_set == set (to_validate )
637+
584638 def _validate_last_impressions (self , client , * to_validate ):
585639 """Validate the last N impressions are present disregarding the order."""
586640 imp_storage = client ._factory ._get_storage ('impressions' )
@@ -765,6 +819,18 @@ def test_get_treatments_with_config(self):
765819 ('sample_feature' , 'invalidKey' , 'off' ),
766820 )
767821
822+ def test_track (self ):
823+ """Test client.track()."""
824+ client = self .factory .client ()
825+ assert (client .track ('user1' , 'user' , 'conversion' , 1 , {"prop1" : "value1" }))
826+ assert (not client .track (None , 'user' , 'conversion' ))
827+ assert (not client .track ('user1' , None , 'conversion' ))
828+ assert (not client .track ('user1' , 'user' , None ))
829+ self ._validate_last_events (
830+ client ,
831+ ('user1' , 'user' , 'conversion' , 1 , "{'prop1': 'value1'}" )
832+ )
833+
768834 def test_manager_methods (self ):
769835 """Test manager.split/splits."""
770836 try :
0 commit comments