Skip to content

Commit 9c529f3

Browse files
authored
tests: avoid using real credentials unit tests (#432)
Closes #431.
1 parent 1620c12 commit 9c529f3

File tree

3 files changed

+323
-539
lines changed

3 files changed

+323
-539
lines changed

tests/unit/spanner_dbapi/test_connect.py

Lines changed: 77 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
import google.auth.credentials
2121

2222

23+
INSTANCE = "test-instance"
24+
DATABASE = "test-database"
25+
PROJECT = "test-project"
26+
USER_AGENT = "user-agent"
27+
28+
2329
def _make_credentials():
2430
class _CredentialsWithScopes(
2531
google.auth.credentials.Credentials, google.auth.credentials.Scoped
@@ -29,138 +35,105 @@ class _CredentialsWithScopes(
2935
return mock.Mock(spec=_CredentialsWithScopes)
3036

3137

38+
@mock.patch("google.cloud.spanner_v1.Client")
3239
class Test_connect(unittest.TestCase):
33-
def test_connect(self):
40+
def test_w_implicit(self, mock_client):
3441
from google.cloud.spanner_dbapi import connect
3542
from google.cloud.spanner_dbapi import Connection
3643

37-
PROJECT = "test-project"
38-
USER_AGENT = "user-agent"
39-
CREDENTIALS = _make_credentials()
40-
41-
with mock.patch("google.cloud.spanner_v1.Client") as client_mock:
42-
connection = connect(
43-
"test-instance",
44-
"test-database",
45-
PROJECT,
46-
CREDENTIALS,
47-
user_agent=USER_AGENT,
48-
)
44+
client = mock_client.return_value
45+
instance = client.instance.return_value
46+
database = instance.database.return_value
4947

50-
self.assertIsInstance(connection, Connection)
51-
52-
client_mock.assert_called_once_with(
53-
project=PROJECT, credentials=CREDENTIALS, client_info=mock.ANY
54-
)
55-
56-
def test_instance_not_found(self):
57-
from google.cloud.spanner_dbapi import connect
48+
connection = connect(INSTANCE, DATABASE)
5849

59-
with mock.patch(
60-
"google.cloud.spanner_v1.instance.Instance.exists", return_value=False,
61-
) as exists_mock:
50+
self.assertIsInstance(connection, Connection)
6251

63-
with self.assertRaises(ValueError):
64-
connect("test-instance", "test-database")
52+
self.assertIs(connection.instance, instance)
53+
client.instance.assert_called_once_with(INSTANCE)
6554

66-
exists_mock.assert_called_once_with()
55+
self.assertIs(connection.database, database)
56+
instance.database.assert_called_once_with(DATABASE, pool=None)
57+
# Datbase constructs its own pool
58+
self.assertIsNotNone(connection.database._pool)
6759

68-
def test_database_not_found(self):
60+
def test_w_explicit(self, mock_client):
61+
from google.cloud.spanner_v1.pool import AbstractSessionPool
6962
from google.cloud.spanner_dbapi import connect
63+
from google.cloud.spanner_dbapi import Connection
64+
from google.cloud.spanner_dbapi.version import PY_VERSION
7065

71-
with mock.patch(
72-
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True,
73-
):
74-
with mock.patch(
75-
"google.cloud.spanner_v1.database.Database.exists", return_value=False,
76-
) as exists_mock:
77-
78-
with self.assertRaises(ValueError):
79-
connect("test-instance", "test-database")
80-
81-
exists_mock.assert_called_once_with()
66+
credentials = _make_credentials()
67+
pool = mock.create_autospec(AbstractSessionPool)
68+
client = mock_client.return_value
69+
instance = client.instance.return_value
70+
database = instance.database.return_value
8271

83-
def test_connect_instance_id(self):
84-
from google.cloud.spanner_dbapi import connect
85-
from google.cloud.spanner_dbapi import Connection
72+
connection = connect(
73+
INSTANCE, DATABASE, PROJECT, credentials, pool=pool, user_agent=USER_AGENT,
74+
)
8675

87-
INSTANCE = "test-instance"
76+
self.assertIsInstance(connection, Connection)
8877

89-
with mock.patch(
90-
"google.cloud.spanner_v1.client.Client.instance"
91-
) as instance_mock:
92-
connection = connect(INSTANCE, "test-database")
78+
mock_client.assert_called_once_with(
79+
project=PROJECT, credentials=credentials, client_info=mock.ANY
80+
)
81+
client_info = mock_client.call_args_list[0][1]["client_info"]
82+
self.assertEqual(client_info.user_agent, USER_AGENT)
83+
self.assertEqual(client_info.python_version, PY_VERSION)
9384

94-
instance_mock.assert_called_once_with(INSTANCE)
85+
self.assertIs(connection.instance, instance)
86+
client.instance.assert_called_once_with(INSTANCE)
9587

96-
self.assertIsInstance(connection, Connection)
88+
self.assertIs(connection.database, database)
89+
instance.database.assert_called_once_with(DATABASE, pool=pool)
9790

98-
def test_connect_database_id(self):
91+
def test_w_instance_not_found(self, mock_client):
9992
from google.cloud.spanner_dbapi import connect
100-
from google.cloud.spanner_dbapi import Connection
101-
102-
DATABASE = "test-database"
10393

104-
with mock.patch(
105-
"google.cloud.spanner_v1.instance.Instance.database"
106-
) as database_mock:
107-
with mock.patch(
108-
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True,
109-
):
110-
connection = connect("test-instance", DATABASE)
94+
client = mock_client.return_value
95+
instance = client.instance.return_value
96+
instance.exists.return_value = False
11197

112-
database_mock.assert_called_once_with(DATABASE, pool=mock.ANY)
98+
with self.assertRaises(ValueError):
99+
connect(INSTANCE, DATABASE)
113100

114-
self.assertIsInstance(connection, Connection)
101+
instance.exists.assert_called_once_with()
115102

116-
def test_default_sessions_pool(self):
103+
def test_w_database_not_found(self, mock_client):
117104
from google.cloud.spanner_dbapi import connect
118105

119-
with mock.patch("google.cloud.spanner_v1.instance.Instance.database"):
120-
with mock.patch(
121-
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True,
122-
):
123-
connection = connect("test-instance", "test-database")
106+
client = mock_client.return_value
107+
instance = client.instance.return_value
108+
database = instance.database.return_value
109+
database.exists.return_value = False
124110

125-
self.assertIsNotNone(connection.database._pool)
111+
with self.assertRaises(ValueError):
112+
connect(INSTANCE, DATABASE)
126113

127-
def test_sessions_pool(self):
114+
database.exists.assert_called_once_with()
115+
116+
def test_w_credential_file_path(self, mock_client):
128117
from google.cloud.spanner_dbapi import connect
129-
from google.cloud.spanner_v1.pool import FixedSizePool
118+
from google.cloud.spanner_dbapi import Connection
119+
from google.cloud.spanner_dbapi.version import PY_VERSION
130120

131-
database_id = "test-database"
132-
pool = FixedSizePool()
121+
credentials_path = "dummy/file/path.json"
133122

134-
with mock.patch(
135-
"google.cloud.spanner_v1.instance.Instance.database"
136-
) as database_mock:
137-
with mock.patch(
138-
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True,
139-
):
140-
connect("test-instance", database_id, pool=pool)
141-
database_mock.assert_called_once_with(database_id, pool=pool)
123+
connection = connect(
124+
INSTANCE,
125+
DATABASE,
126+
PROJECT,
127+
credentials=credentials_path,
128+
user_agent=USER_AGENT,
129+
)
142130

143-
def test_connect_w_credential_file_path(self):
144-
from google.cloud.spanner_dbapi import connect
145-
from google.cloud.spanner_dbapi import Connection
131+
self.assertIsInstance(connection, Connection)
146132

147-
PROJECT = "test-project"
148-
USER_AGENT = "user-agent"
149-
credentials = "dummy/file/path.json"
150-
151-
with mock.patch(
152-
"google.cloud.spanner_v1.Client.from_service_account_json"
153-
) as client_mock:
154-
connection = connect(
155-
"test-instance",
156-
"test-database",
157-
PROJECT,
158-
credentials=credentials,
159-
user_agent=USER_AGENT,
160-
)
161-
162-
self.assertIsInstance(connection, Connection)
163-
164-
client_mock.assert_called_once_with(
165-
credentials, project=PROJECT, client_info=mock.ANY
166-
)
133+
factory = mock_client.from_service_account_json
134+
factory.assert_called_once_with(
135+
credentials_path, project=PROJECT, client_info=mock.ANY,
136+
)
137+
client_info = factory.call_args_list[0][1]["client_info"]
138+
self.assertEqual(client_info.user_agent, USER_AGENT)
139+
self.assertEqual(client_info.python_version, PY_VERSION)

0 commit comments

Comments
 (0)