This repository was archived by the owner on May 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathconftest.py
More file actions
105 lines (87 loc) · 2.45 KB
/
Copy pathconftest.py
File metadata and controls
105 lines (87 loc) · 2.45 KB
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
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import configparser
import datetime
import os
import uuid
import pytest
from sqlalchemy import (
Column,
Integer,
MetaData,
String,
Table,
create_engine,
ForeignKey,
)
@pytest.fixture
def db_url():
project = os.getenv(
"GOOGLE_CLOUD_PROJECT",
os.getenv("PROJECT_ID", "emulator-test-project"),
)
db_url = (
f"spanner:///projects/{project}/instances/"
"sqlalchemy-dialect-test/databases/compliance-test"
)
config = configparser.ConfigParser()
if os.path.exists("test.cfg"):
config.read("test.cfg")
else:
config.read("setup.cfg")
return config.get("db", "default", fallback=db_url)
@pytest.fixture
def table_id():
now = datetime.datetime.now()
table_id = "example_table_{}_{}".format(
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
)
return table_id
@pytest.fixture
def table(db_url, table_id):
engine = create_engine(db_url)
metadata = MetaData(bind=engine)
table = Table(
table_id,
metadata,
Column("user_id", Integer, primary_key=True),
Column("user_name", String(16), nullable=False),
)
table.create()
yield table
table.drop()
@pytest.fixture
def table_w_foreign_key(db_url, table):
engine = create_engine(db_url)
metadata = MetaData(bind=engine)
table_fk = Table(
"table_fk",
metadata,
Column("id", Integer, primary_key=True),
Column("name", String(16), nullable=False),
Column(
table.name + "_user_id",
Integer,
ForeignKey(table.c.user_id, name=table.name + "user_id"),
),
)
table_fk.create()
yield table_fk
table_fk.drop()
@pytest.fixture
def connection(db_url):
engine = create_engine(db_url)
return engine.connect()
def insert_data(conn, table, data):
conn.execute(table.insert(), data)