Skip to content

Commit 9e29f62

Browse files
committed
Clean changes, improve option to update cassettes
1 parent 5f3bebe commit 9e29f62

File tree

7 files changed

+52
-58
lines changed

7 files changed

+52
-58
lines changed

requirements-test.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-r requirements-pypy.txt
22

33
mock
4+
vcrpy==1.10.3
45
pytest
56
pytest-cov
6-
responses==0.5.0
7+
responses==0.5.0

tests/hubstorage/conftest.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import os
2-
import vcr
32
import zlib
43
import base64
5-
import pytest
64
import pickle
5+
6+
import vcr
7+
import mock
8+
import pytest
9+
import shutil
710
import requests
11+
812
from scrapinghub import HubstorageClient
913
from scrapinghub.hubstorage.utils import urlpathjoin
1014

@@ -29,7 +33,7 @@ def serialize(self, cassette_dict):
2933
# receives a dict, must return a string
3034
# there can be binary data inside some of the requests,
3135
# so it's impossible to use json for serialization to string
32-
compressed = zlib.compress(pickle.dumps(cassette_dict))
36+
compressed = zlib.compress(pickle.dumps(cassette_dict, protocol=2))
3337
return base64.b64encode(compressed).decode('utf8')
3438

3539
def deserialize(self, cassette_string):
@@ -46,12 +50,25 @@ def deserialize(self, cassette_string):
4650
def pytest_addoption(parser):
4751
parser.addoption(
4852
"--update-cassettes", action="store_true", default=False,
49-
help="make tests with real services instead of vcr cassettes")
50-
51-
52-
def pytest_generate_tests(metafunc):
53-
if metafunc.config.option.update_cassettes:
53+
help="test with real services rewriting existing vcr cassettes")
54+
parser.addoption(
55+
"--ignore-cassettes", action="store_true", default=False,
56+
help="test with real services skipping existing vcr cassettes")
57+
58+
59+
def pytest_configure(config):
60+
if config.option.update_cassettes:
61+
# there's vcr `all` mode to update cassettes but it doesn't delete
62+
# or clear existing records, so its size will always only grow
63+
if os.path.exists(VCR_CASSETES_DIR):
64+
shutil.rmtree(VCR_CASSETES_DIR)
65+
elif config.option.ignore_cassettes:
66+
# simple hack to just ignore vcr cassettes:
67+
# - all record_mode means recording new interactions + no replay
68+
# - before_record returning None means skipping all the requests
69+
global my_vcr
5470
my_vcr.record_mode = 'all'
71+
my_vcr.before_record_request = lambda request: None
5572

5673

5774
@pytest.fixture(scope='session')
@@ -64,12 +81,20 @@ def hsproject(hsclient):
6481
return hsclient.get_project(TEST_PROJECT_ID)
6582

6683

67-
#@my_vcr.use_cassette()
84+
@my_vcr.use_cassette()
6885
@pytest.fixture(scope='session')
6986
def hsspiderid(hsproject):
7087
return str(hsproject.ids.spider(TEST_SPIDER_NAME, create=1))
7188

7289

90+
@my_vcr.use_cassette()
91+
@pytest.fixture(scope='session')
92+
def hscollection(hsproject):
93+
collection = get_test_collection(hsproject)
94+
clean_collection(collection)
95+
yield collection
96+
clean_collection(collection)
97+
7398

7499
@my_vcr.use_cassette()
75100
@pytest.fixture(autouse=True, scope='session')
@@ -83,25 +108,23 @@ def setup_session(hsclient, hsproject, hscollection):
83108

84109

85110
@pytest.fixture(autouse=True)
86-
def setup_vcrpy_per_test(request, hsproject):
111+
def setup_vcrpy(request, hsproject):
87112
# generates names like "test_module/test_function.yaml"
113+
# otherwise it uses current function name (setup_vcrpy) for all tests
114+
# other option is to add vcr decorator to each test separately
88115
cassette_name = '{}/{}.gz'.format(
89116
request.function.__module__.split('.')[-1],
90117
request.function.__name__
91118
)
119+
# we should clean jobs only when working with real services
120+
# doesn't make sense to clean jobs when working with cassettes
121+
if (request.config.option.update_cassettes or
122+
request.config.option.ignore_cassettes):
123+
remove_all_jobs(hsproject)
92124
with my_vcr.use_cassette(cassette_name):
93125
yield
94126

95127

96-
@my_vcr.use_cassette()
97-
@pytest.fixture(scope='session')
98-
def hscollection(hsproject):
99-
collection = get_test_collection(hsproject)
100-
clean_collection(collection)
101-
yield collection
102-
clean_collection(collection)
103-
104-
105128
# ----------------------------------------------------------------------------
106129

107130

tests/hubstorage/test_collections.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,19 @@ def post_scan_test(hsproject, hscollection):
8686
hscollection.get(last_key)
8787

8888

89-
def test_errors(hscollection):
89+
def test_errors_bad_key(hscollection):
9090
with pytest.raises(KeyError):
9191
hscollection.get('does_not_exist')
92-
args = [
92+
93+
94+
@pytest.mark.parametrize('testarg', [
9395
{'foo': 42},
9496
{'_key': []},
9597
{'_key': 'large_test', 'value': 'x' * 1024 ** 2},
96-
]
97-
for arg in args:
98-
with pytest.raises(ValueError):
99-
hscollection.set(arg)
98+
])
99+
def test_errors(hscollection, testarg):
100+
with pytest.raises(ValueError):
101+
hscollection.set(testarg)
100102

101103

102104
def test_data_download(hsproject, hscollection):

tests/hubstorage/test_jobq.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
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)
2214

2315

2416
def _keys(lst):

tests/hubstorage/test_jobsmeta.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,8 @@
33
44
System tests for operations on stored job metadata
55
"""
6-
import pytest
76
from .conftest import TEST_SPIDER_NAME
87
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)
178

189

1910
def _assertMetadata(meta1, meta2):

tests/hubstorage/test_project.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,9 @@
1313
from .conftest import hsspiderid
1414
from .conftest import start_job
1515
from .conftest import set_testbotgroup
16-
from .conftest import remove_all_jobs
1716
from .testutil import failing_downloader
1817

1918

20-
@pytest.fixture(autouse=True)
21-
def clean_jobs(hsproject):
22-
remove_all_jobs(hsproject)
23-
yield
24-
remove_all_jobs(hsproject)
25-
26-
2719
def test_projectid(hsclient):
2820
p1 = hsclient.get_project(int(TEST_PROJECT_ID))
2921
p2 = hsclient.get_project(str(TEST_PROJECT_ID))

tests/hubstorage/test_system.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,10 @@
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
1413

1514

1615
MAGICN = 1211
1716

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

2518
@pytest.fixture
2619
def panelclient():

0 commit comments

Comments
 (0)