Skip to content

Commit 2a7910f

Browse files
committed
Enable cassettes with gzip serialization
1 parent 448e4d9 commit 2a7910f

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

tests/hubstorage/conftest.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
22
import vcr
3+
import zlib
4+
import base64
35
import pytest
6+
import pickle
47
import requests
58
from scrapinghub import HubstorageClient
69
from scrapinghub.hubstorage.utils import urlpathjoin
@@ -15,9 +18,29 @@
1518
TEST_AUTH = os.getenv('HS_AUTH', 'f' * 32)
1619
TEST_ENDPOINT = os.getenv('HS_ENDPOINT', 'http://storage.vm.scrapinghub.com')
1720

21+
# vcrpy creates the cassetes automatically under VCR_CASSETES_DIR
1822
VCR_CASSETES_DIR = 'tests/hubstorage/cassetes'
1923

24+
25+
class VCRGzipSerializer(object):
26+
"""Custom ZIP serializer for VCR.py."""
27+
28+
def serialize(self, cassette_dict):
29+
# receives a dict, must return a string
30+
# there can be binary data inside some of the requests,
31+
# so it's impossible to use json for serialization to string
32+
compressed = zlib.compress(pickle.dumps(cassette_dict))
33+
return base64.b64encode(compressed).decode('utf8')
34+
35+
def deserialize(self, cassette_string):
36+
# receives a string, must return a dict
37+
decoded = base64.b64decode(cassette_string.encode('utf8'))
38+
return pickle.loads(zlib.decompress(decoded))
39+
40+
2041
my_vcr = vcr.VCR(cassette_library_dir=VCR_CASSETES_DIR, record_mode='once')
42+
my_vcr.register_serializer('gz', VCRGzipSerializer())
43+
my_vcr.serializer = 'gz'
2144

2245

2346
@pytest.fixture(scope='session')
@@ -37,7 +60,7 @@ def hsspiderid(hsproject):
3760

3861

3962

40-
#@my_vcr.use_cassette()
63+
@my_vcr.use_cassette()
4164
@pytest.fixture(autouse=True, scope='session')
4265
def setup_session(hsclient, hsproject, hscollection):
4366
set_testbotgroup(hsproject)
@@ -51,16 +74,15 @@ def setup_session(hsclient, hsproject, hscollection):
5174
@pytest.fixture(autouse=True)
5275
def setup_vcrpy_per_test(request, hsproject):
5376
# generates names like "test_module/test_function.yaml"
54-
# vcrpy creates the cassetes automatically under VCR_CASSETES_DIR
55-
cassette_name = '{}/{}.yaml'.format(
77+
cassette_name = '{}/{}.gz'.format(
5678
request.function.__module__.split('.')[-1],
5779
request.function.__name__
5880
)
59-
#with my_vcr.use_cassette(cassette_name):
60-
yield
81+
with my_vcr.use_cassette(cassette_name):
82+
yield
6183

6284

63-
#@my_vcr.use_cassette()
85+
@my_vcr.use_cassette()
6486
@pytest.fixture(scope='session')
6587
def hscollection(hsproject):
6688
collection = get_test_collection(hsproject)

tests/hubstorage/test_frontier.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test Frontier
33
"""
44
import pytest
5+
56
from .conftest import TEST_FRONTIER_NAME, TEST_FRONTIER_SLOT
67

78

tests/hubstorage/test_project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_samples(hsproject):
244244
count = int(j2.samples.batch_size * 3)
245245
for i in range(count):
246246
ts += i
247-
row = [ts] + list(val*i for val in range(100))
247+
row = [ts] + list(val*i for val in range(10))
248248
samples.append(row)
249249
j2.samples.write(row)
250250
j2.samples.flush()

0 commit comments

Comments
 (0)