Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 406c34b

Browse filesBrowse files
feat: add code samples (googleapis#55)
1 parent af3b97b commit 406c34b
Copy full SHA for 406c34b

File tree

Expand file treeCollapse file tree

6 files changed

+706
-1
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+706
-1
lines changed

‎CONTRIBUTING.md

Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,29 @@ information on using pull requests.
2626

2727
This project follows [Google's Open Source Community
2828
Guidelines](https://opensource.google/conduct/).
29+
30+
## Running tests
31+
32+
SQLAlchemy Spanner dialect includes a test suite, which can be executed both on a live service and Spanner emulator.
33+
34+
**Using pytest**
35+
To execute the test suite with standard `pytest` package you only need to checkout to the package folder and run:
36+
```
37+
pytest -v
38+
```
39+
40+
**Using nox**
41+
The package includes a configuration file for `nox` package, which allows to execute the dialect test suite in an isolated virtual environment. To execute all the `nox` sessions checkout to the dialect folder and then run command:
42+
```
43+
nox
44+
```
45+
To execute only the dialect compliance test suite execute command:
46+
```
47+
nox -s compliance_test
48+
```
49+
50+
**Live service**
51+
To run the test suite on a live service use [setup.cfg](https://github.com/cloudspannerecosystem/python-spanner-sqlalchemy/blob/main/setup.cfg) `db.default` attribute to set URI of the project, instance and database, where the tests should be executed.
52+
53+
**Emulator**
54+
As the dialect is built on top of the Spanner DB API, it also supports running on Spanner emulator. To make it happen you need to set an environment variable, pointing to the emulator service, for example `SPANNER_EMULATOR_HOST=localhost:9010`

‎noxfile.py

Copy file name to clipboardExpand all lines: noxfile.py
+24-1Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class = StreamHandler
6464

6565

6666
BLACK_VERSION = "black==19.10b0"
67-
BLACK_PATHS = ["google", "test", "noxfile.py", "setup.py"]
67+
BLACK_PATHS = ["google", "test", "noxfile.py", "setup.py", "samples"]
6868
DEFAULT_PYTHON_VERSION = "3.8"
6969

7070

@@ -178,3 +178,26 @@ def migration_test(session):
178178
session.run("python", "migration_test_cleanup.py")
179179
if os.path.exists("test.cfg"):
180180
os.remove("test.cfg")
181+
182+
183+
@nox.session(python=DEFAULT_PYTHON_VERSION)
184+
def snippets(session):
185+
"""Run the documentation example snippets."""
186+
# Sanity check: Only run snippets system tests if the environment variable
187+
# is set.
188+
if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""):
189+
session.skip("Credentials must be set via environment variable.")
190+
191+
session.install("pytest")
192+
session.install("sqlalchemy")
193+
session.install(
194+
"git+https://github.com/googleapis/python-spanner.git#egg=google-cloud-spanner"
195+
)
196+
session.install("-e", ".")
197+
session.run("python", "create_test_database.py")
198+
session.run(
199+
"py.test",
200+
"--quiet",
201+
os.path.join("samples", "snippets_test.py"),
202+
*session.posargs,
203+
)

‎samples/__init__.py

Copy file name to clipboardExpand all lines: samples/__init__.py
Whitespace-only changes.

‎samples/conftest.py

Copy file name to clipboard
+92Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
import uuid
17+
18+
import pytest
19+
20+
from sqlalchemy import (
21+
Column,
22+
Integer,
23+
MetaData,
24+
String,
25+
Table,
26+
create_engine,
27+
ForeignKey,
28+
)
29+
30+
31+
@pytest.fixture
32+
def db_url():
33+
return (
34+
"spanner:///projects/appdev-soda-spanner-staging/instances/"
35+
"sqlalchemy-dialect-test/databases/compliance-test"
36+
)
37+
38+
39+
@pytest.fixture
40+
def table_id():
41+
now = datetime.datetime.now()
42+
table_id = "example_table_{}_{}".format(
43+
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
44+
)
45+
return table_id
46+
47+
48+
@pytest.fixture
49+
def table(db_url, table_id):
50+
engine = create_engine(db_url)
51+
metadata = MetaData(bind=engine)
52+
53+
table = Table(
54+
table_id,
55+
metadata,
56+
Column("user_id", Integer, primary_key=True),
57+
Column("user_name", String(16), nullable=False),
58+
)
59+
table.create()
60+
yield table
61+
table.drop()
62+
63+
64+
@pytest.fixture
65+
def table_w_foreign_key(db_url, table):
66+
engine = create_engine(db_url)
67+
metadata = MetaData(bind=engine)
68+
69+
table_fk = Table(
70+
"table_fk",
71+
metadata,
72+
Column("id", Integer, primary_key=True),
73+
Column("name", String(16), nullable=False),
74+
Column(
75+
table.name + "_user_id",
76+
Integer,
77+
ForeignKey(table.c.user_id, name=table.name + "user_id"),
78+
),
79+
)
80+
table_fk.create()
81+
yield table_fk
82+
table_fk.drop()
83+
84+
85+
@pytest.fixture
86+
def connection(db_url):
87+
engine = create_engine(db_url)
88+
return engine.connect()
89+
90+
91+
def insert_data(conn, table, data):
92+
conn.execute(table.insert(), data)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.