Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d38279f

Browse files
cgwywsh
authored andcommitted
To make two more ndb's unittests work with cloud emulator, avoid calling create_index.
For query_test, we just need to delete the call to create_index; For model_test, we need to set up index for the test, which requires an app root_path. The v3 service method being tested is GetIndices, CreateIndex is called to for test set-up. This works with the old GAE SDK. But with cloud emulator, CreateIndex is not supported. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=161211686
1 parent be27f13 commit d38279f

File tree

3 files changed

+48
-69
lines changed

3 files changed

+48
-69
lines changed

ndb/model_test.py

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from .google_imports import memcache
3030
from .google_imports import namespace_manager
3131
from .google_imports import users
32-
from .google_test_imports import datastore_stub_util
3332
from .google_test_imports import real_unittest
3433
from .google_test_imports import unittest
3534

@@ -5210,66 +5209,56 @@ def testRejectOldPickles(self):
52105209

52115210
class IndexTests(test_utils.NDBTest):
52125211

5213-
def create_index(self):
5214-
ci = datastore_stub_util.datastore_pb.CompositeIndex()
5215-
ci.set_app_id(os.environ['APPLICATION_ID'])
5216-
ci.set_id(0)
5217-
ci.set_state(ci.WRITE_ONLY)
5218-
index = ci.mutable_definition()
5219-
index.set_ancestor(0)
5220-
index.set_entity_type('Kind')
5221-
property = index.add_property()
5222-
property.set_name('property1')
5223-
property.set_direction(property.DESCENDING)
5224-
property = index.add_property()
5225-
property.set_name('property2')
5226-
property.set_direction(property.ASCENDING)
5227-
stub = self.testbed.get_stub('datastore_v3')
5228-
stub.CreateIndex(ci)
5212+
def generate_index(self):
5213+
"""Run a query to trigger index generation.
5214+
5215+
Note, index generation is idempotent. Running same query multiple times does
5216+
not change existing index. Hence we don't need to clear indexes between
5217+
tests.
5218+
"""
5219+
5220+
# pylint: disable=unused-variable
5221+
class Kind(model.Model):
5222+
property1 = model.TextProperty()
5223+
property2 = model.TextProperty()
5224+
# pylint: enable=unused-variable
5225+
5226+
qry = query.gql('SELECT * FROM Kind ORDER BY property1 DESC, property2')
5227+
qry.fetch()
52295228

52305229
def testGetIndexes(self):
52315230
self.assertEqual([], model.get_indexes())
52325231

5233-
self.create_index()
5232+
self.generate_index()
52345233

52355234
self.assertEqual(
5236-
[model.IndexState(
5237-
definition=model.Index(kind='Kind',
5238-
properties=[
5239-
model.IndexProperty(name='property1',
5240-
direction='desc'),
5241-
model.IndexProperty(name='property2',
5242-
direction='asc'),
5243-
],
5244-
ancestor=False),
5245-
state='building',
5246-
id=1,
5247-
),
5248-
],
5249-
model.get_indexes())
5235+
model.Index(kind='Kind',
5236+
properties=[
5237+
model.IndexProperty(name='property1',
5238+
direction='desc'),
5239+
model.IndexProperty(name='property2',
5240+
direction='asc'),
5241+
],
5242+
ancestor=False),
5243+
model.get_indexes()[0].definition)
52505244

52515245
def testGetIndexesAsync(self):
52525246
fut = model.get_indexes_async()
52535247
self.assertTrue(isinstance(fut, tasklets.Future))
52545248
self.assertEqual([], fut.get_result())
52555249

5256-
self.create_index()
5250+
self.generate_index()
52575251

52585252
self.assertEqual(
5259-
[model.IndexState(
5260-
definition=model.Index(kind='Kind',
5261-
properties=[
5262-
model.IndexProperty(name='property1',
5263-
direction='desc'),
5264-
model.IndexProperty(name='property2',
5265-
direction='asc'),
5266-
],
5267-
ancestor=False),
5268-
state='building',
5269-
id=1,
5270-
),
5271-
],
5272-
model.get_indexes_async().get_result())
5253+
model.Index(kind='Kind',
5254+
properties=[
5255+
model.IndexProperty(name='property1',
5256+
direction='desc'),
5257+
model.IndexProperty(name='property2',
5258+
direction='asc'),
5259+
],
5260+
ancestor=False),
5261+
model.get_indexes_async().get_result()[0].definition)
52735262

52745263

52755264
class CacheTests(test_utils.NDBTest):

ndb/query_test.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"""Tests for query.py."""
1717

1818
import datetime
19-
import os
2019
import pickle
2120

2221
from .google_imports import datastore_errors
@@ -2032,26 +2031,8 @@ class Bar(model.Model):
20322031
class IndexListTestMixin(object):
20332032
"""Tests for Index lists. Must be used with BaseQueryTestMixin."""
20342033

2035-
def create_index(self):
2036-
ci = datastore_stub_util.datastore_pb.CompositeIndex()
2037-
ci.set_app_id(os.environ['APPLICATION_ID'])
2038-
ci.set_id(0)
2039-
ci.set_state(ci.WRITE_ONLY)
2040-
index = ci.mutable_definition()
2041-
index.set_ancestor(0)
2042-
index.set_entity_type('Foo')
2043-
property = index.add_property()
2044-
property.set_name('name')
2045-
property.set_direction(property.DESCENDING)
2046-
property = index.add_property()
2047-
property.set_name('tags')
2048-
property.set_direction(property.ASCENDING)
2049-
stub = self.testbed.get_stub('datastore_v3')
2050-
stub.CreateIndex(ci)
2051-
20522034
def testIndexListPremature(self):
20532035
# Before calling next() we don't have the information.
2054-
self.create_index()
20552036
q = Foo.query(Foo.name >= 'joe', Foo.tags == 'joe')
20562037
qi = q.iter()
20572038
self.assertEqual(qi.index_list(), None)
@@ -2097,7 +2078,6 @@ def testIndexListExhausted(self):
20972078
def testIndexListWithIndexAndOrder(self):
20982079
# Test a non-trivial query with sort order and an actual composite
20992080
# index present.
2100-
self.create_index()
21012081
q = Foo.query(Foo.name >= 'joe', Foo.tags == 'joe')
21022082
q = q.order(-Foo.name, Foo.tags)
21032083
qi = q.iter()
@@ -2115,7 +2095,6 @@ def testIndexListWithIndexAndOrder(self):
21152095
id=0)])
21162096

21172097
def testIndexListMultiQuery(self):
2118-
self.create_index()
21192098
q = Foo.query(query.OR(Foo.name == 'joe', Foo.name == 'jill'))
21202099
qi = q.iter()
21212100
qi.next()

ndb/test_utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"""
2222

2323
import logging
24+
import shutil
2425
import tempfile
2526
import os
2627

@@ -148,10 +149,20 @@ def HRTest(self):
148149

149150
def setUp(self):
150151
super(NDBTest, self).setUp()
151-
self.testbed.init_datastore_v3_stub()
152+
153+
# model_test.IndexTests requries index setup.
154+
# The legacy datastore_stub requires an gae app root_path for index.yaml
155+
# before updating datastore index.
156+
self.app_root_path = tempfile.mkdtemp()
157+
158+
self.testbed.init_datastore_v3_stub(root_path=self.app_root_path)
152159
self.testbed.init_memcache_stub()
153160
self.testbed.init_taskqueue_stub()
154161

162+
def tearDown(self):
163+
super(NDBTest, self).tearDown()
164+
shutil.rmtree(self.app_root_path)
165+
155166
def MakeConnection(self, *args, **kwargs):
156167
return model.make_connection(*args, **kwargs)
157168

0 commit comments

Comments
 (0)