@@ -110,22 +110,22 @@ def test_get_built_in_attribute_by_name(self):
110110 assert c .get ('kind' ) == 'b'
111111 assert c .get ('name' ) == 'c'
112112 assert c .get ('anonymous' ) is True
113-
113+
114114 def test_get_unknown_attribute (self ):
115115 c = Context .create ('a' )
116116 assert c .get ('b' ) is None
117-
117+
118118 def test_private_attributes (self ):
119119 assert list (Context .create ('a' ).private_attributes ) == []
120120
121121 c = Context .builder ('a' ).private ('b' , '/c/d' ).private ('e' ).build ()
122122 assert list (c .private_attributes ) == ['b' , '/c/d' , 'e' ]
123-
123+
124124 def test_fully_qualified_key (self ):
125125 assert Context .create ('key1' ).fully_qualified_key == 'key1'
126126 assert Context .create ('key1' , 'kind1' ).fully_qualified_key == 'kind1:key1'
127127 assert Context .create ('key%with:things' , 'kind1' ).fully_qualified_key == 'kind1:key%25with%3Athings'
128-
128+
129129 def test_builder_from_context (self ):
130130 c1 = Context .builder ('a' ).kind ('kind1' ).name ('b' ).set ('c' , True ).private ('d' ).build ()
131131 b = Context .builder_from_context (c1 )
@@ -167,7 +167,7 @@ def _assert_contexts_from_factory_equal(fn):
167167 Context .create_multi (Context .create ('a' , 'kind1' ), Context .create ('b' , 'kind2' ))
168168 assert Context .create_multi (Context .create ('a' , 'kind1' ), Context .create ('b' , 'kind2' )) != \
169169 Context .create ('a' , 'kind1' )
170-
170+
171171 _assert_contexts_from_factory_equal (lambda : Context .create ('invalid' , 'kind' ))
172172 assert Context .create ('invalid' , 'kind' ) != Context .create_multi () # different errors
173173
@@ -195,10 +195,10 @@ def test_json_decoding(self):
195195 Context .builder ('key1' ).kind ('kind1' ).anonymous (True ).build ()
196196 assert Context .from_dict ({'kind' : 'kind1' , 'key' : 'key1' , '_meta' : {'privateAttributes' : ['b' ]}}) == \
197197 Context .builder ('key1' ).kind ('kind1' ).private ('b' ).build ()
198-
198+
199199 assert Context .from_dict ({'kind' : 'multi' , 'kind1' : {'key' : 'key1' }, 'kind2' : {'key' : 'key2' }}) == \
200200 Context .create_multi (Context .create ('key1' , 'kind1' ), Context .create ('key2' , 'kind2' ))
201-
201+
202202 assert_context_invalid (Context .from_dict ({'kind' : 'kind1' }))
203203 assert_context_invalid (Context .from_dict ({'kind' : 'kind1' , 'key' : 3 }))
204204 assert_context_invalid (Context .from_dict ({'kind' : 'multi' }))
@@ -256,34 +256,70 @@ class TestContextErrors:
256256 def test_key_empty_string (self ):
257257 assert_context_invalid (Context .create ('' ))
258258 assert_context_invalid (Context .builder ('' ).build ())
259-
259+
260260 @pytest .mark .parametrize ('kind' , ['kind' , 'multi' , 'b$c' , '' ])
261261 def test_kind_invalid_strings (self , kind ):
262262 assert_context_invalid (Context .create ('a' , kind ))
263263 assert_context_invalid (Context .builder ('a' ).kind (kind ).build ())
264-
264+
265265 def test_create_multi_with_no_contexts (self ):
266266 assert_context_invalid (Context .create_multi ())
267-
267+
268268 def test_multi_builder_with_no_contexts (self ):
269269 assert_context_invalid (Context .multi_builder ().build ())
270270
271271 def test_create_multi_with_duplicate_kind (self ):
272272 c1 = Context .create ('a' , 'kind1' )
273273 c2 = Context .create ('b' , 'kind1' )
274274 assert_context_invalid (Context .create_multi (c1 , c2 ))
275-
275+
276276 def test_multi_builder_with_duplicate_kind (self ):
277277 c1 = Context .create ('a' , 'kind1' )
278278 c2 = Context .create ('b' , 'kind1' )
279279 assert_context_invalid (Context .multi_builder ().add (c1 ).add (c2 ).build ())
280-
280+
281281 def test_create_multi_with_invalid_context (self ):
282282 c1 = Context .create ('a' , 'kind1' )
283283 c2 = Context .create ('' )
284284 assert_context_invalid (Context .create_multi (c1 , c2 ))
285-
285+
286286 def test_multi_builder_with_invalid_context (self ):
287287 c1 = Context .create ('a' , 'kind1' )
288288 c2 = Context .create ('' )
289289 assert_context_invalid (Context .multi_builder ().add (c1 ).add (c2 ).build ())
290+
291+
292+ class TestAnonymousRedaction :
293+ def test_redacting_anonoymous_leads_to_invalid_context (self ):
294+ original = Context .builder ('a' ).anonymous (True ).build ()
295+ c = original .without_anonymous_contexts ()
296+
297+ assert_context_invalid (c )
298+
299+ def test_redacting_non_anonymous_does_not_change_context (self ):
300+ original = Context .builder ('a' ).anonymous (False ).build ()
301+ c = original .without_anonymous_contexts ()
302+
303+ assert_context_valid (c )
304+ assert c == original
305+
306+ def test_can_find_non_anonymous_contexts_from_multi (self ):
307+ anon = Context .builder ('a' ).anonymous (True ).build ()
308+ nonanon = Context .create ('b' , 'kind2' )
309+ mc = Context .create_multi (anon , nonanon )
310+
311+ filtered = mc .without_anonymous_contexts ()
312+
313+ assert_context_valid (filtered )
314+ assert filtered .individual_context_count == 1
315+ assert filtered .key == 'b'
316+ assert filtered .kind == 'kind2'
317+
318+ def test_can_filter_all_from_multi (self ):
319+ a = Context .builder ('a' ).anonymous (True ).build ()
320+ b = Context .builder ('b' ).anonymous (True ).build ()
321+ mc = Context .create_multi (a , b )
322+
323+ filtered = mc .without_anonymous_contexts ()
324+
325+ assert_context_invalid (filtered )
0 commit comments