|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | | - |
| 14 | +from logging import Logger |
15 | 15 | from mock import create_autospec |
| 16 | +from typing import Mapping |
| 17 | + |
| 18 | +from google.cloud.spanner_dbapi import Connection |
| 19 | +from google.cloud.spanner_v1 import SpannerClient |
| 20 | +from google.cloud.spanner_v1.client import Client |
| 21 | +from google.cloud.spanner_v1.database import Database |
| 22 | +from google.cloud.spanner_v1.instance import Instance |
| 23 | +from google.cloud.spanner_v1.session import Session |
16 | 24 |
|
17 | 25 | # Default values used to populate required or expected attributes. |
18 | 26 | # Tests should not depend on them: if a test requires a specific |
|
22 | 30 | _DATABASE_ID = "default-database-id" |
23 | 31 |
|
24 | 32 |
|
25 | | -def build_logger(): |
26 | | - """Builds and returns a logger for testing.""" |
27 | | - from logging import Logger |
28 | | - |
29 | | - return create_autospec(Logger, instance=True) |
30 | | - |
31 | | - |
32 | | -# Client objects |
33 | | -# -------------- |
34 | | - |
35 | | - |
36 | | -def build_client(**kwargs): |
| 33 | +def build_client(**kwargs: Mapping) -> Client: |
37 | 34 | """Builds and returns a client for testing using the given arguments. |
38 | 35 | If a required argument is not provided, a default value will be used.""" |
39 | | - from google.cloud.spanner_v1 import Client |
40 | 36 |
|
41 | 37 | if "project" not in kwargs: |
42 | 38 | kwargs["project"] = _PROJECT_ID |
43 | 39 |
|
44 | 40 | return Client(**kwargs) |
45 | 41 |
|
46 | 42 |
|
47 | | -def build_database(**kwargs): |
| 43 | +def build_connection(**kwargs: Mapping) -> Connection: |
| 44 | + """Builds and returns a connection for testing using the given arguments. |
| 45 | + If a required argument is not provided, a default value will be used.""" |
| 46 | + |
| 47 | + if "instance" not in kwargs: |
| 48 | + kwargs["instance"] = build_instance() |
| 49 | + |
| 50 | + if "database" not in kwargs: |
| 51 | + kwargs["database"] = build_database(instance=kwargs["instance"]) |
| 52 | + |
| 53 | + return Connection(**kwargs) |
| 54 | + |
| 55 | + |
| 56 | +def build_database(**kwargs: Mapping) -> Database: |
48 | 57 | """Builds and returns a database for testing using the given arguments. |
49 | | - If a required argument is not provided, a default value will be used..""" |
50 | | - from google.cloud.spanner_v1.database import Database |
| 58 | + If a required argument is not provided, a default value will be used.""" |
51 | 59 |
|
52 | 60 | if "database_id" not in kwargs: |
53 | 61 | kwargs["database_id"] = _DATABASE_ID |
54 | 62 |
|
55 | 63 | if "logger" not in kwargs: |
56 | 64 | kwargs["logger"] = build_logger() |
57 | 65 |
|
58 | | - if "instance" not in kwargs or isinstance(kwargs["instance"], dict): |
59 | | - instance_args = kwargs.pop("instance", {}) |
60 | | - kwargs["instance"] = build_instance(**instance_args) |
| 66 | + if "instance" not in kwargs: |
| 67 | + kwargs["instance"] = build_instance() |
61 | 68 |
|
62 | 69 | database = Database(**kwargs) |
63 | 70 | database._spanner_api = build_spanner_api() |
64 | 71 |
|
65 | 72 | return database |
66 | 73 |
|
67 | 74 |
|
68 | | -def build_instance(**kwargs): |
| 75 | +def build_instance(**kwargs: Mapping) -> Instance: |
69 | 76 | """Builds and returns an instance for testing using the given arguments. |
70 | 77 | If a required argument is not provided, a default value will be used.""" |
71 | | - from google.cloud.spanner_v1.instance import Instance |
72 | 78 |
|
73 | 79 | if "instance_id" not in kwargs: |
74 | 80 | kwargs["instance_id"] = _INSTANCE_ID |
75 | 81 |
|
76 | | - if "client" not in kwargs or isinstance(kwargs["client"], dict): |
77 | | - client_args = kwargs.pop("client", {}) |
78 | | - kwargs["client"] = build_client(**client_args) |
| 82 | + if "client" not in kwargs: |
| 83 | + kwargs["client"] = build_client() |
79 | 84 |
|
80 | 85 | return Instance(**kwargs) |
81 | 86 |
|
82 | 87 |
|
83 | | -def build_spanner_api(): |
| 88 | +def build_logger() -> Logger: |
| 89 | + """Builds and returns a logger for testing.""" |
| 90 | + return create_autospec(Logger, instance=True) |
| 91 | + |
| 92 | + |
| 93 | +def build_session(**kwargs: Mapping) -> Session: |
| 94 | + """Builds and returns a session for testing using the given arguments. |
| 95 | + If a required argument is not provided, a default value will be used.""" |
| 96 | + |
| 97 | + if "database" not in kwargs: |
| 98 | + kwargs["database"] = build_database() |
| 99 | + |
| 100 | + return Session(**kwargs) |
| 101 | + |
| 102 | + |
| 103 | +def build_spanner_api() -> SpannerClient: |
84 | 104 | """Builds and returns a mock Spanner Client API for testing using the given arguments. |
85 | 105 | Commonly used methods are mocked to return default values.""" |
86 | | - from google.cloud.spanner_v1 import SpannerClient |
87 | 106 |
|
88 | 107 | return create_autospec(SpannerClient, instance=True) |
0 commit comments