12
12
TEST_FRONTIER_SLOT = 'site.com'
13
13
TEST_BOTGROUPS = ['python-hubstorage-test' , 'g1' ]
14
14
TEST_COLLECTION_NAME = "test_collection_123"
15
-
16
- VCR_CASSETES_DIR = 'tests/hubstorage/cassetes'
17
-
18
15
TEST_AUTH = os .getenv ('HS_AUTH' , 'f' * 32 )
19
16
TEST_ENDPOINT = os .getenv ('HS_ENDPOINT' , 'http://storage.vm.scrapinghub.com' )
20
17
18
+ VCR_CASSETES_DIR = 'tests/hubstorage/cassetes'
21
19
22
- @pytest .fixture
23
- def vcr_instance (scope = 'session' ):
24
- return vcr .VCR (
25
- cassette_library_dir = VCR_CASSETES_DIR ,
26
- record_mode = 'once' ,
27
- )
28
20
29
21
@pytest .fixture (scope = 'session' )
30
22
def hsclient ():
@@ -38,6 +30,9 @@ def hsproject(hsclient):
38
30
39
31
@pytest .fixture
40
32
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
41
36
return str (hsproject .ids .spider (TEST_SPIDER_NAME , create = 1 ))
42
37
43
38
@@ -47,56 +42,53 @@ def hscollection(hsproject):
47
42
48
43
49
44
@pytest .fixture (autouse = True , scope = 'session' )
50
- def setup_session (hsclient , hsproject ):
45
+ def setup_session (hsclient ):
51
46
yield
52
47
hsclient .close ()
53
48
54
49
50
+ @pytest .fixture
51
+ def vcr_instance (scope = 'session' ):
52
+ return vcr .VCR (cassette_library_dir = VCR_CASSETES_DIR , record_mode = 'once' )
53
+
54
+
55
55
@pytest .fixture (autouse = True )
56
56
def setup_test (hsclient , hsproject , hscollection , request , vcr_instance ):
57
+ # generates names like "test_module/test_function.yaml"
58
+ # vcrpy creates the cassetes automatically under VCR_CASSETES_DIR
57
59
cassette_name = '{}/{}.yaml' .format (
58
60
request .function .__module__ .split ('.' )[- 1 ],
59
61
request .function .__name__
60
62
)
61
63
with vcr_instance .use_cassette (cassette_name ):
62
64
_set_testbotgroup (hsproject )
63
- _remove_all_jobs (hsproject )
64
- _clean_collection (hscollection )
65
- _delete_frontier_slot (hsproject )
65
+ clean_environment (hsproject , hscollection )
66
66
yield
67
- _remove_all_jobs (hsproject )
68
- _clean_collection (hscollection )
69
- _delete_frontier_slot (hsproject )
67
+ clean_environment (hsproject , hscollection )
70
68
_unset_testbotgroup (hsproject )
71
69
72
70
# ----------------------------------------------------------------------------
73
71
74
- def _delete_frontier_slot (hsproject ):
75
- frontier = hsproject .frontier
76
- frontier .delete_slot (TEST_FRONTIER_NAME , TEST_FRONTIER_SLOT )
77
-
78
72
79
- def _clean_collection (hscollection ):
80
- for item in hscollection .iter_values ():
81
- hscollection .delete (item ['_key' ])
73
+ def start_job (hsproject , ** startparams ):
74
+ jobdata = hsproject .jobq .start (** startparams )
75
+ if jobdata :
76
+ jobkey = jobdata .pop ('key' )
77
+ jobauth = (jobkey , jobdata ['auth' ])
78
+ return hsproject .get_job (jobkey , jobauth = jobauth , metadata = jobdata )
82
79
83
80
84
- def _set_testbotgroup (hsproject ):
85
- hsproject .settings .apipost (jl = {'botgroups' : [TEST_BOTGROUPS [0 ]]})
86
- # Additional step to populate JobQ's botgroups table
87
- for botgroup in TEST_BOTGROUPS :
88
- url = urlpathjoin (TEST_ENDPOINT , 'botgroups' , botgroup , 'max_running' )
89
- requests .post (url , auth = hsproject .auth , data = 'null' )
90
- hsproject .settings .expire ()
81
+ # Clean environment section
91
82
92
83
93
- def _unset_testbotgroup (hsproject ):
94
- hsproject .settings .apidelete ('botgroups' )
95
- hsproject .settings .expire ()
96
- # Additional step to delete botgroups in JobQ
97
- for botgroup in TEST_BOTGROUPS :
98
- url = urlpathjoin (TEST_ENDPOINT , 'botgroups' , botgroup )
99
- requests .delete (url , auth = hsproject .auth )
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 )
100
92
101
93
102
94
def _remove_all_jobs (hsproject ):
@@ -118,17 +110,27 @@ def _remove_all_jobs(hsproject):
118
110
def _remove_job (hsproject , jobkey ):
119
111
hsproject .jobq .finish (jobkey )
120
112
hsproject .jobq .delete (jobkey )
121
- _delete_job (hsproject , jobkey )
122
-
123
-
124
- def _delete_job (hsproject , jobkey ):
113
+ # delete job
125
114
assert jobkey .startswith (TEST_PROJECT_ID ), jobkey
126
115
hsproject .jobs .apidelete (jobkey .partition ('/' )[2 ])
127
116
128
117
129
- def start_job (hsproject , ** startparams ):
130
- jobdata = hsproject .jobq .start (** startparams )
131
- if jobdata :
132
- jobkey = jobdata .pop ('key' )
133
- jobauth = (jobkey , jobdata ['auth' ])
134
- return hsproject .get_job (jobkey , jobauth = jobauth , metadata = jobdata )
118
+ # Botgroups helpers section
119
+
120
+
121
+ def _set_testbotgroup (hsproject ):
122
+ hsproject .settings .apipost (jl = {'botgroups' : [TEST_BOTGROUPS [0 ]]})
123
+ # 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
+ hsproject .settings .expire ()
128
+
129
+
130
+ def _unset_testbotgroup (hsproject ):
131
+ hsproject .settings .apidelete ('botgroups' )
132
+ hsproject .settings .expire ()
133
+ # 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 )
0 commit comments