Skip to content

Commit

Permalink
fix: _parse_instance_uri() tests
Browse files Browse the repository at this point in the history
Added more tests for _parse_instance_uri(). Made the function private.
  • Loading branch information
RahulDubey391 committed Dec 20, 2023
1 parent bf0dffe commit 9201514
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
10 changes: 3 additions & 7 deletions google/cloud/alloydb/connector/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@
)


def parse_instance_uri(instance_uri: str) -> Tuple[str, str, str, str]:
def _parse_instance_uri(instance_uri: str) -> Tuple[str, str, str, str]:
# should take form "projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>"
if INSTANCE_URI_REGEX.fullmatch(instance_uri) is None:
raise ValueError(
"Arg `instance_uri` must have "
"format: projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>, projects/<DOMAIN>:<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>"
f"got {instance_uri}."
)

instance_uri_split = INSTANCE_URI_REGEX.split(instance_uri)

return (
instance_uri_split[1],
instance_uri_split[3],
Expand Down Expand Up @@ -79,17 +77,15 @@ def __init__(
client: AlloyDBClient,
keys: asyncio.Future[Tuple[rsa.RSAPrivateKey, str]],
) -> None:

self._instance_uri = instance_uri

# validate and parse instance_uri
(
self._project,
self._region,
self._cluster,
self._name,
) = parse_instance_uri(instance_uri)
) = _parse_instance_uri(instance_uri)

self._instance_uri = instance_uri
self._client = client
self._keys = keys
self._refresh_rate_limiter = AsyncRateLimiter(
Expand Down
48 changes: 31 additions & 17 deletions tests/unit/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,50 @@

import asyncio
from datetime import datetime, timedelta
from typing import Tuple

import aiohttp
from mocks import FakeAlloyDBClient
import pytest

from google.cloud.alloydb.connector.exceptions import RefreshError
from google.cloud.alloydb.connector.instance import Instance, parse_instance_uri
from google.cloud.alloydb.connector.instance import _parse_instance_uri, Instance
from google.cloud.alloydb.connector.refresh import _is_valid, RefreshResult
from google.cloud.alloydb.connector.utils import generate_keys


@pytest.mark.asycio
async def test_parser_instance_uri() -> None:
@pytest.mark.parametrize(
"instance_uri, expected",
[
(
"projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance",
("test-project", "test-region", "test-cluster", "test-instance"),
),
(
"projects/test-domain:test-project/locations/test-region/clusters/test-cluster/instances/test-instance",
(
"test-domain:test-project",
"test-region",
"test-cluster",
"test-instance",
),
),
],
)
def test_parse_instance_uri(instance_uri: str, expected: Tuple[str, str, str]) -> None:
"""
Test to check whether the __init__ method of Instance
can tell if the instance URI that's passed in is formatted correctly.
Test that _parse_instance_uri correctly on
normal instance uri and domain-scoped projects.
"""
instance_uri = [
"projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance",
"projects/google.com:test-project/locations/test-region/clusters/test-cluster/instances/test-instance",
]
assert expected == _parse_instance_uri(instance_uri)

for uri in instance_uri:
project, region, cluster, name = parse_instance_uri(uri)
assert (
project in ["test-project", "google.com:test-project"]
and region == "test-region"
and cluster == "test-cluster"
and name == "test-instance"
)

def test_parse_bad_instance_uri() -> None:
"""
Tests that ValueError is thrown for bad instance uri.
"""
with pytest.raises(ValueError):
_parse_instance_uri("test-project:test-instance")


@pytest.mark.asyncio
Expand Down

0 comments on commit 9201514

Please sign in to comment.