Skip to content

Commit

Permalink
[Cosmos] Fix failing unit tests (#24287)
Browse files Browse the repository at this point in the history
* initial commit

* Client Constructor (#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]>
  • Loading branch information
4 people authored May 13, 2022
1 parent acd6945 commit 1bd414e
Show file tree
Hide file tree
Showing 27 changed files with 546 additions and 494 deletions.
2 changes: 1 addition & 1 deletion sdk/cosmos/azure-cosmos/azure/cosmos/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def IsItemContainerLink(link): # pylint: disable=too-many-return-statements


def GetItemContainerInfo(self_link, alt_content_path, id_from_response):
"""Given the self link and alt_content_path from the reponse header and
"""Given the self link and alt_content_path from the response header and
result extract the collection name and collection id.
Every response header has an alt-content-path that is the owner's path in
Expand Down
21 changes: 5 additions & 16 deletions sdk/cosmos/azure-cosmos/test/test_aad.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
from io import StringIO

import azure.cosmos.cosmos_client as cosmos_client
from azure.cosmos import exceptions
from azure.identity import ClientSecretCredential
from azure.core import exceptions
from azure.cosmos import exceptions, PartitionKey
from azure.core.credentials import AccessToken
import test_config

Expand Down Expand Up @@ -114,19 +112,10 @@ class AadTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.client = cosmos_client.CosmosClient(cls.host, cls.masterKey)
cls.database = test_config._test_config.create_database_if_not_exist(cls.client)
cls.container = test_config._test_config.create_collection_if_not_exist_no_custom_throughput(cls.client)

def test_wrong_credentials(self):
wrong_aad_credentials = ClientSecretCredential(
"wrong_tenant_id",
"wrong_client_id",
"wrong_client_secret")

try:
cosmos_client.CosmosClient(self.host, wrong_aad_credentials)
except exceptions.ClientAuthenticationError as e:
print("Client successfully failed to authenticate with message: {}".format(e.message))
cls.database = cls.client.create_database_if_not_exists(test_config._test_config.TEST_DATABASE_ID)
cls.container = cls.database.create_container_if_not_exists(
id=test_config._test_config.TEST_COLLECTION_SINGLE_PARTITION_ID,
partition_key=PartitionKey(path="/id"))

def test_emulator_aad_credentials(self):
if self.host != 'https://localhost:8081/':
Expand Down
12 changes: 5 additions & 7 deletions sdk/cosmos/azure-cosmos/test/test_client_user_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@
class TestClientUserAgent(unittest.TestCase):

async def test_client_user_agent(self):
client_sync = sync_client.CosmosClient(url=_test_config.host, credential=_test_config.masterKey)
client_async = async_client.CosmosClient(url=_test_config.host, credential=_test_config.masterKey)
async with async_client.CosmosClient(url=_test_config.host, credential=_test_config.masterKey) as client_async:
client_sync = sync_client.CosmosClient(url=_test_config.host, credential=_test_config.masterKey)

self.assertTrue(client_sync.client_connection._user_agent.startswith("azsdk-python-cosmos/"))
self.assertTrue(client_async.client_connection._user_agent.startswith("azsdk-python-cosmos-async/"))
self.assertTrue(client_async.client_connection._user_agent != client_sync.client_connection._user_agent)

await client_async.close()
self.assertTrue(client_sync.client_connection._user_agent.startswith("azsdk-python-cosmos/"))
self.assertTrue(client_async.client_connection._user_agent.startswith("azsdk-python-cosmos-async/"))
self.assertTrue(client_async.client_connection._user_agent != client_sync.client_connection._user_agent)


if __name__ == "__main__":
Expand Down
37 changes: 8 additions & 29 deletions sdk/cosmos/azure-cosmos/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class _test_config(object):
connectionPolicy = documents.ConnectionPolicy()
connectionPolicy.DisableSSLVerification = True

global_host = '[YOUR_GLOBAL_ENDPOINT_HERE]'
write_location_host = '[YOUR_WRITE_ENDPOINT_HERE]'
read_location_host = '[YOUR_READ_ENDPOINT_HERE]'
read_location2_host = '[YOUR_READ_ENDPOINT2_HERE]'
global_masterKey = '[YOUR_KEY_HERE]'
global_host = os.getenv('GLOBAL_ACCOUNT_HOST', host)
write_location_host = os.getenv('WRITE_LOCATION_HOST', host)
read_location_host = os.getenv('READ_LOCATION_HOST', host)
read_location2_host = os.getenv('READ_LOCATION_HOST2', host)
global_masterKey = os.getenv('GLOBAL_ACCOUNT_KEY', masterKey)

write_location = '[YOUR_WRITE_LOCATION_HERE]'
read_location = '[YOUR_READ_LOCATION_HERE]'
read_location2 = '[YOUR_READ_LOCATION2_HERE]'
write_location = os.getenv('WRITE_LOCATION', host)
read_location = os.getenv('READ_LOCATION', host)
read_location2 = os.getenv('READ_LOCATION2', host)

THROUGHPUT_FOR_5_PARTITIONS = 30000
THROUGHPUT_FOR_1_PARTITION = 400
Expand Down Expand Up @@ -84,16 +84,6 @@ def create_database_if_not_exist(cls, client):
cls.IS_MULTIMASTER_ENABLED = client.get_database_account()._EnableMultipleWritableLocations
return cls.TEST_DATABASE

@classmethod
def create_database_if_not_exist_with_throughput(cls, client, throughput):
# type: (CosmosClient) -> Database
if cls.TEST_DATABASE is not None:
return cls.TEST_DATABASE
cls.try_delete_database(client)
cls.TEST_DATABASE = client.create_database(id=cls.TEST_THROUGHPUT_DATABASE_ID, offer_throughput=throughput)
cls.IS_MULTIMASTER_ENABLED = client.get_database_account()._EnableMultipleWritableLocations
return cls.TEST_DATABASE

@classmethod
def try_delete_database(cls, client):
# type: (CosmosClient) -> None
Expand Down Expand Up @@ -131,17 +121,6 @@ def create_multi_partition_collection_with_custom_pk_if_not_exist(cls, client):
cls.remove_all_documents(cls.TEST_COLLECTION_MULTI_PARTITION_WITH_CUSTOM_PK, True)
return cls.TEST_COLLECTION_MULTI_PARTITION_WITH_CUSTOM_PK

@classmethod
def create_collection_if_not_exist_no_custom_throughput(cls, client):
# type: (CosmosClient) -> Container
database = cls.create_database_if_not_exist(client)
collection_id = cls.TEST_COLLECTION_SINGLE_PARTITION_ID

document_collection = database.create_container_if_not_exists(
id=collection_id,
partition_key=PartitionKey(path="/id"))
return document_collection

@classmethod
def create_collection_with_required_throughput(cls, client, throughput, use_custom_partition_key):
# type: (CosmosClient, int, boolean) -> Container
Expand Down
27 changes: 1 addition & 26 deletions sdk/cosmos/azure-cosmos/test/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ def setUpClass(cls):
cls.client = cosmos_client.CosmosClient(cls.host, cls.masterKey, connection_policy=cls.connectionPolicy)
cls.databaseForTest = cls.configs.create_database_if_not_exist(cls.client)

def setUp(self):
self.client = cosmos_client.CosmosClient(self.host, self.masterKey, "Session",
connection_policy=self.connectionPolicy)

def test_database_crud(self):
# read databases.
databases = list(self.client.list_databases())
Expand Down Expand Up @@ -1907,33 +1905,10 @@ def test_client_request_timeout_when_connection_retry_configuration_specified(se
cosmos_client.CosmosClient(CRUDTests.host, CRUDTests.masterKey, "Session", connection_policy=connection_policy)

def test_client_connection_retry_configuration(self):
total_time_for_two_retries = self.initialize_client_with_connection_urllib_retry_config(2)
total_time_for_three_retries = self.initialize_client_with_connection_urllib_retry_config(3)
self.assertGreater(total_time_for_three_retries, total_time_for_two_retries)

total_time_for_two_retries = self.initialize_client_with_connection_core_retry_config(2)
total_time_for_three_retries = self.initialize_client_with_connection_core_retry_config(3)
self.assertGreater(total_time_for_three_retries, total_time_for_two_retries)

def initialize_client_with_connection_urllib_retry_config(self, retries):
retry_policy = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=0.3,
status_forcelist=(500, 502, 504)
)
start_time = time.time()
try:
cosmos_client.CosmosClient(
"https://localhost:9999",
CRUDTests.masterKey,
"Session",
connection_retry_policy=retry_policy)
self.fail()
except AzureError as e:
end_time = time.time()
return end_time - start_time

def initialize_client_with_connection_core_retry_config(self, retries):
start_time = time.time()
Expand Down
2 changes: 0 additions & 2 deletions sdk/cosmos/azure-cosmos/test/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
#SOFTWARE.

import unittest
import uuid
import pytest
import azure.cosmos.documents as documents
import azure.cosmos.cosmos_client as cosmos_client
import test_config
import os
Expand Down
Loading

0 comments on commit 1bd414e

Please sign in to comment.