@@ -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