Skip to content

Commit 470b6ca

Browse files
andrewmathew1Andrew Mathew
andauthored
Replace Throughput Bugfix (#41564)
* replace throughput bugfix * minor changes to tests * added async and negative testing for replace throughput * moved increment percent back into max_throughput if statement * added pytest.raises to negative test --------- Co-authored-by: Andrew Mathew <[email protected]>
1 parent 48821ba commit 470b6ca

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

sdk/cosmos/azure-cosmos/azure/cosmos/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,8 @@ def _replace_throughput(
797797
new_throughput_properties['content']['offerAutopilotSettings']['maxThroughput'] = max_throughput
798798
if increment_percent:
799799
new_throughput_properties['content']['offerAutopilotSettings']['autoUpgradePolicy']['throughputPolicy']['incrementPercent'] = increment_percent # pylint: disable=line-too-long
800-
if throughput.offer_throughput:
801-
new_throughput_properties["content"]["offerThroughput"] = throughput.offer_throughput
800+
if throughput.offer_throughput:
801+
new_throughput_properties["content"]["offerThroughput"] = throughput.offer_throughput
802802
except AttributeError as e:
803803
raise TypeError("offer_throughput must be int or an instance of ThroughputProperties") from e
804804

sdk/cosmos/azure-cosmos/tests/test_crud_container.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import azure.cosmos.documents as documents
2525
import azure.cosmos.exceptions as exceptions
2626
import test_config
27-
from azure.cosmos import _retry_utility
27+
from azure.cosmos import _retry_utility, ThroughputProperties
2828
from azure.cosmos.http_constants import HttpHeaders, StatusCodes
2929
from azure.cosmos.partition_key import PartitionKey
3030

@@ -954,7 +954,34 @@ def test_index_progress_headers(self):
954954

955955
created_db.delete_container(none_coll)
956956

957+
def test_replace_throughput_offer_with_int(self):
958+
created_db = self.databaseForTest
959+
collection = created_db.get_container_client(self.configs.TEST_MULTI_PARTITION_CONTAINER_ID)
960+
961+
new_throughput = ThroughputProperties(offer_throughput=2500)
962+
collection.replace_throughput(new_throughput.offer_throughput)
963+
964+
retrieve_throughput = collection.get_throughput()
965+
assert getattr(retrieve_throughput, "offer_throughput") == getattr(new_throughput, "offer_throughput")
966+
967+
def test_replace_throughput_offer_with_object(self):
968+
created_db = self.databaseForTest
969+
collection = created_db.get_container_client(self.configs.TEST_MULTI_PARTITION_CONTAINER_ID)
970+
971+
new_throughput = ThroughputProperties(offer_throughput=2500)
972+
collection.replace_throughput(new_throughput)
973+
974+
retrieve_throughput = collection.get_throughput()
975+
assert getattr(retrieve_throughput, "offer_throughput") == getattr(new_throughput, "offer_throughput")
976+
977+
def test_negative_replace_throughput_with_all_configs_set(self):
978+
created_db = self.databaseForTest
979+
collection = created_db.get_container_client(self.configs.TEST_MULTI_PARTITION_CONTAINER_ID)
980+
981+
new_throughput = ThroughputProperties(offer_throughput=2500, auto_scale_max_throughput=4000, auto_scale_increment_percent=5)
957982

983+
with pytest.raises(KeyError):
984+
collection.replace_throughput(new_throughput)
958985

959986
def _MockExecuteFunction(self, function, *args, **kwargs):
960987
if HttpHeaders.PartitionKey in args[4].headers:

sdk/cosmos/azure-cosmos/tests/test_crud_container_async.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from azure.cosmos.aio import CosmosClient, _retry_utility_async, DatabaseProxy
2626
from azure.cosmos.http_constants import HttpHeaders, StatusCodes
2727
from azure.cosmos.partition_key import PartitionKey
28+
from azure.cosmos import ThroughputProperties
2829

2930

3031
class TimeoutTransport(AsyncioRequestsTransport):
@@ -956,7 +957,6 @@ def __validate_offer_response_body(self, offer, expected_coll_link, expected_off
956957
assert expected_offer_type == offer.properties.get('offerType')
957958

958959
async def test_offer_read_and_query_async(self):
959-
960960
# Create database.
961961
db = self.database_for_test
962962

@@ -968,7 +968,6 @@ async def test_offer_read_and_query_async(self):
968968
self.__validate_offer_response_body(expected_offer, collection_properties.get('_self'), None)
969969

970970
async def test_offer_replace_async(self):
971-
972971
collection = self.database_for_test.get_container_client(self.configs.TEST_MULTI_PARTITION_CONTAINER_ID)
973972
# Read Offer
974973
expected_offer = await collection.get_throughput()
@@ -984,7 +983,6 @@ async def test_offer_replace_async(self):
984983
assert expected_offer.offer_throughput + 100 == replaced_offer.offer_throughput
985984

986985
async def test_index_progress_headers_async(self):
987-
988986
created_db = self.database_for_test
989987
consistent_coll = created_db.get_container_client(self.configs.TEST_MULTI_PARTITION_CONTAINER_ID)
990988
created_container = created_db.get_container_client(container=consistent_coll)
@@ -1007,6 +1005,35 @@ async def test_index_progress_headers_async(self):
10071005

10081006
await created_db.delete_container(none_coll)
10091007

1008+
async def test_replace_throughput_offer_with_int(self):
1009+
created_db = self.database_for_test
1010+
collection = created_db.get_container_client(self.configs.TEST_MULTI_PARTITION_CONTAINER_ID)
1011+
1012+
new_throughput = ThroughputProperties(offer_throughput=2500)
1013+
await collection.replace_throughput(new_throughput.offer_throughput)
1014+
1015+
retrieve_throughput = await collection.get_throughput()
1016+
assert getattr(retrieve_throughput, "offer_throughput") == getattr(new_throughput, "offer_throughput")
1017+
1018+
async def test_replace_throughput_offer_with_object(self):
1019+
created_db = self.database_for_test
1020+
collection = created_db.get_container_client(self.configs.TEST_MULTI_PARTITION_CONTAINER_ID)
1021+
1022+
new_throughput = ThroughputProperties(offer_throughput=2500)
1023+
await collection.replace_throughput(new_throughput)
1024+
1025+
retrieve_throughput = await collection.get_throughput()
1026+
assert getattr(retrieve_throughput, "offer_throughput") == getattr(new_throughput, "offer_throughput")
1027+
1028+
async def test_negative_replace_throughput_with_all_configs_set(self):
1029+
created_db = self.database_for_test
1030+
collection = created_db.get_container_client(self.configs.TEST_MULTI_PARTITION_CONTAINER_ID)
1031+
1032+
new_throughput = ThroughputProperties(offer_throughput=2500, auto_scale_max_throughput=4000, auto_scale_increment_percent=5)
1033+
1034+
with pytest.raises(KeyError):
1035+
await collection.replace_throughput(new_throughput)
1036+
10101037
async def _mock_execute_function(self, function, *args, **kwargs):
10111038
if HttpHeaders.PartitionKey in args[4].headers:
10121039
self.last_headers.append(args[4].headers[HttpHeaders.PartitionKey])

0 commit comments

Comments
 (0)