1010TEST_SPIDER_NAME = 'hs-test-spider'
1111TEST_FRONTIER_NAME = 'test'
1212TEST_FRONTIER_SLOT = 'site.com'
13- TEST_BOTGROUPS = [ 'python-hubstorage-test' , 'g1' ]
13+ TEST_BOTGROUP = 'python-hubstorage-test'
1414TEST_COLLECTION_NAME = "test_collection_123"
1515TEST_AUTH = os .getenv ('HS_AUTH' , 'f' * 32 )
1616TEST_ENDPOINT = os .getenv ('HS_ENDPOINT' , 'http://storage.vm.scrapinghub.com' )
1717
1818VCR_CASSETES_DIR = 'tests/hubstorage/cassetes'
1919
20+ my_vcr = vcr .VCR (cassette_library_dir = VCR_CASSETES_DIR , record_mode = 'once' )
21+
2022
2123@pytest .fixture (scope = 'session' )
2224def hsclient ():
@@ -28,44 +30,44 @@ def hsproject(hsclient):
2830 return hsclient .get_project (TEST_PROJECT_ID )
2931
3032
31- @pytest .fixture
33+ #@my_vcr.use_cassette()
34+ @pytest .fixture (scope = 'session' )
3235def hsspiderid (hsproject ):
33- # it's important that scope for the fixture is per test ('function'):
34- # all the current per-session fixtures don't do any external requests,
35- # it allows to use vcrpy inside a per-test fixture
3636 return str (hsproject .ids .spider (TEST_SPIDER_NAME , create = 1 ))
3737
3838
39- @pytest .fixture
40- def hscollection (hsproject ):
41- return hsproject .collections .new_store (TEST_COLLECTION_NAME )
42-
4339
40+ #@my_vcr.use_cassette()
4441@pytest .fixture (autouse = True , scope = 'session' )
45- def setup_session (hsclient ):
42+ def setup_session (hsclient , hsproject , hscollection ):
43+ set_testbotgroup (hsproject )
44+ remove_all_jobs (hsproject )
4645 yield
46+ remove_all_jobs (hsproject )
47+ unset_testbotgroup (hsproject )
4748 hsclient .close ()
4849
4950
50- @pytest .fixture
51- def vcr_instance (scope = 'session' ):
52- return vcr .VCR (cassette_library_dir = VCR_CASSETES_DIR , record_mode = 'once' )
53-
54-
5551@pytest .fixture (autouse = True )
56- def setup_test ( hsclient , hsproject , hscollection , request , vcr_instance ):
52+ def setup_vcrpy_per_test ( request , hsproject ):
5753 # generates names like "test_module/test_function.yaml"
5854 # vcrpy creates the cassetes automatically under VCR_CASSETES_DIR
5955 cassette_name = '{}/{}.yaml' .format (
6056 request .function .__module__ .split ('.' )[- 1 ],
6157 request .function .__name__
6258 )
63- with vcr_instance .use_cassette (cassette_name ):
64- _set_testbotgroup (hsproject )
65- clean_environment (hsproject , hscollection )
66- yield
67- clean_environment (hsproject , hscollection )
68- _unset_testbotgroup (hsproject )
59+ #with my_vcr.use_cassette(cassette_name):
60+ yield
61+
62+
63+ #@my_vcr.use_cassette()
64+ @pytest .fixture (scope = 'session' )
65+ def hscollection (hsproject ):
66+ collection = get_test_collection (hsproject )
67+ clean_collection (collection )
68+ yield collection
69+ clean_collection (collection )
70+
6971
7072# ----------------------------------------------------------------------------
7173
@@ -81,17 +83,7 @@ def start_job(hsproject, **startparams):
8183# Clean environment section
8284
8385
84- def clean_environment (hsproject , hscollection ):
85- _remove_all_jobs (hsproject )
86- # drop all items in test collection
87- for item in hscollection .iter_values ():
88- hscollection .delete (item ['_key' ])
89- # delete frontier slot
90- frontier = hsproject .frontier
91- frontier .delete_slot (TEST_FRONTIER_NAME , TEST_FRONTIER_SLOT )
92-
93-
94- def _remove_all_jobs (hsproject ):
86+ def remove_all_jobs (hsproject ):
9587 for k in list (hsproject .settings .keys ()):
9688 if k != 'botgroups' :
9789 del hsproject .settings [k ]
@@ -114,23 +106,32 @@ def _remove_job(hsproject, jobkey):
114106 assert jobkey .startswith (TEST_PROJECT_ID ), jobkey
115107 hsproject .jobs .apidelete (jobkey .partition ('/' )[2 ])
116108
109+ # Collection helpers section
110+
111+
112+ def get_test_collection (project ):
113+ return project .collections .new_store (TEST_COLLECTION_NAME )
114+
115+
116+ def clean_collection (collection ):
117+ for item in collection .iter_values ():
118+ collection .delete (item ['_key' ])
119+
117120
118121# Botgroups helpers section
119122
120123
121- def _set_testbotgroup (hsproject ):
122- hsproject .settings .apipost (jl = {'botgroups' : [TEST_BOTGROUPS [ 0 ] ]})
124+ def set_testbotgroup (hsproject ):
125+ hsproject .settings .apipost (jl = {'botgroups' : [TEST_BOTGROUP ]})
123126 # Additional step to populate JobQ's botgroups table
124- for botgroup in TEST_BOTGROUPS :
125- url = urlpathjoin (TEST_ENDPOINT , 'botgroups' , botgroup , 'max_running' )
126- requests .post (url , auth = hsproject .auth , data = 'null' )
127+ url = urlpathjoin (TEST_ENDPOINT , 'botgroups' , TEST_BOTGROUP , 'max_running' )
128+ requests .post (url , auth = hsproject .auth , data = 'null' )
127129 hsproject .settings .expire ()
128130
129131
130- def _unset_testbotgroup (hsproject ):
132+ def unset_testbotgroup (hsproject ):
131133 hsproject .settings .apidelete ('botgroups' )
132134 hsproject .settings .expire ()
133135 # Additional step to delete botgroups in JobQ
134- for botgroup in TEST_BOTGROUPS :
135- url = urlpathjoin (TEST_ENDPOINT , 'botgroups' , botgroup )
136- requests .delete (url , auth = hsproject .auth )
136+ url = urlpathjoin (TEST_ENDPOINT , 'botgroups' , TEST_BOTGROUP )
137+ requests .delete (url , auth = hsproject .auth )
0 commit comments