Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = --log-cli-level=WARN
3 changes: 1 addition & 2 deletions tests/system/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

KIND = "SomeKind"
OTHER_KIND = "OtherKind"
OTHER_NAMESPACE = "other-namespace"


def eventually(f, predicate, timeout=60, interval=2):
def eventually(f, predicate, timeout=120, interval=2):
"""Runs `f` in a loop, hoping for eventual success.

Some things we're trying to test in Datastore are eventually
Expand Down
40 changes: 18 additions & 22 deletions tests/system/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import logging
import os
import uuid

Expand All @@ -10,7 +11,9 @@

from google.cloud.ndb import global_cache as global_cache_module

from . import KIND, OTHER_KIND, OTHER_NAMESPACE
from . import KIND, OTHER_KIND

log = logging.getLogger(__name__)


def _make_ds_client(namespace):
Expand All @@ -23,22 +26,14 @@ def _make_ds_client(namespace):
return client


def all_entities(client):
def all_entities(client, other_namespace):
return itertools.chain(
client.query(kind=KIND).fetch(),
client.query(kind=OTHER_KIND).fetch(),
client.query(namespace=OTHER_NAMESPACE).fetch(),
client.query(namespace=other_namespace).fetch(),
)


@pytest.fixture(scope="module", autouse=True)
def initial_clean():
# Make sure database is in clean state at beginning of test run
client = _make_ds_client(None)
for entity in all_entities(client):
client.delete(entity.key)


@pytest.fixture(scope="session")
def deleted_keys():
return set()
Expand All @@ -55,17 +50,10 @@ def ds_client(namespace):


@pytest.fixture
def with_ds_client(ds_client, to_delete, deleted_keys):
# Make sure we're leaving database as clean as we found it after each test
results = [
entity
for entity in all_entities(ds_client)
if entity.key not in deleted_keys
]
assert not results

def with_ds_client(ds_client, to_delete, deleted_keys, other_namespace):
yield ds_client

# Clean up after ourselves
while to_delete:
batch = to_delete[:500]
ds_client.delete_multi(batch)
Expand All @@ -74,10 +62,13 @@ def with_ds_client(ds_client, to_delete, deleted_keys):

not_deleted = [
entity
for entity in all_entities(ds_client)
for entity in all_entities(ds_client, other_namespace)
if entity.key not in deleted_keys
]
assert not not_deleted
if not_deleted:
log.warning(
"CLEAN UP: Entities not deleted from test: {}".format(not_deleted)
)


@pytest.fixture
Expand Down Expand Up @@ -125,6 +116,11 @@ def namespace():
return str(uuid.uuid4())


@pytest.fixture
def other_namespace():
return str(uuid.uuid4())


@pytest.fixture
def client_context(namespace):
client = ndb.Client(namespace=namespace)
Expand Down
10 changes: 5 additions & 5 deletions tests/system/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from google.cloud import ndb

from tests.system import KIND, OTHER_NAMESPACE, eventually
from tests.system import KIND, eventually


def _length_equals(n):
Expand Down Expand Up @@ -278,12 +278,12 @@ class SomeKind(ndb.Model):


@pytest.mark.usefixtures("client_context")
def test_namespace(dispose_of):
def test_namespace(dispose_of, other_namespace):
class SomeKind(ndb.Model):
foo = ndb.IntegerProperty()
bar = ndb.StringProperty()

entity1 = SomeKind(foo=1, bar="a", namespace=OTHER_NAMESPACE)
entity1 = SomeKind(foo=1, bar="a", namespace=other_namespace)
entity1.put()
dispose_of(entity1.key._key)

Expand All @@ -293,12 +293,12 @@ class SomeKind(ndb.Model):

eventually(SomeKind.query().fetch, _length_equals(1))

query = SomeKind.query(namespace=OTHER_NAMESPACE)
query = SomeKind.query(namespace=other_namespace)
results = eventually(query.fetch, _length_equals(1))

assert results[0].foo == 1
assert results[0].bar == "a"
assert results[0].key.namespace() == OTHER_NAMESPACE
assert results[0].key.namespace() == other_namespace


@pytest.mark.usefixtures("client_context")
Expand Down