forked from Azure/azure-cosmos-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathttl_tests.py
356 lines (263 loc) · 14.7 KB
/
ttl_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
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
348
349
350
351
352
353
354
355
356
#The MIT License (MIT)
#Copyright (c) 2014 Microsoft Corporation
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
import unittest
import time
import pydocumentdb.document_client as document_client
import pydocumentdb.errors as errors
import test.test_config as test_config
#IMPORTANT NOTES:
# Most test cases in this file create collections in your Azure Cosmos DB account.
# Collections are billing entities. By running these test cases, you may incur monetary costs on your account.
# To Run the test, replace the two member fields (masterKey and host) with values
# associated with your Azure Cosmos DB account.
class Test_ttl_tests(unittest.TestCase):
"""TTL Unit Tests.
"""
host = test_config._test_config.host
masterKey = test_config._test_config.masterKey
testDbName = 'sample database'
def __AssertHTTPFailureWithStatus(self, status_code, func, *args, **kwargs):
"""Assert HTTP failure with status.
:Parameters:
- `status_code`: int
- `func`: function
"""
try:
func(*args, **kwargs)
self.assertFalse(True, 'function should fail.')
except errors.HTTPFailure as inst:
self.assertEqual(inst.status_code, status_code)
@classmethod
def setUpClass(cls):
if (cls.masterKey == '[YOUR_KEY_HERE]' or
cls.host == '[YOUR_ENDPOINT_HERE]'):
raise Exception(
"You must specify your Azure Cosmos DB account values for "
"'masterKey' and 'host' at the top of this class to run the "
"tests.")
def setUp(self):
client = document_client.DocumentClient(Test_ttl_tests.host,
{'masterKey': Test_ttl_tests.masterKey})
query_iterable = client.QueryDatabases('SELECT * FROM root r WHERE r.id=\'' + Test_ttl_tests.testDbName + '\'')
it = iter(query_iterable)
test_db = next(it, None)
if test_db is not None:
client.DeleteDatabase(test_db['_self'])
def test_collection_and_document_ttl_values(self):
client = document_client.DocumentClient(Test_ttl_tests.host, {'masterKey': Test_ttl_tests.masterKey})
created_db = client.CreateDatabase({ 'id': Test_ttl_tests.testDbName })
collection_definition = {'id' : 'sample collection1',
'defaultTtl' : 5
}
created_collection = client.CreateCollection(created_db['_self'], collection_definition)
self.assertEqual(created_collection['defaultTtl'], collection_definition['defaultTtl'])
collection_definition['id'] = 'sample collection2'
collection_definition['defaultTtl'] = None
# None is an unsupported value for defaultTtl. Valid values are -1 or a non-zero positive 32-bit integer value
self.__AssertHTTPFailureWithStatus(
400,
client.CreateCollection,
created_db['_self'],
collection_definition)
collection_definition['id'] = 'sample collection3'
collection_definition['defaultTtl'] = 0
# 0 is an unsupported value for defaultTtl. Valid values are -1 or a non-zero positive 32-bit integer value
self.__AssertHTTPFailureWithStatus(
400,
client.CreateCollection,
created_db['_self'],
collection_definition)
collection_definition['id'] = 'sample collection4'
collection_definition['defaultTtl'] = -10
# -10 is an unsupported value for defaultTtl. Valid values are -1 or a non-zero positive 32-bit integer value
self.__AssertHTTPFailureWithStatus(
400,
client.CreateCollection,
created_db['_self'],
collection_definition)
document_definition = { 'id': 'doc1',
'name': 'sample document',
'key': 'value',
'ttl' : 0}
# 0 is an unsupported value for ttl. Valid values are -1 or a non-zero positive 32-bit integer value
self.__AssertHTTPFailureWithStatus(
400,
client.CreateDocument,
created_collection['_self'],
document_definition)
document_definition['id'] = 'doc2'
document_definition['ttl'] = None
# None is an unsupported value for ttl. Valid values are -1 or a non-zero positive 32-bit integer value
self.__AssertHTTPFailureWithStatus(
400,
client.CreateDocument,
created_collection['_self'],
document_definition)
document_definition['id'] = 'doc3'
document_definition['ttl'] = -10
# -10 is an unsupported value for ttl. Valid values are -1 or a non-zero positive 32-bit integer value
self.__AssertHTTPFailureWithStatus(
400,
client.CreateDocument,
created_collection['_self'],
document_definition)
def test_document_ttl_with_positive_defaultTtl(self):
client = document_client.DocumentClient(Test_ttl_tests.host, {'masterKey': Test_ttl_tests.masterKey})
created_db = client.CreateDatabase({ 'id': Test_ttl_tests.testDbName })
collection_definition = {'id' : 'sample collection',
'defaultTtl' : 5
}
created_collection = client.CreateCollection(created_db['_self'], collection_definition)
document_definition = { 'id': 'doc1',
'name': 'sample document',
'key': 'value'}
created_document = client.CreateDocument(created_collection['_self'], document_definition)
time.sleep(7)
# the created document should be gone now as it's ttl value would be same as defaultTtl value of the collection
self.__AssertHTTPFailureWithStatus(
404,
client.ReadDocument,
created_document['_self'])
document_definition['id'] = 'doc2'
document_definition['ttl'] = -1
created_document = client.CreateDocument(created_collection['_self'], document_definition)
time.sleep(5)
# the created document should NOT be gone as it's ttl value is set to -1(never expire) which overrides the collections's defaultTtl value
read_document = client.ReadDocument(created_document['_self'])
self.assertEqual(created_document['id'], read_document['id'])
document_definition['id'] = 'doc3'
document_definition['ttl'] = 2
created_document = client.CreateDocument(created_collection['_self'], document_definition)
time.sleep(4)
# the created document should be gone now as it's ttl value is set to 2 which overrides the collections's defaultTtl value(5)
self.__AssertHTTPFailureWithStatus(
404,
client.ReadDocument,
created_document['_self'])
document_definition['id'] = 'doc4'
document_definition['ttl'] = 8
created_document = client.CreateDocument(created_collection['_self'], document_definition)
time.sleep(6)
# the created document should NOT be gone as it's ttl value is set to 8 which overrides the collections's defaultTtl value(5)
read_document = client.ReadDocument(created_document['_self'])
self.assertEqual(created_document['id'], read_document['id'])
time.sleep(4)
# the created document should be gone now as we have waited for (6+4) secs which is greater than documents's ttl value of 8
self.__AssertHTTPFailureWithStatus(
404,
client.ReadDocument,
created_document['_self'])
def test_document_ttl_with_negative_one_defaultTtl(self):
client = document_client.DocumentClient(Test_ttl_tests.host, {'masterKey': Test_ttl_tests.masterKey})
created_db = client.CreateDatabase({ 'id': Test_ttl_tests.testDbName })
collection_definition = {'id' : 'sample collection',
'defaultTtl' : -1
}
created_collection = client.CreateCollection(created_db['_self'], collection_definition)
document_definition = { 'id': 'doc1',
'name': 'sample document',
'key': 'value'}
# the created document's ttl value would be -1 inherited from the collection's defaultTtl and this document will never expire
created_document1 = client.CreateDocument(created_collection['_self'], document_definition)
# This document is also set to never expire explicitly
document_definition['id'] = 'doc2'
document_definition['ttl'] = -1
created_document2 = client.CreateDocument(created_collection['_self'], document_definition)
document_definition['id'] = 'doc3'
document_definition['ttl'] = 2
created_document3 = client.CreateDocument(created_collection['_self'], document_definition)
time.sleep(4)
# the created document should be gone now as it's ttl value is set to 2 which overrides the collections's defaultTtl value(-1)
self.__AssertHTTPFailureWithStatus(
404,
client.ReadDocument,
created_document3['_self'])
# The documents with id doc1 and doc2 will never expire
read_document = client.ReadDocument(created_document1['_self'])
self.assertEqual(created_document1['id'], read_document['id'])
read_document = client.ReadDocument(created_document2['_self'])
self.assertEqual(created_document2['id'], read_document['id'])
def test_document_ttl_with_no_defaultTtl(self):
client = document_client.DocumentClient(Test_ttl_tests.host, {'masterKey': Test_ttl_tests.masterKey})
created_db = client.CreateDatabase({ 'id': Test_ttl_tests.testDbName })
collection_definition = {'id' : 'sample collection' }
created_collection = client.CreateCollection(created_db['_self'], collection_definition)
document_definition = { 'id': 'doc1',
'name': 'sample document',
'key': 'value',
'ttl' : 5}
created_document = client.CreateDocument(created_collection['_self'], document_definition)
time.sleep(7)
# Created document still exists even after ttl time has passed since the TTL is disabled at collection level(no defaultTtl property defined)
read_document = client.ReadDocument(created_document['_self'])
self.assertEqual(created_document['id'], read_document['id'])
def test_document_ttl_misc(self):
client = document_client.DocumentClient(Test_ttl_tests.host, {'masterKey': Test_ttl_tests.masterKey})
created_db = client.CreateDatabase({ 'id': Test_ttl_tests.testDbName })
collection_definition = {'id' : 'sample collection',
'defaultTtl' : 8
}
created_collection = client.CreateCollection(created_db['_self'], collection_definition)
document_definition = { 'id': 'doc1',
'name': 'sample document',
'key': 'value'}
created_document = client.CreateDocument(created_collection['_self'], document_definition)
time.sleep(10)
# the created document cannot be deleted since it should already be gone now
self.__AssertHTTPFailureWithStatus(
404,
client.DeleteDocument,
created_document['_self'])
# We can create a document with the same id after the ttl time has expired
created_document = client.CreateDocument(created_collection['_self'], document_definition)
self.assertEqual(created_document['id'], document_definition['id'])
time.sleep(3)
# Upsert the document after 3 secs to reset the document's ttl
document_definition['key'] = 'value2'
upserted_docment = client.UpsertDocument(created_collection['_self'], document_definition)
time.sleep(7)
# Upserted document still exists after 10 secs from document creation time(with collection's defaultTtl set to 8) since it's ttl was reset after 3 secs by upserting it
read_document = client.ReadDocument(upserted_docment['_self'])
self.assertEqual(upserted_docment['id'], read_document['id'])
time.sleep(3)
# the upserted document should be gone now after 10 secs from the last write(upsert) of the document
self.__AssertHTTPFailureWithStatus(
404,
client.ReadDocument,
upserted_docment['_self'])
documents = list(client.QueryDocuments(
created_collection['_self'],
{
'query': 'SELECT * FROM root r'
}))
self.assertEqual(0, len(documents))
# Removes defaultTtl property from collection to disable ttl at collection level
collection_definition.pop('defaultTtl')
replaced_collection = client.ReplaceCollection(created_collection['_self'], collection_definition)
document_definition['id'] = 'doc2'
created_document = client.CreateDocument(replaced_collection['_self'], document_definition)
time.sleep(5)
# Created document still exists even after ttl time has passed since the TTL is disabled at collection level
read_document = client.ReadDocument(created_document['_self'])
self.assertEqual(created_document['id'], read_document['id'])
if __name__ == '__main__':
try:
unittest.main()
except SystemExit as inst:
if inst.args[0] is True: # raised by sys.exit(True) when tests failed
raise