Skip to content

Commit 63f0b8c

Browse files
committed
Clean conftest for sh.hs tests
1 parent bbd4687 commit 63f0b8c

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

tests/hubstorage/conftest.py

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@
1212
TEST_FRONTIER_SLOT = 'site.com'
1313
TEST_BOTGROUPS = ['python-hubstorage-test', 'g1']
1414
TEST_COLLECTION_NAME = "test_collection_123"
15-
16-
VCR_CASSETES_DIR = 'tests/hubstorage/cassetes'
17-
1815
TEST_AUTH = os.getenv('HS_AUTH', 'f' * 32)
1916
TEST_ENDPOINT = os.getenv('HS_ENDPOINT', 'http://storage.vm.scrapinghub.com')
2017

18+
VCR_CASSETES_DIR = 'tests/hubstorage/cassetes'
2119

22-
@pytest.fixture
23-
def vcr_instance(scope='session'):
24-
return vcr.VCR(
25-
cassette_library_dir=VCR_CASSETES_DIR,
26-
record_mode='once',
27-
)
2820

2921
@pytest.fixture(scope='session')
3022
def hsclient():
@@ -38,6 +30,9 @@ def hsproject(hsclient):
3830

3931
@pytest.fixture
4032
def 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
4136
return str(hsproject.ids.spider(TEST_SPIDER_NAME, create=1))
4237

4338

@@ -47,56 +42,53 @@ def hscollection(hsproject):
4742

4843

4944
@pytest.fixture(autouse=True, scope='session')
50-
def setup_session(hsclient, hsproject):
45+
def setup_session(hsclient):
5146
yield
5247
hsclient.close()
5348

5449

50+
@pytest.fixture
51+
def vcr_instance(scope='session'):
52+
return vcr.VCR(cassette_library_dir=VCR_CASSETES_DIR, record_mode='once')
53+
54+
5555
@pytest.fixture(autouse=True)
5656
def setup_test(hsclient, hsproject, hscollection, request, vcr_instance):
57+
# generates names like "test_module/test_function.yaml"
58+
# vcrpy creates the cassetes automatically under VCR_CASSETES_DIR
5759
cassette_name = '{}/{}.yaml'.format(
5860
request.function.__module__.split('.')[-1],
5961
request.function.__name__
6062
)
6163
with vcr_instance.use_cassette(cassette_name):
6264
_set_testbotgroup(hsproject)
63-
_remove_all_jobs(hsproject)
64-
_clean_collection(hscollection)
65-
_delete_frontier_slot(hsproject)
65+
clean_environment(hsproject, hscollection)
6666
yield
67-
_remove_all_jobs(hsproject)
68-
_clean_collection(hscollection)
69-
_delete_frontier_slot(hsproject)
67+
clean_environment(hsproject, hscollection)
7068
_unset_testbotgroup(hsproject)
7169

7270
# ----------------------------------------------------------------------------
7371

74-
def _delete_frontier_slot(hsproject):
75-
frontier = hsproject.frontier
76-
frontier.delete_slot(TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT)
77-
7872

79-
def _clean_collection(hscollection):
80-
for item in hscollection.iter_values():
81-
hscollection.delete(item['_key'])
73+
def start_job(hsproject, **startparams):
74+
jobdata = hsproject.jobq.start(**startparams)
75+
if jobdata:
76+
jobkey = jobdata.pop('key')
77+
jobauth = (jobkey, jobdata['auth'])
78+
return hsproject.get_job(jobkey, jobauth=jobauth, metadata=jobdata)
8279

8380

84-
def _set_testbotgroup(hsproject):
85-
hsproject.settings.apipost(jl={'botgroups': [TEST_BOTGROUPS[0]]})
86-
# Additional step to populate JobQ's botgroups table
87-
for botgroup in TEST_BOTGROUPS:
88-
url = urlpathjoin(TEST_ENDPOINT, 'botgroups', botgroup, 'max_running')
89-
requests.post(url, auth=hsproject.auth, data='null')
90-
hsproject.settings.expire()
81+
# Clean environment section
9182

9283

93-
def _unset_testbotgroup(hsproject):
94-
hsproject.settings.apidelete('botgroups')
95-
hsproject.settings.expire()
96-
# Additional step to delete botgroups in JobQ
97-
for botgroup in TEST_BOTGROUPS:
98-
url = urlpathjoin(TEST_ENDPOINT, 'botgroups', botgroup)
99-
requests.delete(url, auth=hsproject.auth)
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)
10092

10193

10294
def _remove_all_jobs(hsproject):
@@ -118,17 +110,27 @@ def _remove_all_jobs(hsproject):
118110
def _remove_job(hsproject, jobkey):
119111
hsproject.jobq.finish(jobkey)
120112
hsproject.jobq.delete(jobkey)
121-
_delete_job(hsproject, jobkey)
122-
123-
124-
def _delete_job(hsproject, jobkey):
113+
# delete job
125114
assert jobkey.startswith(TEST_PROJECT_ID), jobkey
126115
hsproject.jobs.apidelete(jobkey.partition('/')[2])
127116

128117

129-
def start_job(hsproject, **startparams):
130-
jobdata = hsproject.jobq.start(**startparams)
131-
if jobdata:
132-
jobkey = jobdata.pop('key')
133-
jobauth = (jobkey, jobdata['auth'])
134-
return hsproject.get_job(jobkey, jobauth=jobauth, metadata=jobdata)
118+
# Botgroups helpers section
119+
120+
121+
def _set_testbotgroup(hsproject):
122+
hsproject.settings.apipost(jl={'botgroups': [TEST_BOTGROUPS[0]]})
123+
# 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+
hsproject.settings.expire()
128+
129+
130+
def _unset_testbotgroup(hsproject):
131+
hsproject.settings.apidelete('botgroups')
132+
hsproject.settings.expire()
133+
# 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)

0 commit comments

Comments
 (0)