@@ -54,66 +54,65 @@ def test_create_user_from_context_only_user_id(self):
5454 self .assertEqual (user .user_id , test_user_id )
5555
5656 def test_create_user_from_context_with_user_id_attribute (self ):
57- test_user_id = "userId-12345"
57+ """Comprehensive test for userId attribute support including priority, functionality, and error cases"""
58+ targeting_key_id = "targeting-12345"
59+ user_id = "user_id-12345"
60+ user_id_attr = "userId-12345"
5861
59- # Test userId as the only user ID source
62+ # Test 1: userId as the only user ID source
6063 context = EvaluationContext (
61- targeting_key = None , attributes = {"userId" : test_user_id }
64+ targeting_key = None , attributes = {"userId" : user_id_attr }
6265 )
6366 user = DevCycleUser .create_user_from_context (context )
6467 self .assertIsNotNone (user )
65- self .assertEqual (user .user_id , test_user_id )
66-
67- # Test that userId is excluded from custom data when used as user ID
68- self .assertIsNone (user .customData )
69-
70- def test_create_user_from_context_user_id_priority (self ):
71- targeting_key_id = "targeting-12345"
72- user_id = "user_id-12345"
73- user_id_attr = "userId-12345"
68+ self .assertEqual (user .user_id , user_id_attr )
7469
75- # Test targeting_key takes precedence over user_id and userId
70+ # Test 2: Priority order - targeting_key > user_id > userId
7671 context = EvaluationContext (
7772 targeting_key = targeting_key_id ,
7873 attributes = {"user_id" : user_id , "userId" : user_id_attr }
7974 )
8075 user = DevCycleUser .create_user_from_context (context )
8176 self .assertEqual (user .user_id , targeting_key_id )
8277
83- # Test user_id takes precedence over userId
8478 context = EvaluationContext (
8579 targeting_key = None ,
8680 attributes = {"user_id" : user_id , "userId" : user_id_attr }
8781 )
8882 user = DevCycleUser .create_user_from_context (context )
8983 self .assertEqual (user .user_id , user_id )
9084
91- # Test userId is used when targeting_key and user_id are not available
92- context = EvaluationContext (
93- targeting_key = None ,
94- attributes = {"userId" : user_id_attr }
95- )
96- user = DevCycleUser .create_user_from_context (context )
97- self .assertEqual (user .user_id , user_id_attr )
85+ # Test 3: Error cases - invalid userId types
86+ with self .assertRaises (TargetingKeyMissingError ):
87+ DevCycleUser .create_user_from_context (
88+ EvaluationContext (targeting_key = None , attributes = {"userId" : 12345 })
89+ )
90+
91+ with self .assertRaises (TargetingKeyMissingError ):
92+ DevCycleUser .create_user_from_context (
93+ EvaluationContext (targeting_key = None , attributes = {"userId" : None })
94+ )
9895
99- def test_create_user_from_context_user_id_fields_always_excluded (self ):
96+ def test_create_user_from_context_user_id_exclusion (self ):
97+ """Test that all user ID fields (targeting_key, user_id, userId) are excluded from custom data"""
10098 targeting_key_id = "targeting-12345"
101- user_id_attr = "userId-12345"
10299 user_id = "user_id-12345"
100+ user_id_attr = "userId-12345"
103101
104- # When targeting_key is used, both user_id and userId should be excluded from custom data
102+ # Test exclusion when targeting_key is used
105103 context = EvaluationContext (
106104 targeting_key = targeting_key_id ,
107- attributes = {"user_id" : user_id , "userId" : user_id_attr , "other_field" : "value" }
105+ attributes = {"user_id" : user_id , "userId" : user_id_attr , "targeting_key" : "should-be-excluded" , " other_field" : "value" }
108106 )
109107 user = DevCycleUser .create_user_from_context (context )
110108 self .assertEqual (user .user_id , targeting_key_id )
111109 self .assertIsNotNone (user .customData )
112110 self .assertNotIn ("user_id" , user .customData )
113111 self .assertNotIn ("userId" , user .customData )
112+ self .assertNotIn ("targeting_key" , user .customData )
114113 self .assertEqual (user .customData ["other_field" ], "value" )
115114
116- # When user_id is used, userId should still be excluded from custom data
115+ # Test exclusion when user_id is used
117116 context = EvaluationContext (
118117 targeting_key = None ,
119118 attributes = {"user_id" : user_id , "userId" : user_id_attr , "other_field" : "value" }
@@ -125,24 +124,7 @@ def test_create_user_from_context_user_id_fields_always_excluded(self):
125124 self .assertNotIn ("userId" , user .customData )
126125 self .assertEqual (user .customData ["other_field" ], "value" )
127126
128- def test_create_user_from_context_targeting_key_excluded_from_attributes (self ):
129- targeting_key_id = "targeting-12345"
130-
131- # When targeting_key appears in attributes, it should be excluded from custom data
132- context = EvaluationContext (
133- targeting_key = targeting_key_id ,
134- attributes = {"targeting_key" : "should-be-excluded" , "other_field" : "value" }
135- )
136- user = DevCycleUser .create_user_from_context (context )
137- self .assertEqual (user .user_id , targeting_key_id )
138- self .assertIsNotNone (user .customData )
139- self .assertNotIn ("targeting_key" , user .customData )
140- self .assertEqual (user .customData ["other_field" ], "value" )
141-
142- def test_create_user_from_context_user_id_attr_excluded_when_used (self ):
143- user_id_attr = "userId-12345"
144-
145- # When userId is used as user ID, it should be excluded from custom data
127+ # Test exclusion when userId is used
146128 context = EvaluationContext (
147129 targeting_key = None ,
148130 attributes = {"userId" : user_id_attr , "other_field" : "value" }
@@ -153,23 +135,6 @@ def test_create_user_from_context_user_id_attr_excluded_when_used(self):
153135 self .assertNotIn ("userId" , user .customData )
154136 self .assertEqual (user .customData ["other_field" ], "value" )
155137
156- def test_create_user_from_context_invalid_user_id_attr_types (self ):
157- # Test non-string userId values are ignored and cause error
158- with self .assertRaises (TargetingKeyMissingError ):
159- DevCycleUser .create_user_from_context (
160- EvaluationContext (targeting_key = None , attributes = {"userId" : 12345 })
161- )
162-
163- with self .assertRaises (TargetingKeyMissingError ):
164- DevCycleUser .create_user_from_context (
165- EvaluationContext (targeting_key = None , attributes = {"userId" : None })
166- )
167-
168- with self .assertRaises (TargetingKeyMissingError ):
169- DevCycleUser .create_user_from_context (
170- EvaluationContext (targeting_key = None , attributes = {"userId" : True })
171- )
172-
173138 def test_create_user_from_context_with_attributes (self ):
174139 test_user_id = "12345"
175140 context = EvaluationContext (
0 commit comments