Skip to content

Commit 72afdd0

Browse files
authored
[Cosmos] Add option for disabling auto id generation in create_item (Azure#15715)
* Add option for disabling auto id generation in create_item * Adds ID generation to kwargs, removes bad test * Default disableAutoID gen to true, add back test * use correct kwarg * Fix test typos * Change flag to enable instead of disable * Adds test for item create with no ID * disable header flag * Fixes auto ID generation in create_item * Remove print and unnecessary passthrough_args
1 parent f7e0d77 commit 72afdd0

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,20 +1072,21 @@ def CreateItem(self, database_or_container_link, document, options=None, **kwarg
10721072
# not each time the function is called (like it is in say, Ruby). This means
10731073
# that if you use a mutable default argument and mutate it, you will and have
10741074
# mutated that object for all future calls to the function as well. So, using
1075-
# a non-mutable deafult in this case(None) and assigning an empty dict(mutable)
1075+
# a non-mutable default in this case(None) and assigning an empty dict(mutable)
10761076
# inside the method For more details on this gotcha, please refer
10771077
# http://docs.python-guide.org/en/latest/writing/gotchas/
10781078
if options is None:
10791079
options = {}
10801080

10811081
# We check the link to be document collection link since it can be database
10821082
# link in case of client side partitioning
1083-
if base.IsItemContainerLink(database_or_container_link):
1084-
options = self._AddPartitionKey(database_or_container_link, document, options)
1085-
10861083
collection_id, document, path = self._GetContainerIdWithPathForItem(
10871084
database_or_container_link, document, options
10881085
)
1086+
1087+
if base.IsItemContainerLink(database_or_container_link):
1088+
options = self._AddPartitionKey(database_or_container_link, document, options)
1089+
10891090
return self.Create(document, path, "docs", collection_id, None, options, **kwargs)
10901091

10911092
def UpsertItem(self, database_or_container_link, document, options=None, **kwargs):

sdk/cosmos/azure-cosmos/azure/cosmos/container.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ def create_item(
485485
:param pre_trigger_include: trigger id to be used as pre operation trigger.
486486
:param post_trigger_include: trigger id to be used as post operation trigger.
487487
:param indexing_directive: Indicate whether the document should be omitted from indexing.
488+
:keyword bool enable_automatic_id_generation: Enable automatic id generation if no id present.
488489
:keyword str session_token: Token for use with Session consistency.
489490
:keyword dict[str,str] initial_headers: Initial headers to be sent as part of the request.
490491
:keyword str etag: An ETag value, or the wildcard character (*). Used to check if the resource
@@ -498,7 +499,7 @@ def create_item(
498499
request_options = build_options(kwargs)
499500
response_hook = kwargs.pop('response_hook', None)
500501

501-
request_options["disableAutomaticIdGeneration"] = True
502+
request_options["disableAutomaticIdGeneration"] = not kwargs.pop('enable_automatic_id_generation', False)
502503
if populate_query_metrics:
503504
request_options["populateQueryMetrics"] = populate_query_metrics
504505
if pre_trigger_include is not None:

sdk/cosmos/azure-cosmos/test/test_crud.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,15 @@ def test_document_crud(self):
770770
# create a document
771771
before_create_documents_count = len(documents)
772772

773+
# create a document with auto ID generation
774+
document_definition = {'name': 'sample document',
775+
'spam': 'eggs',
776+
'key': 'value'}
777+
778+
created_document = created_collection.create_item(body=document_definition, enable_automatic_id_generation=True)
779+
self.assertEqual(created_document.get('name'),
780+
document_definition['name'])
781+
773782
document_definition = {'name': 'sample document',
774783
'spam': 'eggs',
775784
'key': 'value',
@@ -790,7 +799,7 @@ def test_document_crud(self):
790799
documents = list(created_collection.read_all_items())
791800
self.assertEqual(
792801
len(documents),
793-
before_create_documents_count + 1,
802+
before_create_documents_count + 2,
794803
'create should increase the number of documents')
795804
# query documents
796805
documents = list(created_collection.query_items(

sdk/cosmos/azure-cosmos/test/test_ttl.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_document_ttl_with_positive_defaultTtl(self):
156156

157157
time.sleep(5)
158158

159-
# 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
159+
# the created document should NOT be gone as its ttl value is set to -1(never expire) which overrides the collection's defaultTtl value
160160
read_document = created_collection.read_item(item=document_definition['id'], partition_key=document_definition['id'])
161161
self.assertEqual(created_document['id'], read_document['id'])
162162

@@ -166,7 +166,7 @@ def test_document_ttl_with_positive_defaultTtl(self):
166166

167167
time.sleep(4)
168168

169-
# the created document should be gone now as it's ttl value is set to 2 which overrides the collections's defaultTtl value(5)
169+
# the created document should be gone now as its ttl value is set to 2 which overrides the collection's defaultTtl value(5)
170170
self.__AssertHTTPFailureWithStatus(
171171
StatusCodes.NOT_FOUND,
172172
created_collection.read_item,
@@ -180,7 +180,7 @@ def test_document_ttl_with_positive_defaultTtl(self):
180180

181181
time.sleep(6)
182182

183-
# the created document should NOT be gone as it's ttl value is set to 8 which overrides the collections's defaultTtl value(5)
183+
# the created document should NOT be gone as its ttl value is set to 8 which overrides the collection's defaultTtl value(5)
184184
read_document = created_collection.read_item(item=created_document['id'], partition_key=created_document['id'])
185185
self.assertEqual(created_document['id'], read_document['id'])
186186

@@ -221,7 +221,7 @@ def test_document_ttl_with_negative_one_defaultTtl(self):
221221

222222
time.sleep(4)
223223

224-
# the created document should be gone now as it's ttl value is set to 2 which overrides the collections's defaultTtl value(-1)
224+
# the created document should be gone now as it's ttl value is set to 2 which overrides the collection's defaultTtl value(-1)
225225
self.__AssertHTTPFailureWithStatus(
226226
StatusCodes.NOT_FOUND,
227227
created_collection.read_item,
@@ -294,7 +294,7 @@ def test_document_ttl_misc(self):
294294

295295
time.sleep(7)
296296

297-
# 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
297+
# Upserted document still exists after 10 secs from document creation time(with collection's defaultTtl set to 8) since its ttl was reset after 3 secs by upserting it
298298
read_document = created_collection.read_item(item=upserted_docment['id'], partition_key=upserted_docment['id'])
299299
self.assertEqual(upserted_docment['id'], read_document['id'])
300300

0 commit comments

Comments
 (0)