-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtest_ibm_serverless_client.py
More file actions
80 lines (65 loc) · 2.83 KB
/
test_ibm_serverless_client.py
File metadata and controls
80 lines (65 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# This code is a Qiskit project.
#
# (C) Copyright IBM 2025.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Test IBMServerlessClient."""
import uuid
import unittest
import tempfile
from unittest.mock import patch
from qiskit_serverless import IBMServerlessClient
from qiskit_serverless.core.enums import Channel
class TestIBMServerlessClient(unittest.TestCase):
"""Unit tests for IBMServerlessClient."""
@patch(
"qiskit_serverless.core.clients.serverless_client.ServerlessClient._verify_credentials"
)
@patch("qiskit_ibm_runtime.accounts.management._DEFAULT_ACCOUNT_CONFIG_JSON_FILE")
def test_save_load_account(self, mock_file_path, mock_verify_credentials):
"""Test saving and loading accounts with the IBMServerlessClient."""
# Mock ServerlessClient credential verification
mock_verify_credentials.return_value = None
# Save config in a temporary file
with tempfile.NamedTemporaryFile() as temp_file:
mock_file_path.return_value = temp_file.name
channels_to_test = [
Channel.IBM_CLOUD.value,
Channel.IBM_QUANTUM_PLATFORM.value,
]
instances_to_test = [
"dummy_hub/dummy_group/dummy_project",
"dummy_crn",
"dummy_crn",
]
for use_channel, use_instance in zip(channels_to_test, instances_to_test):
use_token = "save_token"
use_name = f"test_save_account_{uuid.uuid4().hex}"
with self.subTest(use_channel=use_channel):
IBMServerlessClient.save_account(
token=use_token,
name=use_name,
instance=use_instance,
channel=use_channel,
)
client = IBMServerlessClient(name=use_name)
self.assertEqual(client.account.channel, use_channel)
self.assertEqual(client.account.token, use_token)
self.assertEqual(client.account.instance, use_instance)
def test_ibm_quantum_channel(self):
"""Test error raised when initializing account with `ibm_quantum` channel."""
use_channel = "ibm_quantum"
use_instance = "h/g/p"
use_token = "save_token"
with self.assertRaisesRegex(ValueError, r"Your channel value is not correct"):
IBMServerlessClient(
channel=use_channel, instance=use_instance, token=use_token
)
if __name__ == "__main__":
unittest.main()