This repository was archived by the owner on Dec 25, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathstats_test.py
More file actions
347 lines (310 loc) · 13.6 KB
/
Copy pathstats_test.py
File metadata and controls
347 lines (310 loc) · 13.6 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#
# Copyright 2008 The ndb Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for stats.py."""
import datetime
import os
from .google_imports import datastore
from .google_test_imports import unittest
from . import stats
from . import test_utils
class StatsTests(test_utils.NDBTest):
def setUp(self):
"""Setup test infrastructure."""
super(StatsTests, self).setUp()
self.PopulateStatEntities()
the_module = stats
def PopulateStatEntities(self):
"""Insert stat entities into Cloud Datastore."""
# GlobalStat
self.CreateStatEntity(stats.GlobalStat.STORED_KIND_NAME,
has_entity_bytes=True,
has_builtin_index_stats=True,
has_composite_index_stats=True)
# NamespaceStat
self.CreateStatEntity(stats.NamespaceStat.STORED_KIND_NAME,
subject_namespace='name-space',
has_entity_bytes=True,
has_builtin_index_stats=True,
has_composite_index_stats=True)
# KindStat
self.CreateStatEntity(stats.KindStat.STORED_KIND_NAME, 'foo',
has_entity_bytes=True,
has_builtin_index_stats=True,
has_composite_index_stats=True)
self.CreateStatEntity(stats.KindStat.STORED_KIND_NAME, 'foo2',
has_entity_bytes=True,
has_builtin_index_stats=True,
has_composite_index_stats=True)
# KindRootEntityStat
self.CreateStatEntity(stats.KindRootEntityStat.STORED_KIND_NAME, 'foo3',
has_entity_bytes=True)
self.CreateStatEntity(stats.KindRootEntityStat.STORED_KIND_NAME, 'foo4',
has_entity_bytes=True)
# KindNonRootEntityStat
self.CreateStatEntity(stats.KindNonRootEntityStat.STORED_KIND_NAME, 'foo5',
has_entity_bytes=True)
self.CreateStatEntity(stats.KindNonRootEntityStat.STORED_KIND_NAME, 'foo6',
has_entity_bytes=True)
# PropertyTypeStat
self.CreateStatEntity(stats.PropertyTypeStat.STORED_KIND_NAME,
property_type='pt1',
has_entity_bytes=True,
has_builtin_index_stats=True)
self.CreateStatEntity(stats.PropertyTypeStat.STORED_KIND_NAME,
property_type='pt2',
has_entity_bytes=True,
has_builtin_index_stats=True)
# KindPropertyTypeStat
self.CreateStatEntity(stats.KindPropertyTypeStat.STORED_KIND_NAME,
kind_name='foo1',
property_type='pt1',
has_entity_bytes=True,
has_builtin_index_stats=True)
self.CreateStatEntity(stats.KindPropertyTypeStat.STORED_KIND_NAME,
kind_name='foo1',
property_type='pt2',
has_entity_bytes=True,
has_builtin_index_stats=True)
self.CreateStatEntity(stats.KindPropertyTypeStat.STORED_KIND_NAME,
kind_name='foo2',
property_type='pt2',
has_entity_bytes=True,
has_builtin_index_stats=True)
# KindPropertyNameStat
self.CreateStatEntity(stats.KindPropertyNameStat.STORED_KIND_NAME,
kind_name='foo11',
property_name='pn1',
has_entity_bytes=True,
has_builtin_index_stats=True)
self.CreateStatEntity(stats.KindPropertyNameStat.STORED_KIND_NAME,
kind_name='foo11',
property_name='pn2',
has_entity_bytes=True,
has_builtin_index_stats=True)
self.CreateStatEntity(stats.KindPropertyNameStat.STORED_KIND_NAME,
kind_name='foo21',
property_name='pn2',
has_entity_bytes=True,
has_builtin_index_stats=True)
# KindPropertyNamePropertyTypeStat
self.CreateStatEntity(
stats.KindPropertyNamePropertyTypeStat.STORED_KIND_NAME,
kind_name='foo12',
property_type='pt1',
property_name='pn1',
has_entity_bytes=True,
has_builtin_index_stats=True)
self.CreateStatEntity(
stats.KindPropertyNamePropertyTypeStat.STORED_KIND_NAME,
kind_name='foo12',
property_type='pt2',
property_name='pn2',
has_entity_bytes=True,
has_builtin_index_stats=True)
self.CreateStatEntity(
stats.KindPropertyNamePropertyTypeStat.STORED_KIND_NAME,
kind_name='foo22',
property_type='pt2',
property_name='pn2',
has_entity_bytes=True,
has_builtin_index_stats=True)
# KindCompositeIndexStat
self.CreateStatEntity(
stats.KindCompositeIndexStat.STORED_KIND_NAME,
kind_name='foo12',
composite_index_id=1)
self.CreateStatEntity(
stats.KindCompositeIndexStat.STORED_KIND_NAME,
kind_name='foo12',
composite_index_id=2)
self.CreateStatEntity(
stats.KindCompositeIndexStat.STORED_KIND_NAME,
kind_name='foo22',
composite_index_id=3)
def CreateStatEntity(self,
kind,
kind_name=None,
property_type=None,
property_name=None,
subject_namespace=None,
composite_index_id=None,
has_entity_bytes=None,
has_builtin_index_stats=None,
has_composite_index_stats=None):
"""Create a single Statistic datastore entity.
Args:
kind: The name of the kind to store.
kind_name: The value of the 'kind_name' property to set on the entity.
property_type: The value of the 'property_type' property to set on the
entity.
property_name: The value of the 'property_name' property to set on the
entity.
subject_namespace: The namespace for NamespaceStat entities.
composite_index_id: The index id of composite index.
has_entity_bytes: The stat has the entity_bytes property.
has_builtin_index_stats: The stat entity has builtin_index_bytes and
builtin_index_count.
has_composite_index_stats: The stat entity has composite_index_bytes and
composite_index_count.
"""
stat = datastore.Entity(kind)
stat['bytes'] = 4
stat['count'] = 2
stat['timestamp'] = datetime.datetime.utcfromtimestamp(40)
if has_entity_bytes:
stat['entity_bytes'] = 2
if has_builtin_index_stats:
stat['builtin_index_count'] = 3
stat['builtin_index_bytes'] = 1
if has_composite_index_stats:
stat['composite_index_count'] = 2
stat['composite_index_bytes'] = 1
if kind_name is not None:
stat['kind_name'] = kind_name
if property_type is not None:
stat['property_type'] = property_type
if property_name is not None:
stat['property_name'] = property_name
if subject_namespace is not None:
stat['subject_namespace'] = subject_namespace
if composite_index_id is not None:
stat['index_id'] = composite_index_id
datastore.Put(stat)
def testGlobalStat(self):
"""Test fetching the global stat singleton."""
res = stats.GlobalStat.query().fetch()
self.assertEquals(1, len(res))
self.assertEquals(4, res[0].bytes)
self.assertEquals(2, res[0].entity_bytes)
self.assertEquals(3, res[0].builtin_index_count)
self.assertEquals(1, res[0].builtin_index_bytes)
self.assertEquals(2, res[0].composite_index_count)
self.assertEquals(1, res[0].composite_index_bytes)
def testNamespaceStat(self):
"""Test fetching the global stat singleton."""
res = stats.NamespaceStat.query().fetch()
self.assertEquals(1, len(res))
self.assertEquals(4, res[0].bytes)
self.assertEquals('name-space', res[0].subject_namespace)
self.assertEquals(2, res[0].entity_bytes)
self.assertEquals(3, res[0].builtin_index_count)
self.assertEquals(1, res[0].builtin_index_bytes)
self.assertEquals(2, res[0].composite_index_count)
self.assertEquals(1, res[0].composite_index_bytes)
def testKindStat(self):
"""Test fetching the Kind stats."""
res = stats.KindStat.query().fetch()
self.assertEquals(2, len(res))
self.assertEquals('foo', res[0].kind_name)
self.assertEquals('foo2', res[1].kind_name)
self.assertEquals(2, res[0].entity_bytes)
self.assertEquals(3, res[0].builtin_index_count)
self.assertEquals(1, res[0].builtin_index_bytes)
self.assertEquals(2, res[0].composite_index_count)
self.assertEquals(1, res[0].composite_index_bytes)
def testKindRootEntityStat(self):
"""Test fetching the Kind root entity stats."""
res = stats.KindRootEntityStat.query().fetch()
self.assertEquals(2, len(res))
self.assertEquals('foo3', res[0].kind_name)
self.assertEquals('foo4', res[1].kind_name)
self.assertEquals(2, res[0].entity_bytes)
def testKindNonRootEntityStat(self):
"""Test fetching the Kind non-root entity stats."""
res = stats.KindNonRootEntityStat.query().fetch()
self.assertEquals(2, len(res))
self.assertEquals('foo5', res[0].kind_name)
self.assertEquals('foo6', res[1].kind_name)
self.assertEquals(2, res[0].entity_bytes)
def testPropertyTypeStat(self):
"""Test fetching the property type stats."""
res = stats.PropertyTypeStat.query().fetch()
self.assertEquals(2, len(res))
self.assertEquals('pt1', res[0].property_type)
self.assertEquals('pt2', res[1].property_type)
self.assertEquals(2, res[0].entity_bytes)
self.assertEquals(3, res[0].builtin_index_count)
self.assertEquals(1, res[0].builtin_index_bytes)
def testKindPropertyTypeStat(self):
"""Test fetching the (kind, property type) stats."""
res = stats.KindPropertyTypeStat.query().fetch()
self.assertEquals(3, len(res))
self.assertEquals('foo1', res[0].kind_name)
self.assertEquals('pt1', res[0].property_type)
self.assertEquals('foo1', res[1].kind_name)
self.assertEquals('pt2', res[1].property_type)
self.assertEquals('foo2', res[2].kind_name)
self.assertEquals('pt2', res[2].property_type)
self.assertEquals(2, res[0].entity_bytes)
self.assertEquals(3, res[0].builtin_index_count)
self.assertEquals(1, res[0].builtin_index_bytes)
query = stats.KindPropertyTypeStat.query(
stats.KindPropertyTypeStat.kind_name == 'foo2')
res = query.fetch()
self.assertEquals(1, len(res))
self.assertEquals('foo2', res[0].kind_name)
def testKindPropertyNameStat(self):
"""Test fetching the (kind, property name) type stats."""
res = stats.KindPropertyNameStat.query().fetch()
self.assertEquals(3, len(res))
self.assertEquals('foo11', res[0].kind_name)
self.assertEquals('pn1', res[0].property_name)
self.assertEquals('foo11', res[1].kind_name)
self.assertEquals('pn2', res[1].property_name)
self.assertEquals('foo21', res[2].kind_name)
self.assertEquals('pn2', res[2].property_name)
self.assertEquals(2, res[0].entity_bytes)
self.assertEquals(3, res[0].builtin_index_count)
self.assertEquals(1, res[0].builtin_index_bytes)
query = stats.KindPropertyNameStat.query(
stats.KindPropertyNameStat.kind_name == 'foo21')
res = query.fetch()
self.assertEquals(1, len(res))
self.assertEquals('foo21', res[0].kind_name)
def testKindPropertyNamePropertyTypeStat(self):
"""Test fetching the (kind, property name, property type) stats."""
res = stats.KindPropertyNamePropertyTypeStat.query().fetch()
self.assertEquals(3, len(res))
self.assertEquals('foo12', res[0].kind_name)
self.assertEquals('pn1', res[0].property_name)
self.assertEquals('pt1', res[0].property_type)
self.assertEquals('foo12', res[1].kind_name)
self.assertEquals('pn2', res[1].property_name)
self.assertEquals('pt2', res[1].property_type)
self.assertEquals('foo22', res[2].kind_name)
self.assertEquals('pn2', res[2].property_name)
self.assertEquals('pt2', res[2].property_type)
self.assertEquals(2, res[0].entity_bytes)
self.assertEquals(3, res[0].builtin_index_count)
self.assertEquals(1, res[0].builtin_index_bytes)
query = stats.KindPropertyNamePropertyTypeStat.query(
stats.KindPropertyNamePropertyTypeStat.kind_name == 'foo22')
res = query.fetch()
self.assertEquals(1, len(res))
self.assertEquals('foo22', res[0].kind_name)
def testKindCompositeIndex(self):
"""Test fetching the (kind, composite index id) stats."""
res = stats.KindCompositeIndexStat.query().fetch()
self.assertEquals(3, len(res))
self.assertEquals('foo12', res[0].kind_name)
self.assertEquals(1, res[0].index_id)
self.assertEquals('foo12', res[1].kind_name)
self.assertEquals(2, res[1].index_id)
self.assertEquals('foo22', res[2].kind_name)
self.assertEquals(3, res[2].index_id)
self.assertEquals(4, res[0].bytes)
self.assertEquals(2, res[0].count)
if __name__ == '__main__':
unittest.main()