Skip to content

Commit 332454f

Browse files
committed
Cassette for session setup, clean test bg logic
1 parent 0602bba commit 332454f

File tree

7 files changed

+106
-65
lines changed

7 files changed

+106
-65
lines changed

tests/hubstorage/conftest.py

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
TEST_SPIDER_NAME = 'hs-test-spider'
1111
TEST_FRONTIER_NAME = 'test'
1212
TEST_FRONTIER_SLOT = 'site.com'
13-
TEST_BOTGROUPS = ['python-hubstorage-test', 'g1']
13+
TEST_BOTGROUP = 'python-hubstorage-test'
1414
TEST_COLLECTION_NAME = "test_collection_123"
1515
TEST_AUTH = os.getenv('HS_AUTH', 'f' * 32)
1616
TEST_ENDPOINT = os.getenv('HS_ENDPOINT', 'http://storage.vm.scrapinghub.com')
1717

1818
VCR_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')
2224
def 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')
3235
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
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)

tests/hubstorage/test_collections.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ def _mkitem():
1717
field3=3, field4={'v4k': 'v4v'})
1818

1919

20-
def test_simple_count(hsproject):
20+
def test_simple_count(hsproject, hscollection):
2121
test_item = dict(_mkitem())
2222
test_item['_key'] = 'a'
2323

24-
collection = hsproject.collections.new_store(TEST_COLLECTION_NAME)
25-
collection.set(test_item)
26-
assert collection.count() == 1
24+
hscollection.set(test_item)
25+
assert hscollection.count() == 1
2726

2827

2928
def post_get_delete_test(hsproject):
@@ -51,42 +50,40 @@ def post_get_delete_test(hsproject):
5150
col.get(test_key)
5251

5352

54-
def post_scan_test(hsproject):
55-
col = hsproject.collections.new_store(TEST_COLLECTION_NAME)
56-
53+
def post_scan_test(hsproject, hscollection):
5754
# populate with 20 items
5855
test_item = _mkitem()
5956
last_key = None
60-
with closing(col.create_writer()) as writer:
57+
with closing(hscollection.create_writer()) as writer:
6158
for i in range(20):
6259
test_item['_key'] = last_key = "post_scan_test%d" % i
6360
test_item['counter'] = i
6461
writer.write(test_item)
6562

6663
# check last value is as expected
67-
returned_item = col.get(last_key)
64+
returned_item = hscollection.get(last_key)
6865
del test_item['_key']
6966
assert test_item == returned_item
7067

7168
# get all values starting with 1
72-
result = list(col.get(prefix='post_scan_test1'))
69+
result = list(hscollection.get(prefix='post_scan_test1'))
7370
# 1 & 10-19 = 11 items
7471
assert len(result) == 11
7572

7673
# combining with normal filters
77-
result = list(col.get(filter='["counter", ">", [5]]',
74+
result = list(hscollection.get(filter='["counter", ">", [5]]',
7875
prefix='post_scan_test1'))
7976
# 10-19
8077
assert len(result) == 10
8178

8279
# bulk delete
83-
col.delete('post_scan_test%d' % i for i in range(20))
80+
hscollection.delete('post_scan_test%d' % i for i in range(20))
8481

8582
# test items removed (check first and last)
8683
with pytest.raises(KeyError):
87-
col.get('post_scan_test0')
84+
hscollection.get('post_scan_test0')
8885
with pytest.raises(KeyError):
89-
col.get(last_key)
86+
hscollection.get(last_key)
9087

9188

9289
def test_errors(hscollection):
@@ -102,10 +99,9 @@ def test_errors(hscollection):
10299
hscollection.set(arg)
103100

104101

105-
def test_data_download(hsproject):
106-
col = hsproject.collections.new_store(TEST_COLLECTION_NAME)
102+
def test_data_download(hsproject, hscollection):
107103
items = []
108-
with closing(col.create_writer()) as writer:
104+
with closing(hscollection.create_writer()) as writer:
109105
for i in range(20):
110106
test_item = _mkitem()
111107
test_item['_key'] = "test_data_download%d" % i
@@ -114,12 +110,12 @@ def test_data_download(hsproject):
114110
items.append(test_item)
115111

116112
# check parameters are passed correctly
117-
downloaded = list(col.iter_values(prefix='test_data_download1'))
113+
downloaded = list(hscollection.iter_values(prefix='test_data_download1'))
118114
assert len(downloaded) == 11
119115

120116
# simulate network timeouts and download data
121117
with failing_downloader(hsproject.collections):
122-
downloaded = list(col.iter_values(start='test_data_download1'))
118+
downloaded = list(hscollection.iter_values(start='test_data_download1'))
123119
assert len(downloaded) == 19
124120

125121

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_jobq.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111

1212
from .conftest import TEST_PROJECT_ID, TEST_SPIDER_NAME
1313
from .conftest import hsspiderid
14+
from .conftest import remove_all_jobs
15+
16+
17+
@pytest.fixture(autouse=True)
18+
def clean_jobs(hsproject):
19+
remove_all_jobs(hsproject)
20+
yield
21+
remove_all_jobs(hsproject)
1422

1523

1624
def _keys(lst):

tests/hubstorage/test_jobsmeta.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
44
System tests for operations on stored job metadata
55
"""
6+
import pytest
67
from .conftest import TEST_SPIDER_NAME
78
from .conftest import start_job
9+
from .conftest import remove_all_jobs
10+
11+
12+
@pytest.fixture(autouse=True)
13+
def clean_jobs(hsproject):
14+
remove_all_jobs(hsproject)
15+
yield
16+
remove_all_jobs(hsproject)
817

918

1019
def _assertMetadata(meta1, meta2):

tests/hubstorage/test_project.py

Lines changed: 17 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,9 +12,18 @@
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
16+
from .conftest import remove_all_jobs
1717
from .testutil import failing_downloader
1818

1919

20+
@pytest.fixture(autouse=True)
21+
def clean_jobs(hsproject):
22+
remove_all_jobs(hsproject)
23+
yield
24+
remove_all_jobs(hsproject)
25+
26+
2027
def test_projectid(hsclient):
2128
p1 = hsclient.get_project(int(TEST_PROJECT_ID))
2229
p2 = hsclient.get_project(str(TEST_PROJECT_ID))
@@ -157,7 +164,13 @@ def test_broad(hsproject, hsspiderid):
157164
job.purged()
158165

159166

160-
def test_settings(hsproject):
167+
@pytest.fixture
168+
def restore_botgroup(hsproject):
169+
yield
170+
set_testbotgroup(hsproject)
171+
172+
173+
def test_settings(hsproject, restore_botgroup):
161174
settings = dict(hsproject.settings)
162175
settings.pop('botgroups', None) # ignore testsuite botgroups
163176
assert settings == {}

tests/hubstorage/test_system.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010
from .conftest import TEST_ENDPOINT, TEST_SPIDER_NAME
1111
from .conftest import TEST_PROJECT_ID, TEST_AUTH
1212
from .conftest import start_job
13+
from .conftest import remove_all_jobs
1314

1415

1516
MAGICN = 1211
1617

18+
@pytest.fixture(autouse=True)
19+
def clean_jobs(hsproject):
20+
remove_all_jobs(hsproject)
21+
yield
22+
remove_all_jobs(hsproject)
23+
1724

1825
@pytest.fixture
1926
def panelclient():

0 commit comments

Comments
 (0)