Skip to content

Commit 4cbae3d

Browse files
simorenohannatischgahl-levytjprescott
authored
[Cosmos] Fix failing unit tests (Azure#24287)
* initial commit * Client Constructor (Azure#20310) * Removed some stuff * Looking at constructors * Updated request * Added client close * working client creation Co-authored-by: simorenoh <[email protected]> * read database database read works, but ignored exception is returned: Fatal error on SSL transport NoneType has no attribute 'send' (_loop._proactor.send) RuntimeError: Event loop is closed Unclosed connector/ connection * Update simon_testfile.py * with coroutine Added methods needed to use async with when initializing client, but logs output "Exception ignored... Runtime Error: Event loop is closed" * Update simon_testfile.py * small changes * async with returns no exceptions * async read container * async item read * cleaning up * create item/ database methods * item delete working * docs replace functionality missing upsert and other resources * upsert functionality missing read_all_items and both query methods for container class * missing query methods * CRUD for udf, sproc, triggers * initial query logic + container methods * missing some execution logic and tests * oops * fully working queries * small fix to query_items() also fixed README and added examples_async * Update _cosmos_client_connection_async.py * Update _cosmos_client_connection.py * documentation update * updated MIT dates and get_user_client() description * Update CHANGELOG.md * Delete simon_testfile.py * leftover retry utility * Update README.md * docs and removed six package * changes based on comments still missing discussion resolution on SSL verification and tests for async functionality under test module (apart from samples which are basically end to end tests) * small change in type hints * updated readme * fixes based on conversations * added missing type comments * update changelog for ci pipeline * added typehints, moved params into keywords, added decorators, made _connection_policy private * changes based on sync with central sdk * remove is_system_key from scripts (only used in execute_sproc) is_system_key verifies that an empty partition key is properly dealt with if ['partitionKey']['systemKey'] exists in the container options - however, we do not allow containers to be created with empty partition key values in the python sdk, so the functionality is needless * Revert "remove is_system_key from scripts (only used in execute_sproc)" Reverting last commit, will find way to init is_system_key for now * async script proxy using composition * pylint * capitalized constants * Apply suggestions from code review Clarifying comments for README Co-authored-by: Gahl Levy <[email protected]> * closing python code snippet * last doc updates * Update sdk/cosmos/azure-cosmos/CHANGELOG.md Co-authored-by: Simon Moreno <[email protected]> * version update * cosmos updates for release * fix connection string comma * Update CHANGELOG.md * fixing extra await keyword in sample * Update CHANGELOG.md * Update CHANGELOG.md * first round of fixes * Update test_config.py * round 2 * raising failed result in get_Database_accounts * small changes * more small fixes * Update test_query.py * Update test_query.py * Update _global_endpoint_manager.py Co-authored-by: annatisch <[email protected]> Co-authored-by: Gahl Levy <[email protected]> Co-authored-by: Travis Prescott <[email protected]>
1 parent e649b49 commit 4cbae3d

27 files changed

+546
-494
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def IsItemContainerLink(link): # pylint: disable=too-many-return-statements
491491

492492

493493
def GetItemContainerInfo(self_link, alt_content_path, id_from_response):
494-
"""Given the self link and alt_content_path from the reponse header and
494+
"""Given the self link and alt_content_path from the response header and
495495
result extract the collection name and collection id.
496496
497497
Every response header has an alt-content-path that is the owner's path in

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
from io import StringIO
2828

2929
import azure.cosmos.cosmos_client as cosmos_client
30-
from azure.cosmos import exceptions
31-
from azure.identity import ClientSecretCredential
32-
from azure.core import exceptions
30+
from azure.cosmos import exceptions, PartitionKey
3331
from azure.core.credentials import AccessToken
3432
import test_config
3533

@@ -114,19 +112,10 @@ class AadTest(unittest.TestCase):
114112
@classmethod
115113
def setUpClass(cls):
116114
cls.client = cosmos_client.CosmosClient(cls.host, cls.masterKey)
117-
cls.database = test_config._test_config.create_database_if_not_exist(cls.client)
118-
cls.container = test_config._test_config.create_collection_if_not_exist_no_custom_throughput(cls.client)
119-
120-
def test_wrong_credentials(self):
121-
wrong_aad_credentials = ClientSecretCredential(
122-
"wrong_tenant_id",
123-
"wrong_client_id",
124-
"wrong_client_secret")
125-
126-
try:
127-
cosmos_client.CosmosClient(self.host, wrong_aad_credentials)
128-
except exceptions.ClientAuthenticationError as e:
129-
print("Client successfully failed to authenticate with message: {}".format(e.message))
115+
cls.database = cls.client.create_database_if_not_exists(test_config._test_config.TEST_DATABASE_ID)
116+
cls.container = cls.database.create_container_if_not_exists(
117+
id=test_config._test_config.TEST_COLLECTION_SINGLE_PARTITION_ID,
118+
partition_key=PartitionKey(path="/id"))
130119

131120
def test_emulator_aad_credentials(self):
132121
if self.host != 'https://localhost:8081/':

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@
3838
class TestClientUserAgent(unittest.TestCase):
3939

4040
async def test_client_user_agent(self):
41-
client_sync = sync_client.CosmosClient(url=_test_config.host, credential=_test_config.masterKey)
42-
client_async = async_client.CosmosClient(url=_test_config.host, credential=_test_config.masterKey)
41+
async with async_client.CosmosClient(url=_test_config.host, credential=_test_config.masterKey) as client_async:
42+
client_sync = sync_client.CosmosClient(url=_test_config.host, credential=_test_config.masterKey)
4343

44-
self.assertTrue(client_sync.client_connection._user_agent.startswith("azsdk-python-cosmos/"))
45-
self.assertTrue(client_async.client_connection._user_agent.startswith("azsdk-python-cosmos-async/"))
46-
self.assertTrue(client_async.client_connection._user_agent != client_sync.client_connection._user_agent)
47-
48-
await client_async.close()
44+
self.assertTrue(client_sync.client_connection._user_agent.startswith("azsdk-python-cosmos/"))
45+
self.assertTrue(client_async.client_connection._user_agent.startswith("azsdk-python-cosmos-async/"))
46+
self.assertTrue(client_async.client_connection._user_agent != client_sync.client_connection._user_agent)
4947

5048

5149
if __name__ == "__main__":

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

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ class _test_config(object):
4545
connectionPolicy = documents.ConnectionPolicy()
4646
connectionPolicy.DisableSSLVerification = True
4747

48-
global_host = '[YOUR_GLOBAL_ENDPOINT_HERE]'
49-
write_location_host = '[YOUR_WRITE_ENDPOINT_HERE]'
50-
read_location_host = '[YOUR_READ_ENDPOINT_HERE]'
51-
read_location2_host = '[YOUR_READ_ENDPOINT2_HERE]'
52-
global_masterKey = '[YOUR_KEY_HERE]'
48+
global_host = os.getenv('GLOBAL_ACCOUNT_HOST', host)
49+
write_location_host = os.getenv('WRITE_LOCATION_HOST', host)
50+
read_location_host = os.getenv('READ_LOCATION_HOST', host)
51+
read_location2_host = os.getenv('READ_LOCATION_HOST2', host)
52+
global_masterKey = os.getenv('GLOBAL_ACCOUNT_KEY', masterKey)
5353

54-
write_location = '[YOUR_WRITE_LOCATION_HERE]'
55-
read_location = '[YOUR_READ_LOCATION_HERE]'
56-
read_location2 = '[YOUR_READ_LOCATION2_HERE]'
54+
write_location = os.getenv('WRITE_LOCATION', host)
55+
read_location = os.getenv('READ_LOCATION', host)
56+
read_location2 = os.getenv('READ_LOCATION2', host)
5757

5858
THROUGHPUT_FOR_5_PARTITIONS = 30000
5959
THROUGHPUT_FOR_1_PARTITION = 400
@@ -84,16 +84,6 @@ def create_database_if_not_exist(cls, client):
8484
cls.IS_MULTIMASTER_ENABLED = client.get_database_account()._EnableMultipleWritableLocations
8585
return cls.TEST_DATABASE
8686

87-
@classmethod
88-
def create_database_if_not_exist_with_throughput(cls, client, throughput):
89-
# type: (CosmosClient) -> Database
90-
if cls.TEST_DATABASE is not None:
91-
return cls.TEST_DATABASE
92-
cls.try_delete_database(client)
93-
cls.TEST_DATABASE = client.create_database(id=cls.TEST_THROUGHPUT_DATABASE_ID, offer_throughput=throughput)
94-
cls.IS_MULTIMASTER_ENABLED = client.get_database_account()._EnableMultipleWritableLocations
95-
return cls.TEST_DATABASE
96-
9787
@classmethod
9888
def try_delete_database(cls, client):
9989
# type: (CosmosClient) -> None
@@ -131,17 +121,6 @@ def create_multi_partition_collection_with_custom_pk_if_not_exist(cls, client):
131121
cls.remove_all_documents(cls.TEST_COLLECTION_MULTI_PARTITION_WITH_CUSTOM_PK, True)
132122
return cls.TEST_COLLECTION_MULTI_PARTITION_WITH_CUSTOM_PK
133123

134-
@classmethod
135-
def create_collection_if_not_exist_no_custom_throughput(cls, client):
136-
# type: (CosmosClient) -> Container
137-
database = cls.create_database_if_not_exist(client)
138-
collection_id = cls.TEST_COLLECTION_SINGLE_PARTITION_ID
139-
140-
document_collection = database.create_container_if_not_exists(
141-
id=collection_id,
142-
partition_key=PartitionKey(path="/id"))
143-
return document_collection
144-
145124
@classmethod
146125
def create_collection_with_required_throughput(cls, client, throughput, use_custom_partition_key):
147126
# type: (CosmosClient, int, boolean) -> Container

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ def setUpClass(cls):
112112
cls.client = cosmos_client.CosmosClient(cls.host, cls.masterKey, connection_policy=cls.connectionPolicy)
113113
cls.databaseForTest = cls.configs.create_database_if_not_exist(cls.client)
114114

115-
def setUp(self):
116-
self.client = cosmos_client.CosmosClient(self.host, self.masterKey, "Session",
117-
connection_policy=self.connectionPolicy)
115+
118116
def test_database_crud(self):
119117
# read databases.
120118
databases = list(self.client.list_databases())
@@ -1907,33 +1905,10 @@ def test_client_request_timeout_when_connection_retry_configuration_specified(se
19071905
cosmos_client.CosmosClient(CRUDTests.host, CRUDTests.masterKey, "Session", connection_policy=connection_policy)
19081906

19091907
def test_client_connection_retry_configuration(self):
1910-
total_time_for_two_retries = self.initialize_client_with_connection_urllib_retry_config(2)
1911-
total_time_for_three_retries = self.initialize_client_with_connection_urllib_retry_config(3)
1912-
self.assertGreater(total_time_for_three_retries, total_time_for_two_retries)
1913-
19141908
total_time_for_two_retries = self.initialize_client_with_connection_core_retry_config(2)
19151909
total_time_for_three_retries = self.initialize_client_with_connection_core_retry_config(3)
19161910
self.assertGreater(total_time_for_three_retries, total_time_for_two_retries)
19171911

1918-
def initialize_client_with_connection_urllib_retry_config(self, retries):
1919-
retry_policy = Retry(
1920-
total=retries,
1921-
read=retries,
1922-
connect=retries,
1923-
backoff_factor=0.3,
1924-
status_forcelist=(500, 502, 504)
1925-
)
1926-
start_time = time.time()
1927-
try:
1928-
cosmos_client.CosmosClient(
1929-
"https://localhost:9999",
1930-
CRUDTests.masterKey,
1931-
"Session",
1932-
connection_retry_policy=retry_policy)
1933-
self.fail()
1934-
except AzureError as e:
1935-
end_time = time.time()
1936-
return end_time - start_time
19371912

19381913
def initialize_client_with_connection_core_retry_config(self, retries):
19391914
start_time = time.time()

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
#SOFTWARE.
2121

2222
import unittest
23-
import uuid
2423
import pytest
25-
import azure.cosmos.documents as documents
2624
import azure.cosmos.cosmos_client as cosmos_client
2725
import test_config
2826
import os

0 commit comments

Comments
 (0)