File: test_analytical_ttl_async.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (279 lines) | stat: -rw-r--r-- 12,912 bytes parent folder | download
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
# The MIT License (MIT)
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# import unittest
# import uuid
# import time
# import pytest
#
# from azure.cosmos.aio import CosmosClient
# import azure.cosmos.exceptions as exceptions
# from azure.cosmos.http_constants import StatusCodes
# import test_config
# from azure.cosmos.partition_key import PartitionKey
#
#
# pytestmark = pytest.mark.cosmosEmulator
#
#
# # IMPORTANT NOTES:
#
# #  	Most test cases in this file create collections in your Azure Cosmos 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 account.
#
# #   In order for the analytical ttl tests to work you have to enable Azure Synapse Link for your Azure Cosmos DB
# #   account.
#
#
# @pytest.mark.usefixtures("teardown")
# class Test_ttl_tests(unittest.TestCase):
#     """TTL Unit Tests.
#     """
#
#     host = test_config._test_config.host
#     masterKey = test_config._test_config.masterKey
#     connectionPolicy = test_config._test_config.connectionPolicy
#
#     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 exceptions.CosmosHttpResponseError as inst:
#             self.assertEqual(inst.status_code, status_code)
#
#     @classmethod
#     async def setUpClass(cls):
#         if (cls.masterKey == '[YOUR_KEY_HERE]' or
#                 cls.host == '[YOUR_ENDPOINT_HERE]'):
#             raise Exception(
#                     "You must specify your Azure Cosmos account values for "
#                     "'masterKey' and 'host' at the top of this class to run the "
#                     "tests.")
#         cls.client = CosmosClient(cls.host, cls.masterKey, consistency_level="Session",
#                                                 connection_policy=cls.connectionPolicy)
#         cls.created_db = await cls.client.create_database_if_not_exists("TTL_tests_database" + str(uuid.uuid4()))
#
#     async def test_collection_ttl_replace_values(self):
#         created_collection = await self.created_db.create_container_if_not_exists(
#             id='test_ttl_values1' + str(uuid.uuid4()),
#             partition_key=PartitionKey(path='/id'),
#             analytical_storage_ttl=-1)
#         await self.created_db.replace_container(created_collection, partition_key=PartitionKey(path='/id'),
#                                           analytical_storage_ttl=1000)
#         created_collection_properties = await created_collection.read()
#         self.assertEqual(created_collection_properties['analyticalStorageTtl'], 1000)
#
#         await self.created_db.delete_container(container=created_collection)
#
#     async def test_collection_and_document_ttl_values(self):
#         ttl = 10
#         created_collection = await self.created_db.create_container_if_not_exists(
#             id='test_ttl_values1' + str(uuid.uuid4()),
#             partition_key=PartitionKey(path='/id'),
#             analytical_storage_ttl=ttl)
#         created_collection_properties = await created_collection.read()
#         self.assertEqual(created_collection_properties['analyticalStorageTtl'], ttl)
#
#         collection_id = 'test_ttl_values4' + str(uuid.uuid4())
#         ttl = -10
#
#         # -10 is an unsupported value for ttl. Valid values are -1 or a non-zero positive 32-bit integer value
#         self.__AssertHTTPFailureWithStatus(
#             StatusCodes.BAD_REQUEST,
#             await self.created_db.create_container,
#             collection_id,
#             PartitionKey(path='/id'),
#             None,
#             ttl)
#
#         document_definition = {'id': 'doc1' + str(uuid.uuid4()),
#                                '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(
#             StatusCodes.BAD_REQUEST,
#             created_collection.create_item,
#             document_definition)
#
#         document_definition['id'] = 'doc2' + str(uuid.uuid4())
#         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(
#             StatusCodes.BAD_REQUEST,
#             await created_collection.create_item,
#             document_definition)
#
#         await self.created_db.delete_container(container=created_collection)
#
#     async def test_document_ttl_with_positive_analyticalStorageTtl(self):
#         created_collection = await self.created_db.create_container_if_not_exists(
#             id='test_ttl_values3' + str(uuid.uuid4()),
#             partition_key=PartitionKey(path='/id'),
#             analytical_storage_ttl=5)
#
#         document_definition = {'id': 'doc1' + str(uuid.uuid4()),
#                                'name': 'sample document',
#                                'key': 'value'}
#
#         created_document = await created_collection.create_item(body=document_definition)
#
#         document_definition['id'] = 'doc2' + str(uuid.uuid4())
#         document_definition['ttl'] = -1
#         created_document = await created_collection.create_item(body=document_definition)
#
#         time.sleep(5)
#
#         # the created document should NOT be gone as its ttl value is set to -1(never expire) which overrides the
#         # collection's analyticalStorageTtl value
#         read_document = await created_collection.read_item(item=document_definition['id'],
#                                                      partition_key=document_definition['id'])
#         self.assertEqual(created_document['id'], read_document['id'])
#
#         document_definition['id'] = 'doc3' + str(uuid.uuid4())
#         document_definition['ttl'] = 2
#         created_document = await created_collection.create_item(body=document_definition)
#
#         document_definition['id'] = 'doc4' + str(uuid.uuid4())
#         document_definition['ttl'] = 8
#         created_document = await created_collection.create_item(body=document_definition)
#
#         time.sleep(6)
#
#         # the created document should NOT be gone as its ttl value is set to 8 which overrides the collection's
#         # analyticalStorageTtl value(5)
#         read_document = await created_collection.read_item(item=created_document['id'], partition_key=created_document['id'])
#         self.assertEqual(created_document['id'], read_document['id'])
#
#         time.sleep(4)
#
#         await self.created_db.delete_container(container=created_collection)
#
#     async def test_document_ttl_with_negative_one_analyticalStorageTtl(self):
#         created_collection = await self.created_db.create_container_if_not_exists(
#             id='test_ttl_values4' + str(uuid.uuid4()),
#             partition_key=PartitionKey(path='/id'),
#             analytical_storage_ttl=-1)
#
#         document_definition = {'id': 'doc1' + str(uuid.uuid4()),
#                                'name': 'sample document',
#                                'key': 'value'}
#
#         # the created document's ttl value would be -1 inherited from the collection's analyticalStorageTtl and this
#         # document will never expire
#         created_document1 = await created_collection.create_item(body=document_definition)
#
#         # This document is also set to never expire explicitly
#         document_definition['id'] = 'doc2' + str(uuid.uuid4())
#         document_definition['ttl'] = -1
#         created_document2 = await created_collection.create_item(body=document_definition)
#
#         document_definition['id'] = 'doc3' + str(uuid.uuid4())
#         document_definition['ttl'] = 2
#
#         # The documents with id doc1 and doc2 will never expire
#         read_document = await created_collection.read_item(item=created_document1['id'],
#                                                      partition_key=created_document1['id'])
#         self.assertEqual(created_document1['id'], read_document['id'])
#
#         read_document = await created_collection.read_item(item=created_document2['id'],
#                                                      partition_key=created_document2['id'])
#         self.assertEqual(created_document2['id'], read_document['id'])
#
#         await self.created_db.delete_container(container=created_collection)
#
#     async def test_document_ttl_with_no_analyticalStorageTtl(self):
#         created_collection = await self.created_db.create_container_if_not_exists(
#             id='test_ttl_no_analyticalStorageTtl' + str(uuid.uuid4()),
#             partition_key=PartitionKey(path='/id', kind='Hash')
#         )
#
#         document_definition = {'id': 'doc1' + str(uuid.uuid4()),
#                                'name': 'sample document',
#                                'key': 'value',
#                                'ttl': 5}
#
#         created_document = await created_collection.create_item(body=document_definition)
#
#         time.sleep(7)
#
#         # Created document still exists even after ttl time has passed since the TTL is disabled at collection level
#         # (no analyticalStorageTtl property defined)
#         read_document = await created_collection.read_item(item=created_document['id'], partition_key=created_document['id'])
#         self.assertEqual(created_document['id'], read_document['id'])
#
#         await self.created_db.delete_container(container=created_collection)
#
#     async def test_document_ttl_misc(self):
#         created_collection = await self.created_db.create_container_if_not_exists(
#             id='test_ttl_values5' + str(uuid.uuid4()),
#             partition_key=PartitionKey(path='/id'),
#             analytical_storage_ttl=8)
#
#         document_definition = {'id': 'doc1' + str(uuid.uuid4()),
#                                'name': 'sample document',
#                                'key': 'value'}
#
#         # We can create a document with the same id after the ttl time has expired
#         await created_collection.create_item(body=document_definition)
#         created_document = await created_collection.read_item(document_definition['id'], document_definition['id'])
#         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_document = await created_collection.upsert_item(body=document_definition)
#
#         time.sleep(7)
#
#         # Upserted document still exists after 10 secs from document creation time(with collection's
#         # analyticalStorageTtl set to 8) since its ttl was reset after 3 secs by upserting it
#         read_document = await created_collection.read_item(item=upserted_document['id'], partition_key=upserted_document['id'])
#         self.assertEqual(upserted_document['id'], read_document['id'])
#
#         time.sleep(3)
#
#         documents = [document async for document in created_collection.query_items(
#             query='SELECT * FROM root r',
#             enable_cross_partition_query=True
#         )]
#
#         self.assertEqual(1, len(documents))
#
#         # Removes analyticalStorageTtl property from collection to disable ttl at collection level
#         replaced_collection = await self.created_db.replace_container(
#             container=created_collection,
#             partition_key=PartitionKey(path='/id', kind='Hash'),
#             analytical_storage_ttl=None
#         )
#
#         document_definition['id'] = 'doc2' + str(uuid.uuid4())
#         created_document = await created_collection.create_item(body=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 = await created_collection.read_item(item=created_document['id'], partition_key=created_document['id'])
#         self.assertEqual(created_document['id'], read_document['id'])
#
#         await self.created_db.delete_container(container=created_collection)
#
#
# 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