Skip to content

Commit afb8338

Browse files
committed
Improve fixture for collection/frontier
1 parent b88ab2a commit afb8338

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

tests/hubstorage/conftest.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,46 @@ def hsproject(hsclient):
3030
return hsclient.get_project(TEST_PROJECT_ID)
3131

3232

33+
#@my_vcr.use_cassette()
3334
@pytest.fixture(scope='session')
3435
def hsspiderid(hsproject):
3536
return str(hsproject.ids.spider(TEST_SPIDER_NAME, create=1))
3637

3738

38-
@pytest.fixture(scope='session')
39-
def hscollection(hsproject):
40-
return hsproject.collections.new_store(TEST_COLLECTION_NAME)
41-
4239

43-
@my_vcr.use_cassette()
40+
#@my_vcr.use_cassette()
4441
@pytest.fixture(autouse=True, scope='session')
4542
def setup_session(hsclient, hsproject, hscollection):
46-
_set_testbotgroup(hsproject)
43+
set_testbotgroup(hsproject)
4744
yield
48-
_unset_testbotgroup(hsproject)
45+
remove_all_jobs(hsproject)
46+
unset_testbotgroup(hsproject)
4947
hsclient.close()
5048

5149

5250
@pytest.fixture(autouse=True)
53-
def setup_vcrpy_per_test(request, hsproject, hscollection):
51+
def setup_vcrpy_per_test(request, hsproject):
5452
# generates names like "test_module/test_function.yaml"
5553
# vcrpy creates the cassetes automatically under VCR_CASSETES_DIR
5654
cassette_name = '{}/{}.yaml'.format(
5755
request.function.__module__.split('.')[-1],
5856
request.function.__name__
5957
)
60-
with my_vcr.use_cassette(cassette_name):
61-
clean_environment(hsproject, hscollection)
62-
yield
63-
clean_environment(hsproject, hscollection)
58+
#with my_vcr.use_cassette(cassette_name):
59+
remove_all_jobs(hsproject)
60+
yield
61+
62+
63+
#@my_vcr.use_cassette()
64+
@pytest.fixture(scope='session')
65+
def hscollection(hsproject):
66+
collection = hsproject.collections.new_store(TEST_COLLECTION_NAME)
67+
for item in collection.iter_values():
68+
collection.delete(item['_key'])
69+
yield collection
70+
# drop all items in test collection
71+
for item in collection.iter_values():
72+
collection.delete(item['_key'])
6473

6574
# ----------------------------------------------------------------------------
6675

@@ -76,17 +85,7 @@ def start_job(hsproject, **startparams):
7685
# Clean environment section
7786

7887

79-
def clean_environment(hsproject, hscollection):
80-
_remove_all_jobs(hsproject)
81-
# drop all items in test collection
82-
for item in hscollection.iter_values():
83-
hscollection.delete(item['_key'])
84-
# delete frontier slot
85-
frontier = hsproject.frontier
86-
frontier.delete_slot(TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT)
87-
88-
89-
def _remove_all_jobs(hsproject):
88+
def remove_all_jobs(hsproject):
9089
for k in list(hsproject.settings.keys()):
9190
if k != 'botgroups':
9291
del hsproject.settings[k]
@@ -113,15 +112,15 @@ def _remove_job(hsproject, jobkey):
113112
# Botgroups helpers section
114113

115114

116-
def _set_testbotgroup(hsproject):
115+
def set_testbotgroup(hsproject):
117116
hsproject.settings.apipost(jl={'botgroups': [TEST_BOTGROUP]})
118117
# Additional step to populate JobQ's botgroups table
119118
url = urlpathjoin(TEST_ENDPOINT, 'botgroups', TEST_BOTGROUP, 'max_running')
120119
requests.post(url, auth=hsproject.auth, data='null')
121120
hsproject.settings.expire()
122121

123122

124-
def _unset_testbotgroup(hsproject):
123+
def unset_testbotgroup(hsproject):
125124
hsproject.settings.apidelete('botgroups')
126125
hsproject.settings.expire()
127126
# Additional step to delete botgroups in JobQ

tests/hubstorage/test_frontier.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
"""
22
Test Frontier
33
"""
4+
import pytest
45
from .conftest import TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT
56

67

8+
@pytest.fixture(autouse=True)
9+
def delete_frontier_slot(hsproject):
10+
frontier = hsproject.frontier
11+
frontier.delete_slot(TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT)
12+
13+
714
def _get_urls(batch):
815
return [r[0] for r in batch['requests']]
916

tests/hubstorage/test_project.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"""
22
Test Project
33
"""
4-
import json
5-
from random import randint, random
6-
74
import six
5+
import json
86
import pytest
97
from six.moves import range
108
from requests.exceptions import HTTPError
@@ -14,6 +12,7 @@
1412
from .conftest import TEST_PROJECT_ID, TEST_SPIDER_NAME
1513
from .conftest import hsspiderid
1614
from .conftest import start_job
15+
from .conftest import set_testbotgroup
1716
from .testutil import failing_downloader
1817

1918

@@ -157,7 +156,13 @@ def test_broad(hsproject, hsspiderid):
157156
job.purged()
158157

159158

160-
def test_settings(hsproject):
159+
@pytest.fixture
160+
def restore_botgroup(hsproject):
161+
yield
162+
set_testbotgroup(hsproject)
163+
164+
165+
def test_settings(hsproject, restore_botgroup):
161166
settings = dict(hsproject.settings)
162167
settings.pop('botgroups', None) # ignore testsuite botgroups
163168
assert settings == {}

0 commit comments

Comments
 (0)