Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable testing of cirq_google notebooks. #5766

Merged
merged 14 commits into from
Sep 3, 2022
Prev Previous commit
Next Next commit
Use mock.patch.dict to mock the google.colab module
  • Loading branch information
pavoljuhas committed Jul 16, 2022
commit 36be3f491b7685e38d7b80adc29300ac4fb42eea
17 changes: 4 additions & 13 deletions cirq-google/cirq_google/engine/qcs_notebook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import unittest.mock as mock
import pytest

Expand Down Expand Up @@ -97,13 +98,9 @@ def test_get_qcs_objects_for_notebook_no_processors(engine_mock):
_ = get_qcs_objects_for_notebook()


@mock.patch.dict('sys.modules', {'google.colab': mock.Mock()})
@mock.patch('cirq_google.engine.qcs_notebook.get_engine')
def test_get_qcs_objects_for_notebook_auth_succeeds(engine_mock):
# force google.colab to import
auth_mock = mock.Mock()
import sys

sys.modules['google.colab'] = auth_mock
fake_processor = cg.engine.SimulatedLocalProcessor(
processor_id='tester', project_name='mock_project', device=cg.Sycamore
)
Expand All @@ -116,16 +113,12 @@ def test_get_qcs_objects_for_notebook_auth_succeeds(engine_mock):
assert result.project_id == 'mock_project'
assert len(result.device.metadata.qubit_set) == 54

del sys.modules['google.colab']


@mock.patch.dict('sys.modules', {'google.colab': mock.Mock()})
@mock.patch('cirq_google.engine.qcs_notebook.get_engine')
def test_get_qcs_objects_for_notebook_auth_fails(engine_mock):
# force google.colab to import
auth_mock = mock.Mock()
import sys
auth_mock = sys.modules['google.colab']

sys.modules['google.colab'] = auth_mock
auth_mock.auth.authenticate_user = mock.Mock(side_effect=Exception('mock auth failure'))
fake_processor = cg.engine.SimulatedLocalProcessor(
processor_id='tester', project_name='mock_project', device=cg.Sycamore
Expand All @@ -139,5 +132,3 @@ def test_get_qcs_objects_for_notebook_auth_fails(engine_mock):
assert not result.signed_in
assert result.is_simulator
assert result.project_id == 'fake_project'

del sys.modules['google.colab']