44from configmanager .utils import not_set
55
66
7+ def sub_dict (dct , keys ):
8+ """
9+ Helper for tests that creates a dictionary from the given dictionary
10+ with just the listed keys included.
11+ """
12+ return {k : dct [k ] for k in keys if k in dct }
13+
14+
715def test_hooks_available_on_all_sections ():
816 config = Config ({
917 'uploads' : {
@@ -30,11 +38,11 @@ def test_not_found_hook():
3038
3139 @config .hooks .not_found
3240 def first_hook (* args , ** kwargs ):
33- calls .append (('first' , args , kwargs ))
41+ calls .append (('first' , args , sub_dict ( kwargs , ( 'section' , 'name' )) ))
3442
3543 @config .hooks .not_found
3644 def second_hook (* args , ** kwargs ):
37- calls .append (('second' , args , kwargs ))
45+ calls .append (('second' , args , sub_dict ( kwargs , ( 'section' , 'name' )) ))
3846
3947 assert len (calls ) == 0
4048
@@ -56,7 +64,7 @@ def second_hook(*args, **kwargs):
5664 # the hook handlers again, including any subsequent hook handlers as part of current event.
5765 @config .hooks .not_found
5866 def third_hook (* args , ** kwargs ):
59- calls .append (('third' , args , kwargs ))
67+ calls .append (('third' , args , sub_dict ( kwargs , ( 'section' , 'name' )) ))
6068
6169 assert kwargs ['section' ]
6270 assert kwargs ['name' ]
@@ -68,7 +76,7 @@ def third_hook(*args, **kwargs):
6876 # Fourth hook will never be called because the third hook already resolves the missing name
6977 @config .hooks .not_found
7078 def fourth_hook (* args , ** kwargs ):
71- calls .append (('fourth' , args , kwargs ))
79+ calls .append (('fourth' , args , sub_dict ( kwargs , ( 'section' , 'name' )) ))
7280
7381 assert len (calls ) == 4
7482
@@ -98,11 +106,11 @@ def test_item_added_to_section_hook():
98106
99107 @config .hooks .item_added_to_section
100108 def item_added_to_section (* args , ** kwargs ):
101- calls .append (('first' , args , kwargs ))
109+ calls .append (('first' , args , sub_dict ( kwargs , ( 'section' , 'subject' , 'alias' )) ))
102110
103111 @config .hooks .item_added_to_section
104112 def item_added_to_section2 (* args , ** kwargs ):
105- calls .append (('second' , args , kwargs ))
113+ calls .append (('second' , args , sub_dict ( kwargs , ( 'section' , 'subject' , 'alias' )) ))
106114
107115 assert calls == []
108116
@@ -191,11 +199,11 @@ def test_section_added_to_section_hook():
191199
192200 @config .hooks .section_added_to_section
193201 def section_added_to_section1 (* args , ** kwargs ):
194- calls .append (('on_root' , args , kwargs ))
202+ calls .append (('on_root' , args , sub_dict ( kwargs , ( 'section' , 'subject' , 'alias' )) ))
195203
196204 @config .uploads .hooks .section_added_to_section
197205 def section_added_to_section2 (* args , ** kwargs ):
198- calls .append (('on_uploads' , args , kwargs ))
206+ calls .append (('on_uploads' , args , sub_dict ( kwargs , ( 'section' , 'subject' , 'alias' )) ))
199207
200208 assert calls == []
201209
0 commit comments