11import os
22import vcr
3+ import zlib
4+ import base64
35import pytest
6+ import pickle
47import requests
58from scrapinghub import HubstorageClient
69from scrapinghub .hubstorage .utils import urlpathjoin
1518TEST_AUTH = os .getenv ('HS_AUTH' , 'f' * 32 )
1619TEST_ENDPOINT = os .getenv ('HS_ENDPOINT' , 'http://storage.vm.scrapinghub.com' )
1720
21+ # vcrpy creates the cassetes automatically under VCR_CASSETES_DIR
1822VCR_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+
2041my_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' )
4265def setup_session (hsclient , hsproject , hscollection ):
4366 set_testbotgroup (hsproject )
@@ -51,16 +74,15 @@ def setup_session(hsclient, hsproject, hscollection):
5174@pytest .fixture (autouse = True )
5275def 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' )
6587def hscollection (hsproject ):
6688 collection = get_test_collection (hsproject )
0 commit comments