forked from googleapis/python-spanner-django
/
test_connect.py
107 lines (81 loc) · 3.29 KB
/
test_connect.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright 2020 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
"""connect() module function unit tests."""
import unittest
from unittest import mock
import google.auth.credentials
from google.api_core.gapic_v1.client_info import ClientInfo
from spanner_dbapi import connect, Connection
def _make_credentials():
class _CredentialsWithScopes(
google.auth.credentials.Credentials, google.auth.credentials.Scoped
):
pass
return mock.Mock(spec=_CredentialsWithScopes)
class Test_connect(unittest.TestCase):
def test_connect(self):
PROJECT = "test-project"
USER_AGENT = "user-agent"
CREDENTIALS = _make_credentials()
CLIENT_INFO = ClientInfo(user_agent=USER_AGENT)
with mock.patch("spanner_dbapi.spanner_v1.Client") as client_mock:
with mock.patch(
"spanner_dbapi.google_client_info", return_value=CLIENT_INFO
) as client_info_mock:
connection = connect(
"test-instance",
"test-database",
PROJECT,
CREDENTIALS,
USER_AGENT,
)
self.assertIsInstance(connection, Connection)
client_info_mock.assert_called_once_with(USER_AGENT)
client_mock.assert_called_once_with(
project=PROJECT,
credentials=CREDENTIALS,
client_info=CLIENT_INFO,
)
def test_instance_not_found(self):
with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists",
return_value=False,
) as exists_mock:
with self.assertRaises(ValueError):
connect("test-instance", "test-database")
exists_mock.assert_called_once_with()
def test_database_not_found(self):
with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists",
return_value=True,
):
with mock.patch(
"google.cloud.spanner_v1.database.Database.exists",
return_value=False,
) as exists_mock:
with self.assertRaises(ValueError):
connect("test-instance", "test-database")
exists_mock.assert_called_once_with()
def test_connect_instance_id(self):
INSTANCE = "test-instance"
with mock.patch(
"google.cloud.spanner_v1.client.Client.instance"
) as instance_mock:
connection = connect(INSTANCE, "test-database")
instance_mock.assert_called_once_with(INSTANCE)
self.assertIsInstance(connection, Connection)
def test_connect_database_id(self):
DATABASE = "test-database"
with mock.patch(
"google.cloud.spanner_v1.instance.Instance.database"
) as database_mock:
with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists",
return_value=True,
):
connection = connect("test-instance", DATABASE)
database_mock.assert_called_once_with(DATABASE, pool=mock.ANY)
self.assertIsInstance(connection, Connection)