azure-quantum-python icon indicating copy to clipboard operation
azure-quantum-python copied to clipboard

Support Unit Test Recording playback for multiple calls

Open guenp opened this issue 4 years ago • 1 comments

Update QuantumTestBase to support repeated calls in unit test playback mode.

See note here:

https://github.com/microsoft/qdk-python/blob/f7e944d6f674e5aa63f63f8f0d3c84ede2a73502/azure-quantum/tests/unit/test_job.py#L245

guenp avatar Aug 03 '21 01:08 guenp

A workaround for this issue is to mock out the Job.wait_until_completed method with a mock wait method that pauses/resumes the recording before and after:

with mock.patch.object(
    Job,
    "wait_until_completed",
    self.mock_wait(Job.wait_until_completed)
):
    service.run(...)

with a method mock_wait on the QuantumTestBase class:

    def mock_wait(self, job_wait_until_completed):
        # Workaround for issue #118
        # See: https://github.com/microsoft/qdk-python/issues/118
        # Create closure to expose self and Job.wait_until_completed to
        # mock function
        def _mock_wait(job, *args, **kwargs):
            # Pause recording such that we don't record multiple calls
            self.pause_recording() 
            job_wait_until_completed(job, *args, **kwargs)
            self.resume_recording()

            # Record a single GET request such that job.wait_until_completed
            # doesn't fail when running recorded tests
            return job_wait_until_completed(job, *args, **kwargs)

        return _mock_wait

guenp avatar Feb 05 '22 01:02 guenp