Skip to content

Commit 7dc57d1

Browse files
vshlapakovBurnzZ
authored andcommitted
Normalize Job instances in tests
1 parent ee40071 commit 7dc57d1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

tests/client/test_items.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22
from six.moves import range
33

4+
from .utils import normalize_job_for_tests
5+
46

57
def _add_test_items(job, size=3):
68
for i in range(size):
@@ -28,6 +30,7 @@ def test_items_iter(spider, json_and_msgpack):
2830

2931
def test_items_list(spider, json_and_msgpack):
3032
job = spider.jobs.run(meta={'state': 'running'})
33+
job = normalize_job_for_tests(job)
3134
_add_test_items(job)
3235

3336
o = job.items.list()
@@ -40,6 +43,7 @@ def test_items_list(spider, json_and_msgpack):
4043

4144
def test_items_list_iter(spider, json_and_msgpack):
4245
job = spider.jobs.run(meta={'state': 'running'})
46+
job = normalize_job_for_tests(job)
4347
_add_test_items(job)
4448
job.finish()
4549

tests/client/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,30 @@ def validate_default_meta(meta, state='pending', units=1,
1313
assert meta.get('units') == units
1414
assert meta.get('api_url') == TEST_DASH_ENDPOINT
1515
assert meta.get('portia_url')
16+
17+
18+
def normalize_job_for_tests(job):
19+
"""A temporary workaround to deal with VCR.py cassettes(snapshots).
20+
21+
The existing tests highly rely on VCR.py which creates snapshots of real
22+
HTTP requests and responses, and during the test process tries to match
23+
requests with the snapshots. Sometimes it's hard to run an appropriate test
24+
environment locally, so we allow to use our servers to create snapshots
25+
for new tests, by "normalizing" the snapshots via patching hosts/credentials
26+
on-the-fly before saving it (see #112).
27+
28+
The problem here is that we patch only requests data and not responses data,
29+
which is pretty difficult to unify over the whole client. It means that if
30+
some test gets data from API (say, a new job ID) and uses it to form another
31+
requests (get the job data), it will form the HTTP requests differently,
32+
thus it won't match with the snapshots during the test process and the tests
33+
will fail.
34+
35+
As a temporary workaround, the helper gets a Job instance, extracts its key,
36+
replaces the project ID part with TEST_PROJECT_ID, and returns a new Job.
37+
So, the other requests done via the new job instance (updating job items,
38+
accessing job logs, etc) will be done using proper URLs matching with
39+
existing snapshots.
40+
"""
41+
normalized_key = '{}/{}'.format(TEST_PROJECT_ID, job.key.split('/', 1)[1])
42+
return job._client.get_job(normalized_key)

0 commit comments

Comments
 (0)