8000 test: Use named db in system tests · googleapis/python-datastore@6321dae · GitHub
[go: up one dir, main page]

Skip to content

Commit 6321dae

Browse files
committed
test: Use named db in system tests
1 parent 490e4af commit 6321dae

File tree

7 files changed

+45
-9
lines changed

7 files changed

+45
-9
lines changed

CONTRIBUTING.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Running System Tests
174174

175175
- You'll also need stored data in your dataset. To populate this data, run::
176176

177+
$ export SYSTEM_TESTS_DATABASE=system-tests-named-db
177178
$ python tests/system/utils/populate_datastore.py
178179

179180
- If you make a mistake during development (i.e. a failing test that

noxfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ def install_systemtest_dependencies(session, *constraints):
230230

231231
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
232232
@nox.parametrize("disable_grpc", [False, True])
233-
def system(session, disable_grpc):
233+
@nox.parametrize("use_named_db", [False, True])
234+
def system(session, disable_grpc, use_named_db):
234235
"""Run the system test suite."""
235236
constraints_path = str(
236237
CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
@@ -256,6 +257,7 @@ def system(session, disable_grpc):
256257
env = {}
257258
if disable_grpc:
258259
env["GOOGLE_CLOUD_DISABLE_GRPC"] = "True"
260+
env["SYSTEM_TESTS_DATABASE"] = "system-tests-named-db" if use_named_db else ""
259261

260262
# Run py.test against the system tests.
261263
if system_test_exists:

owlbot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def system\(session\):
120120
"""\
121121
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
122122
@nox.parametrize("disable_grpc", [False, True])
123-
def system(session, disable_grpc):
123+
@nox.parametrize("use_named_db", [False, True])
124+
def system(session, disable_grpc, use_named_db):
124125
""",
125126
)
126127

@@ -133,6 +134,7 @@ def system(session, disable_grpc):
133134
env = {}
134135
if disable_grpc:
135136
env["GOOGLE_CLOUD_DISABLE_GRPC"] = "True"
137+
env["SYSTEM_TESTS_DATABASE"] = "system-tests-named-db" if use_named_db else ""
136138
137139
# Run py.test against the system tests.
138140
""",

tests/system/_helpers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from google.cloud.datastore.client import DATASTORE_DATASET
1919
from test_utils.system import unique_resource_id
2020

21+
_DATASTORE_DATABASE = "SYSTEM_TESTS_DATABASE"
22+
TEST_DATABASE = os.getenv(_DATASTORE_DATABASE)
2123
EMULATOR_DATASET = os.getenv(DATASTORE_DATASET)
2224

2325

tests/system/conftest.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ def test_namespace():
3131

3232
@pytest.fixture(scope="session")
3333
def data 10000 store_client(test_namespace):
34+
database = ""
35+
if _helpers.TEST_DATABASE is not None:
36+
database = _helpers.TEST_DATABASE
3437
if _helpers.EMULATOR_DATASET is not None:
3538
http = requests.Session() # Un-authorized.
36-
return datastore.Client(
39+
client = datastore.Client(
3740
project=_helpers.EMULATOR_DATASET,
41+
database=database,
3842
namespace=test_namespace,
3943
_http=http,
4044
)
4145
else:
42-
return datastore.Client(namespace=test_namespace)
46+
client = datastore.Client(database=database, namespace=test_namespace)
47+
48+
assert client.database == database
49+
return client
4350

4451

4552
@pytest.fixture(scope="function")

tests/system/utils/clear_datastore.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
MAX_DEL_ENTITIES = 500
3737

3838

39+
def get_system_test_db():
40+
return os.getenv("SYSTEM_TESTS_DATABASE") or "system-tests-named-db"
41+
42+
3943
def print_func(message):
4044
if os.getenv("GOOGLE_CLOUD_NO_PRINT") != "true":
4145
print(message)
@@ -85,14 +89,18 @@ def remove_all_entities(client):
8589
client.delete_multi(keys)
8690

8791

88-
def main():
89-
client = datastore.Client()
92+
def run(database):
93+
client = datastore.Client(database=database) 6D4E
9094
kinds = sys.argv[1:]
9195

9296
if len(kinds) == 0:
9397
kinds = ALL_KINDS
9498

95-
print_func("This command will remove all entities for " "the following kinds:")
99+
print_func(
100+
"This command will remove all entities from the database "
101+
+ database
102+
+ " for the following kinds:"
103+
)
96104
print_func("\n".join("- " + val for val in kinds))
97105
response = input("Is this OK [y/n]? ")
98106

@@ -105,5 +113,10 @@ def main():
105113
print_func("Doing nothing.")
106114

107115

116+
def main():
117+
for database in ["", get_system_test_db()]:
118+
run(database)
119+
120+
108121
if __name__ == "__main__":
109122
main()

tests/system/utils/populate_datastore.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
LARGE_CHARACTER_KIND = "LargeCharacter"
6060

6161

62+
def get_system_test_db():
63+
return os.getenv("SYSTEM_TESTS_DATABASE") or "system-tests-named-db"
64+
65+
6266
def print_func(message):
6367
if os.getenv("GOOGLE_CLOUD_NO_PRINT") != "true":
6468
print(message)
@@ -175,8 +179,8 @@ def add_timestamp_keys(client=None):
175179
batch.put(entity)
176180

177181

178-
def main():
179-
client = datastore.Client()
182+
def run(database):
183+
client = datastore.Client(database=database)
180184
flags = sys.argv[1:]
181185

182186
if len(flags) == 0:
@@ -192,5 +196,10 @@ def main():
192196
add_timestamp_keys(client)
193197

194198

199+
def main():
200+
for database in ["", get_system_test_db()]:
201+
run(database)
202+
203+
195204
if __name__ == "__main__":
196205
main()

0 commit comments

Comments
 (0)
0