-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
260 lines (180 loc) · 7.49 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# -*- coding: utf-8 -*-
"""
tests.py
"""
import os
import sys
import logging
import unittest
sys.path.insert(0, '/usr/local/google_appengine')
import dev_appserver
dev_appserver.fix_sys_path()
from google.appengine.ext import testbed
from google.appengine.ext.ndb import model
from google.appengine.ext.ndb import tasklets
from google.appengine.datastore import datastore_stub_util
from babushka import BabushkaModel, BabushkaModelError
from example.models import Blog, Post
import webapp2
import yaml
import time
# --------------------------------------------------------------------
# Base Test Case
# --------------------------------------------------------------------
class BaseTestCase(unittest.TestCase):
def setUp(self):
"""Set up the test framework.
Service stubs are available for the following services:
- Datastore (use init_datastore_v3_stub)
- Memcache (use init_memcache_stub)
- Task Queue (use init_taskqueue_stub)
- Images (only for dev_appserver; use init_images_stub)
- URL fetch (use init_urlfetch_stub)
- User service (use init_user_stub)
- XMPP (use init_xmpp_stub)
"""
# First, create an instance of the Testbed class.
self.testbed = testbed.Testbed()
# Then activate the testbed, which prepares the service stubs for use.
self.testbed.activate()
# To set custom env vars, pass them as kwargs *after* activate().
# self.setup_env()
# Next, declare which service stubs you want to use.
# self.testbed.init_memcache_stub()
# self.testbed.init_user_stub()
# Add taskqueue support with our queue.yaml path
# self.testbed.init_taskqueue_stub(root_path=os.path.dirname(os.path.dirname( __file__ )))
# Create a consistency policy that will simulate the High Replication consistency model.
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=0)
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy, require_indexes=False)
# self.testbed.init_datastore_v3_stub(consistency_policy=self.policy, require_indexes=True,
# root_path=os.path.dirname(os.path.dirname( __file__ )))
# Only when testing ndb.
self.reset_kind_map()
self.setup_context_cache()
def tearDown(self):
# This restores the original stubs so that tests do not interfere
# with each other.
self.testbed.deactivate()
# Clear thread-local variables.
self.clear_globals()
def reset_kind_map(self):
model.Model._reset_kind_map()
def setup_context_cache(self):
"""Set up the context cache.
We only need cache active when testing the cache, so the default
behavior is to disable it to avoid misleading test results. Override
this when needed.
"""
ctx = tasklets.get_context()
ctx.set_cache_policy(False)
ctx.set_memcache_policy(False)
def clear_globals(self):
webapp2._local.__release_local__()
def register_model(self, name, cls):
model.Model._kind_map[name] = cls
# --------------------------------------------------------------------
# Babushka Test Case
# --------------------------------------------------------------------
class BabushkaTestCase(BaseTestCase):
def test_creation(self):
self.register_model('Blog', Blog)
self.register_model('Post', Post)
data = yaml.load(open('example/data.yaml'))
blog = Blog.get_or_insert(data['blog']['name'], name=data['blog']['name'])
for post in data['blog']['posts']:
post = Post(parent=blog.key, **post)
post.put()
for post in data['blog']['posts']:
post = Post(blog=blog.key, **post)
post.put()
def test_bad_assignment(self):
class Blog(BabushkaModel):
pass
class Post(BabushkaModel):
blog = model.KeyProperty(kind='Blog', required=False)
title = model.StringProperty(required=True)
_cache_break = 'badattr'
blog = Blog.get_or_insert('main')
post = Post(parent=blog.key, title='title')
self.assertRaises(BabushkaModelError, post.put)
Post._cache_break = 'title'
post = Post(parent=blog.key, title='title')
self.assertRaises(BabushkaModelError, post.put)
Post._cache_break = 'blog'
post = Post(parent=blog.key, title='title')
post.put()
def test_assignments(self):
class Post(BabushkaModel):
blog = model.KeyProperty(kind='Blog', required=False)
title = model.StringProperty(required=True)
post = Post(title='title')
post.put()
class Post(BabushkaModel):
blog = model.KeyProperty(kind='Blog', required=False)
title = model.StringProperty(required=True)
class Babushka:
cache_break = 'blog'
post = Post(title='title')
post.put()
class Post(BabushkaModel):
blog = model.KeyProperty(kind='Blog', required=False)
title = model.StringProperty(required=True)
_cache_break = 'blog'
post = Post(title='title')
post.put()
class Post(BabushkaModel):
blog = model.KeyProperty(kind='Blog', required=False)
title = model.StringProperty(required=True)
_cache_break = None
post = Post(title='title')
post.put()
def test_cache_keys(self):
class Blog(BabushkaModel):
pass
class Post(BabushkaModel):
blog = model.KeyProperty(kind='Blog', required=False)
title = model.StringProperty(required=True)
blog = Blog.get_or_insert('main')
post = Post(parent=blog.key, title='title')
post.put()
old_blog_cache_key = blog.cache_key
old_post_cache_key = post.cache_key
post.title = 'new title'
post.put()
blog = Blog.get_by_id('main')
self.assertNotEqual(blog.cache_key, old_blog_cache_key)
self.assertNotEqual(post.cache_key, old_post_cache_key)
class Blog(BabushkaModel):
pass
class Post(BabushkaModel):
blog = model.KeyProperty(kind='Blog', required=False)
title = model.StringProperty(required=True)
blog = Blog.get_or_insert('main')
post = Post(blog=blog.key, title='title')
post.put()
old_blog_cache_key = blog.cache_key
old_post_cache_key = post.cache_key
post.title = 'new title'
post.put()
blog = Blog.get_by_id('main')
self.assertEqual(blog.cache_key, old_blog_cache_key)
self.assertNotEqual(post.cache_key, old_post_cache_key)
class Blog(BabushkaModel):
pass
class Post(BabushkaModel):
blog = model.KeyProperty(kind='Blog', required=False)
title = model.StringProperty(required=True)
_cache_break = 'blog'
blog = Blog.get_or_insert('main')
post = Post(blog=blog.key, title='title')
post.put()
old_blog_cache_key = blog.cache_key
old_post_cache_key = post.cache_key
post.title = 'new title'
post.put()
blog = Blog.get_by_id('main')
self.assertNotEqual(blog.cache_key, old_blog_cache_key)
self.assertNotEqual(post.cache_key, old_post_cache_key)
if __name__ == '__main__':
unittest.main()