1
1
import os
2
2
import vcr
3
+ import zlib
4
+ import base64
3
5
import pytest
6
+ import pickle
4
7
import requests
5
8
from scrapinghub import HubstorageClient
6
9
from scrapinghub .hubstorage .utils import urlpathjoin
15
18
TEST_AUTH = os .getenv ('HS_AUTH' , 'f' * 32 )
16
19
TEST_ENDPOINT = os .getenv ('HS_ENDPOINT' , 'http://storage.vm.scrapinghub.com' )
17
20
21
+ # vcrpy creates the cassetes automatically under VCR_CASSETES_DIR
18
22
VCR_CASSETES_DIR = 'tests/hubstorage/cassetes'
19
23
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
+
20
41
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'
21
44
22
45
23
46
@pytest .fixture (scope = 'session' )
@@ -37,7 +60,7 @@ def hsspiderid(hsproject):
37
60
38
61
39
62
40
- # @my_vcr.use_cassette()
63
+ @my_vcr .use_cassette ()
41
64
@pytest .fixture (autouse = True , scope = 'session' )
42
65
def setup_session (hsclient , hsproject , hscollection ):
43
66
set_testbotgroup (hsproject )
@@ -51,16 +74,15 @@ def setup_session(hsclient, hsproject, hscollection):
51
74
@pytest .fixture (autouse = True )
52
75
def setup_vcrpy_per_test (request , hsproject ):
53
76
# 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 (
56
78
request .function .__module__ .split ('.' )[- 1 ],
57
79
request .function .__name__
58
80
)
59
- # with my_vcr.use_cassette(cassette_name):
60
- yield
81
+ with my_vcr .use_cassette (cassette_name ):
82
+ yield
61
83
62
84
63
- # @my_vcr.use_cassette()
85
+ @my_vcr .use_cassette ()
64
86
@pytest .fixture (scope = 'session' )
65
87
def hscollection (hsproject ):
66
88
collection = get_test_collection (hsproject )
0 commit comments