Skip to content

Commit e8ea8ce

Browse files
author
Chris Rossi
authored
test: address flakiness in metadata system tests (#726)
Fixes #703, #66
1 parent e503de8 commit e8ea8ce

File tree

1 file changed

+94
-82
lines changed

1 file changed

+94
-82
lines changed

packages/google-cloud-ndb/tests/system/test_metadata.py

Lines changed: 94 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@
1919

2020
from google.cloud import ndb
2121

22-
from . import eventually
22+
from test_utils import retry
2323

2424

25-
def _length_at_least(n):
26-
def predicate(sequence):
27-
return len(sequence) >= n
28-
29-
return predicate
25+
_retry_assertion_errors = retry.RetryErrors(AssertionError)
3026

3127

3228
@pytest.mark.usefixtures("client_context")
@@ -47,11 +43,14 @@ class MyKind(ndb.Model):
4743
entity2.put()
4844
dispose_of(entity2.key._key)
4945

50-
query = ndb.Query(kind=Kind.KIND_NAME, namespace="_test_namespace_")
51-
results = eventually(query.fetch, _length_at_least(2))
46+
@_retry_assertion_errors
47+
def query_metadata():
48+
query = ndb.Query(kind=Kind.KIND_NAME, namespace="_test_namespace_")
49+
results = query.fetch()
50+
kinds = [result.kind_name for result in results]
51+
assert all(kind in kinds for kind in ["AnyKind", "MyKind"])
5252

53-
kinds = [result.kind_name for result in results]
54-
assert all(kind in kinds for kind in ["AnyKind", "MyKind"]) != []
53+
query_metadata()
5554

5655

5756
@pytest.mark.usefixtures("client_context")
@@ -86,23 +85,26 @@ class SomeKind(ndb.Model):
8685
entity4.put()
8786
dispose_of(entity4.key._key)
8887

89-
kinds = eventually(get_kinds, _length_at_least(4))
90-
assert (
91-
all(kind in kinds for kind in ["AnyKind", "MyKind", "OtherKind", "SomeKind"])
92-
!= []
93-
)
88+
@_retry_assertion_errors
89+
def query_metadata():
90+
kinds = get_kinds()
91+
assert all(
92+
kind in kinds for kind in ["AnyKind", "MyKind", "OtherKind", "SomeKind"]
93+
)
94+
95+
kinds = get_kinds(start="N")
96+
assert all(kind in kinds for kind in ["OtherKind", "SomeKind"]) != []
97+
assert not any(kind in kinds for kind in ["AnyKind", "MyKind"])
9498

95-
kinds = get_kinds(start="N")
96-
assert all(kind in kinds for kind in ["OtherKind", "SomeKind"]) != []
97-
assert not any(kind in kinds for kind in ["AnyKind", "MyKind"])
99+
kinds = get_kinds(end="N")
100+
assert all(kind in kinds for kind in ["AnyKind", "MyKind"]) != []
101+
assert not any(kind in kinds for kind in ["OtherKind", "SomeKind"])
98102

99-
kinds = get_kinds(end="N")
100-
assert all(kind in kinds for kind in ["AnyKind", "MyKind"]) != []
101-
assert not any(kind in kinds for kind in ["OtherKind", "SomeKind"])
103+
kinds = get_kinds(start="L", end="P")
104+
assert all(kind in kinds for kind in ["MyKind", "OtherKind"]) != []
105+
assert not any(kind in kinds for kind in ["AnyKind", "SomeKind"])
102106

103-
kinds = get_kinds(start="L", end="P")
104-
assert all(kind in kinds for kind in ["MyKind", "OtherKind"]) != []
105-
assert not any(kind in kinds for kind in ["AnyKind", "SomeKind"])
107+
query_metadata()
106108

107109

108110
@pytest.mark.usefixtures("client_context")
@@ -123,13 +125,15 @@ class AnyKind(ndb.Model):
123125
entity2.put()
124126
dispose_of(entity2.key._key)
125127

126-
query = ndb.Query(kind=Namespace.KIND_NAME)
127-
results = eventually(query.fetch, _length_at_least(2))
128+
@_retry_assertion_errors
129+
def query_metadata():
130+
query = ndb.Query(kind=Namespace.KIND_NAME)
131+
results = query.fetch()
128132

129-
names = [result.namespace_name for result in results]
130-
assert (
131-
all(name in names for name in ["_test_namespace_", "_test_namespace_2_"]) != []
132-
)
133+
names = [result.namespace_name for result in results]
134+
assert all(name in names for name in ["_test_namespace_", "_test_namespace_2_"])
135+
136+
query_metadata()
133137

134138

135139
@pytest.mark.usefixtures("client_context")
@@ -151,22 +155,23 @@ class AnyKind(ndb.Model):
151155
entity3.put()
152156
dispose_of(entity3.key._key)
153157

154-
names = eventually(get_namespaces, _length_at_least(3))
155-
assert (
156-
all(
158+
@_retry_assertion_errors
159+
def query_metadata():
160+
names = get_namespaces()
161+
assert all(
157162
name in names for name in ["CoolNamespace", "MyNamespace", "OtherNamespace"]
158163
)
159-
!= []
160-
)
161164

162-
names = get_namespaces(start="L")
163-
assert all(name in names for name in ["MyNamespace", "OtherNamspace"]) != []
165+
names = get_namespaces(start="L")
166+
assert all(name in names for name in ["MyNamespace", "OtherNamspace"]) != []
167+
168+
names = get_namespaces(end="N")
169+
assert all(name in names for name in ["CoolNamespace", "MyNamespace"]) != []
164170

165-
names = get_namespaces(end="N")
166-
assert all(name in names for name in ["CoolNamespace", "MyNamespace"]) != []
171+
names = get_namespaces(start="D", end="N")
172+
assert all(name in names for name in ["MyNamespace"]) != []
167173

168-
names = get_namespaces(start="D", end="N")
169-
assert all(name in names for name in ["MyNamespace"]) != []
174+
query_metadata()
170175

171176

172177
@pytest.mark.usefixtures("client_context")
@@ -184,13 +189,17 @@ class AnyKind(ndb.Model):
184189
entity1.put()
185190
dispose_of(entity1.key._key)
186191

187-
query = ndb.Query(kind=Property.KIND_NAME)
188-
results = eventually(query.fetch, _length_at_least(2))
192+
@_retry_assertion_errors
193+
def query_metadata():
194+
query = ndb.Query(kind=Property.KIND_NAME)
195+
results = query.fetch()
196+
197+
properties = [
198+
result.property_name for result in results if result.kind_name == "AnyKind"
199+
]
200+
assert properties == ["bar", "foo"]
189201

190-
properties = [
191-
result.property_name for result in results if result.kind_name == "AnyKind"
192-
]
193-
assert properties == ["bar", "foo"]
202+
query_metadata()
194203

195204

196205
@pytest.mark.usefixtures("client_context")
@@ -207,20 +216,21 @@ class AnyKind(ndb.Model):
207216
entity1.put()
208217
dispose_of(entity1.key._key)
209218

210-
properties = eventually(
211-
lambda: get_properties_of_kind("AnyKind"), _length_at_least(4)
212-
)
219+
@_retry_assertion_errors
220+
def query_metadata():
221+
properties = get_properties_of_kind("AnyKind")
222+
assert properties == ["bar", "baz", "foo", "qux"]
213223

214-
assert properties == ["bar", "baz", "foo", "qux"]
224+
properties = get_properties_of_kind("AnyKind", start="c")
225+
assert properties == ["foo", "qux"]
215226

216-
properties = get_properties_of_kind("AnyKind", start="c")
217-
assert properties == ["foo", "qux"]
227+
properties = get_properties_of_kind("AnyKind", end="e")
228+
assert properties == ["bar", "baz"]
218229

219-
properties = get_properties_of_kind("AnyKind", end="e")
220-
assert properties == ["bar", "baz"]
230+
properties = get_properties_of_kind("AnyKind", start="c", end="p")
231+
assert properties == ["foo"]
221232

222-
properties = get_properties_of_kind("AnyKind", start="c", end="p")
223-
assert properties == ["foo"]
233+
query_metadata()
224234

225235

226236
@pytest.mark.usefixtures("client_context")
@@ -238,20 +248,21 @@ class AnyKind(ndb.Model):
238248
entity1.put()
239249
dispose_of(entity1.key._key)
240250

241-
properties = eventually(
242-
lambda: get_properties_of_kind("AnyKind"), _length_at_least(4)
243-
)
251+
@_retry_assertion_errors
252+
def query_metadata():
253+
properties = get_properties_of_kind("AnyKind")
254+
assert properties == ["bar", "baz", "foo", "qux"]
244255

245-
assert properties == ["bar", "baz", "foo", "qux"]
256+
properties = get_properties_of_kind("AnyKind", start="c")
257+
assert properties == ["foo", "qux"]
246258

247-
properties = get_properties_of_kind("AnyKind", start="c")
248-
assert properties == ["foo", "qux"]
259+
properties = get_properties_of_kind("AnyKind", end="e")
260+
assert properties == ["bar", "baz"]
249261

250-
properties = get_properties_of_kind("AnyKind", end="e")
251-
assert properties == ["bar", "baz"]
262+
properties = get_properties_of_kind("AnyKind", start="c", end="p")
263+
assert properties == ["foo"]
252264

253-
properties = get_properties_of_kind("AnyKind", start="c", end="p")
254-
assert properties == ["foo"]
265+
query_metadata()
255266

256267

257268
@pytest.mark.usefixtures("client_context")
@@ -268,22 +279,23 @@ class AnyKind(ndb.Model):
268279
entity1.put()
269280
dispose_of(entity1.key._key)
270281

271-
representations = eventually(
272-
lambda: get_representations_of_kind("AnyKind"), _length_at_least(4)
273-
)
282+
@_retry_assertion_errors
283+
def query_metadata():
284+
representations = get_representations_of_kind("AnyKind")
285+
assert representations == {
286+
"bar": ["STRING"],
287+
"baz": ["INT64"],
288+
"foo": ["INT64"],
289+
"qux": ["STRING"],
290+
}
274291

275-
assert representations == {
276-
"bar": ["STRING"],
277-
"baz": ["INT64"],
278-
"foo": ["INT64"],
279-
"qux": ["STRING"],
280-
}
292+
representations = get_representations_of_kind("AnyKind", start="c")
293+
assert representations == {"foo": ["INT64"], "qux": ["STRING"]}
281294

282-
representations = get_representations_of_kind("AnyKind", start="c")
283-
assert representations == {"foo": ["INT64"], "qux": ["STRING"]}
295+
representations = get_representations_of_kind("AnyKind", end="e")
296+
assert representations == {"bar": ["STRING"], "baz": ["INT64"]}
284297

285-
representations = get_representations_of_kind("AnyKind", end="e")
286-
assert representations == {"bar": ["STRING"], "baz": ["INT64"]}
298+
representations = get_representations_of_kind("AnyKind", start="c", end="p")
299+
assert representations == {"foo": ["INT64"]}
287300

288-
representations = get_representations_of_kind("AnyKind", start="c", end="p")
289-
assert representations == {"foo": ["INT64"]}
301+
query_metadata()

0 commit comments

Comments
 (0)