Skip to content

Commit

Permalink
add url and headers to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
splch committed Sep 20, 2024
1 parent d5ed477 commit d11e6ff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
14 changes: 11 additions & 3 deletions qiskit_ionq/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ def get_register_sizes_and_labels(
return sizes, labels


def compress_to_metadata_string(metadata: dict | list) -> str: # pylint: disable=invalid-name
def compress_to_metadata_string(
metadata: dict | list,
) -> str: # pylint: disable=invalid-name
"""
Convert a metadata object to a compact string format (dumped, gzipped, base64 encoded)
for storing in IonQ API metadata
Expand All @@ -330,7 +332,9 @@ def compress_to_metadata_string(metadata: dict | list) -> str: # pylint: disabl
return encoded.decode()


def decompress_metadata_string(input_string: str) -> dict | list: # pylint: disable=invalid-name
def decompress_metadata_string(
input_string: str,
) -> dict | list: # pylint: disable=invalid-name
"""
Convert compact string format (dumped, gzipped, base64 encoded) from
IonQ API metadata back into a dict or list of dicts relevant to building
Expand Down Expand Up @@ -541,7 +545,11 @@ def get_n_qubits(backend: str, _fallback=100) -> int:
token = creds.get("token")
# could use provider.get_calibration_data().get("qubits", 36)
try:
target = backend[8:] if backend.startswith("ionq_qpu.") else backend
target = (
backend.split("ionq_qpu.")[1]
if backend.startswith("ionq_qpu.")
else backend
)
return requests.get(
url=f"{url}/characterizations/backends/{target}/current",
headers={"Authorization": f"apiKey {token}"},
Expand Down
61 changes: 34 additions & 27 deletions test/helpers/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,6 @@
from qiskit_ionq.helpers import get_n_qubits


def test_user_agent_header():
"""
Tests whether the generated user_agent contains all the required information with the right
version format.
"""
ionq_client = IonQClient()
generated_user_agent = ionq_client.api_headers["User-Agent"]

user_agent_info_keywords = ["qiskit-ionq", "qiskit-terra", "os", "python"]
# Checks if all keywords are present in user-agent string.
all_user_agent_keywords_avail = all(
keyword in generated_user_agent for keyword in user_agent_info_keywords
)

# Checks whether there is at-least 3 version strings from qiskit-ionq, qiskit-terra, python.
has_all_version_strings = len(re.findall(r"\s*([\d.]+)", generated_user_agent)) >= 3
assert all_user_agent_keywords_avail and has_all_version_strings


def test_get_n_qubits_success():
"""Test get_n_qubits returns correct number of qubits and checks correct URL."""
with patch("requests.get") as mock_get:
Expand All @@ -65,10 +46,23 @@ def test_get_n_qubits_success():
expected_url = (
"https://api.ionq.co/v0.3/characterizations/backends/aria-1/current"
)
mock_get.assert_called_with(
url=expected_url,
headers={"Authorization": f"apiKey {MagicMock().token}"},
timeout=5,

# Create a regular expression to match the Authorization header with an apiKey
expected_headers = {"Authorization": re.compile(r"apiKey\s+\S+")}

# Check the arguments of the last call to `requests.get`
mock_get.assert_called()
args, kwargs = mock_get.call_args
assert (
kwargs["url"] == expected_url
), f"Expected URL {expected_url}, but got {kwargs['url']}"

# Assert that the headers contain the apiKey in the expected format
assert re.match(
expected_headers["Authorization"], kwargs["headers"]["Authorization"]
), (
f"Expected headers to match {expected_headers['Authorization'].pattern}, "
f"but got {kwargs['headers']['Authorization']}"
)

assert result == 11, f"Expected 11 qubits, but got {result}"
Expand All @@ -83,10 +77,23 @@ def test_get_n_qubits_fallback():
expected_url = (
"https://api.ionq.co/v0.3/characterizations/backends/aria-1/current"
)
mock_get.assert_called_with(
url=expected_url,
headers={"Authorization": f"apiKey {MagicMock().token}"},
timeout=5,

# Create a regular expression to match the Authorization header with an apiKey
expected_headers = {"Authorization": re.compile(r"apiKey\s+\S+")}

# Check the arguments of the last call to `requests.get`
mock_get.assert_called()
args, kwargs = mock_get.call_args
assert (
kwargs["url"] == expected_url
), f"Expected URL {expected_url}, but got {kwargs['url']}"

# Assert that the headers contain the apiKey in the expected format
assert re.match(
expected_headers["Authorization"], kwargs["headers"]["Authorization"]
), (
f"Expected headers to match {expected_headers['Authorization'].pattern}, "
f"but got {kwargs['headers']['Authorization']}"
)

assert result == 100, f"Expected fallback of 100 qubits, but got {result}"

0 comments on commit d11e6ff

Please sign in to comment.