def test_table_service_stats_f(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): tsc = TableServiceClient( self.account_url(tables_cosmos_account_name, "cosmos"), tables_primary_cosmos_account_key) stats = tsc.get_service_stats( raw_response_hook=self.override_response_body_with_live_status) self._assert_stats_default(stats) self.sleep(SLEEP_DELAY)
def test_retention_too_long(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange tsc = TableServiceClient(self.account_url(tables_storage_account_name, "table"), tables_primary_storage_account_key) minute_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=366)) # Assert pytest.raises(HttpResponseError, tsc.set_service_properties, None, None, minute_metrics)
def test_azurite_url(self): account_url = "https://127.0.0.1:10002/my_account" tsc = TableServiceClient( account_url, credential=self.tables_primary_storage_account_key) assert tsc.account_name == "my_account" assert tsc.url == "https://127.0.0.1:10002/my_account" assert tsc.location_mode == "primary" assert tsc.credential.account_key == self.tables_primary_storage_account_key assert tsc.credential.account_name == "my_account"
def test_table_service_stats_when_unavailable(self, resource_group, location, storage_account, storage_account_key): # Arrange tsc = TableServiceClient(self.account_url(storage_account, "table"), storage_account_key) # Act stats = tsc.get_service_stats( raw_response_hook=self.override_response_body_with_unavailable_status) # Assert self._assert_stats_unavailable(stats)
def delete_table(self): from azure.data.tables import TableServiceClient from azure.core.exceptions import ResourceNotFoundError table_service_client = TableServiceClient(account_url=self.account_url, credential=self.access_key) try: table_service_client.delete_table(table_name=self.table_name) except ResourceNotFoundError: print("TableNotFound")
def test_retention_too_long(self, resource_group, location, storage_account, storage_account_key): # Arrange tsc = TableServiceClient(self.account_url(storage_account, "table"), storage_account_key) minute_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=366)) # Assert self.assertRaises(HttpResponseError, tsc.set_service_properties, None, None, minute_metrics)
def test_delete_table_invalid_name(self, resource_group, location, storage_account, storage_account_key): # Arrange ts = TableServiceClient(self.account_url(storage_account, "table"), storage_account_key) invalid_table_name = "my_table" with pytest.raises(ValueError) as excinfo: ts.create_table(invalid_table_name) assert "Table names must be alphanumeric, cannot begin with a number, and must be between 3-63 characters long.""" in str( excinfo)
def test_delete_table_invalid_name(self): # Arrange ts = TableServiceClient(self.account_url(self.tables_cosmos_account_name, "cosmos"), self.tables_primary_cosmos_account_key) invalid_table_name = "my_table" with pytest.raises(ValueError) as excinfo: ts.create_table(invalid_table_name) assert "Table names must be alphanumeric, cannot begin with a number, and must be between 3-63 characters long.""" in str( excinfo)
def test_too_many_cors_rules(self, resource_group, location, storage_account, storage_account_key): # Arrange tsc = TableServiceClient(self.account_url(storage_account, "table"), storage_account_key) cors = [] for i in range(0, 6): cors.append(CorsRule(['www.xyz.com'], ['GET'])) # Assert self.assertRaises(HttpResponseError, tsc.set_service_properties, None, None, None, cors)
def test_too_many_cors_rules(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange tsc = TableServiceClient(self.account_url(tables_storage_account_name, "table"), tables_primary_storage_account_key) cors = [] for i in range(0, 6): cors.append(CorsRule(['www.xyz.com'], ['GET'])) # Assert pytest.raises(HttpResponseError, tsc.set_service_properties, None, None, None, cors)
def test_table_service_stats_when_unavailable( self, tables_cosmos_account_name, tables_primary_cosmos_account_key): tsc = TableServiceClient( self.account_url(tables_cosmos_account_name, "cosmos"), tables_primary_cosmos_account_key) stats = tsc.get_service_stats( raw_response_hook=self. override_response_body_with_unavailable_status) self._assert_stats_unavailable(stats)
def test_account_sas(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange url = self.account_url(storage_account, "table") if 'cosmos' in url: pytest.skip("Cosmos Tables does not yet support sas") tsc = TableServiceClient(url, storage_account_key) table = self._create_table(tsc) try: entity = { 'PartitionKey': u'test', 'RowKey': u'test1', 'text': u'hello', } table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) entity['RowKey'] = u'test2' table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) token = generate_account_sas( storage_account.name, storage_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) # Act service = TableServiceClient( self.account_url(storage_account, "table"), credential=token, ) sas_table = service.get_table_client(table.table_name) entities = list(sas_table.list_entities()) # Assert self.assertEqual(len(entities), 2) self.assertEqual(entities[0].text.value, 'hello') self.assertEqual(entities[1].text.value, 'hello') finally: self._delete_table(table=table, ts=tsc)
def test_create_service_with_custom_account_endpoint_path( self, resource_group, location, cosmos_account, cosmos_account_key): custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token for service_type in SERVICES.items(): conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};TableEndpoint={};'.format( cosmos_account.name, cosmos_account_key, custom_account_url) # Act service = service_type[0].from_connection_string(conn_string, table_name="foo") # Assert self.assertEqual(service.account_name, cosmos_account.name) self.assertEqual(service.credential.account_name, cosmos_account.name) self.assertEqual(service.credential.account_key, cosmos_account_key) self.assertEqual(service._primary_hostname, 'local-machine:11002/custom/account/path') service = TableServiceClient(account_url=custom_account_url) self.assertEqual(service.account_name, None) self.assertEqual(service.credential, None) self.assertEqual(service._primary_hostname, 'local-machine:11002/custom/account/path') # mine doesnt have a question mark at the end self.assertTrue( service.url.startswith( 'http://local-machine:11002/custom/account/path')) service = TableClient(account_url=custom_account_url, table_name="foo") self.assertEqual(service.account_name, None) self.assertEqual(service.table_name, "foo") self.assertEqual(service.credential, None) self.assertEqual(service._primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue( service.url.startswith( 'http://local-machine:11002/custom/account/path')) service = TableClient.from_table_url( "http://local-machine:11002/custom/account/path/foo" + self.sas_token) self.assertEqual(service.account_name, None) self.assertEqual(service.table_name, "foo") self.assertEqual(service.credential, None) self.assertEqual(service._primary_hostname, 'local-machine:11002/custom/account/path') self.assertTrue( service.url.startswith( 'http://local-machine:11002/custom/account/path')) if self.is_live: sleep(SLEEP_DELAY)
def test_account_sas(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange url = self.account_url(tables_cosmos_account_name, "cosmos") tsc = TableServiceClient(url, tables_primary_cosmos_account_key) table = self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) entity['RowKey'] = 'test2' table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) token = generate_account_sas( tables_cosmos_account_name, tables_primary_cosmos_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) # Act service = TableServiceClient( self.account_url(tables_cosmos_account_name, "cosmos"), credential=token, ) sas_table = service.get_table_client(table.table_name) entities = list(sas_table.list_entities()) # Assert assert len(entities) == 2 assert entities[0].text == 'hello' assert entities[1].text == 'hello' finally: self._delete_table(table=table, ts=tsc)
def test_too_many_cors_rules(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): tsc = TableServiceClient(self.account_url(tables_cosmos_account_name, "cosmos"), credential=tables_primary_cosmos_account_key) cors = [] for i in range(0, 6): cors.append(TableCorsRule(['www.xyz.com'], ['GET'])) with pytest.raises(HttpResponseError): tsc.set_service_properties(cors=cors)
def test_create_client_for_cosmos_emulator(self): emulator_credential = AzureNamedKeyCredential( 'localhost', self.tables_primary_cosmos_account_key) emulator_connstr = "DefaultEndpointsProtocol=http;AccountName=localhost;AccountKey={};TableEndpoint=http://localhost:8902/;".format( self.tables_primary_cosmos_account_key) client = TableServiceClient.from_connection_string(emulator_connstr) assert client.url == "http://localhost:8902" assert client.account_name == 'localhost' assert client.credential.named_key.name == 'localhost' assert client.credential.named_key.key == self.tables_primary_cosmos_account_key assert client._cosmos_endpoint assert client.scheme == 'http' client = TableServiceClient("http://localhost:8902/", credential=emulator_credential) assert client.url == "http://localhost:8902" assert client.account_name == 'localhost' assert client.credential.named_key.name == 'localhost' assert client.credential.named_key.key == self.tables_primary_cosmos_account_key assert client._cosmos_endpoint assert client.scheme == 'http' table = TableClient.from_connection_string(emulator_connstr, 'tablename') assert table.url == "http://localhost:8902" assert table.account_name == 'localhost' assert table.table_name == 'tablename' assert table.credential.named_key.name == 'localhost' assert table.credential.named_key.key == self.tables_primary_cosmos_account_key assert table._cosmos_endpoint assert table.scheme == 'http' table = TableClient("http://localhost:8902/", "tablename", credential=emulator_credential) assert table.url == "http://localhost:8902" assert table.account_name == 'localhost' assert table.table_name == 'tablename' assert table.credential.named_key.name == 'localhost' assert table.credential.named_key.key == self.tables_primary_cosmos_account_key assert table._cosmos_endpoint assert table.scheme == 'http' table = TableClient.from_table_url( "http://localhost:8902/Tables('tablename')", credential=emulator_credential) assert table.url == "http://localhost:8902" assert table.account_name == 'localhost' assert table.table_name == 'tablename' assert table.credential.named_key.name == 'localhost' assert table.credential.named_key.key == self.tables_primary_cosmos_account_key assert table._cosmos_endpoint assert table.scheme == 'http'
def _set_up(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): self.ts = TableServiceClient(self.account_url(tables_cosmos_account_name, "cosmos"), tables_primary_cosmos_account_key) self.table_name = self.get_resource_name('uttable') self.table = self.ts.get_table_client(self.table_name) if self.is_live: try: self.ts.create_table(self.table_name) except ResourceExistsError: pass self.test_tables = []
def _set_up(self, storage_account, storage_account_key): self.ts = TableServiceClient(self.account_url(storage_account, "table"), storage_account_key) self.table_name = self.get_resource_name('uttable') self.table = self.ts.get_table_client(self.table_name) if self.is_live: try: self.ts.create_table(self.table_name) except ResourceExistsError: pass self.test_tables = []
def test_table_service_stats_f(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange tsc = TableServiceClient(self.account_url(cosmos_account, "cosmos"), cosmos_account_key) # Act stats = tsc.get_service_stats(raw_response_hook=self.override_response_body_with_live_status) # Assert self._assert_stats_default(stats) if self.is_live: sleep(SLEEP_DELAY)
def test_delete_table_invalid_name(self): # Arrange account_url = self.account_url(self.tables_storage_account_name, "table") tsc = TableServiceClient(account_url, credential=self.credential) invalid_table_name = "my_table" with pytest.raises(ValueError) as excinfo: tsc.delete_table(invalid_table_name) assert "Table names must be alphanumeric, cannot begin with a number, and must be between 3-63 characters long.""" in str( excinfo)
def test_create_table_if_exists(self, resource_group, location, storage_account, storage_account_key): ts = TableServiceClient(self.account_url(storage_account, "table"), storage_account_key) table_name = self._get_table_reference() t0 = ts.create_table(table_name) t1 = ts.create_table_if_not_exists(table_name) self.assertIsNotNone(t0) self.assertIsNotNone(t1) self.assertEqual(t0.table_name, t1.table_name) ts.delete_table(table_name)
def test_create_table_invalid_name(self, tables_storage_account_name, tables_primary_storage_account_key): account_url = self.account_url(tables_storage_account_name, "table") tsc = TableServiceClient(account_url, credential=tables_primary_storage_account_key) invalid_table_name = "my_table" with pytest.raises(ValueError) as excinfo: tsc.create_table(invalid_table_name) assert "Storage table names must be alphanumeric, cannot begin with a number, and must be between 3-63 characters long." "" in str( excinfo)
def authentication_by_shared_key(self): # Instantiate a TableServiceClient using a shared access key # [START auth_from_shared_key] from azure.data.tables import TableServiceClient from azure.core.credentials import AzureNamedKeyCredential credential = AzureNamedKeyCredential(self.account_name, self.access_key) with TableServiceClient(account_url=self.account_url, credential=credential) as table_service: properties = table_service.get_service_properties() print("Shared Key: {}".format(properties))
def test_delete_table_with_existing_table(self, resource_group, location, storage_account, storage_account_key): # Arrange ts = TableServiceClient(self.account_url(storage_account, "table"), storage_account_key) table = self._create_table(ts) # Act # deleted = table.delete_table() deleted = ts.delete_table(table_name=table.table_name) # Assert self.assertIsNone(deleted)
def test_delete_table_with_non_existing_table_fail_not_exist( self, resource_group, location, storage_account, storage_account_key): # Arrange ts = TableServiceClient(self.account_url(storage_account, "table"), storage_account_key) table_name = self._get_table_reference() # Act with self.assertRaises(HttpResponseError): ts.delete_table(table_name)
def test_table_service_stats_f(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange tsc = TableServiceClient( self.account_url(tables_storage_account_name, "table"), tables_primary_storage_account_key) # Act stats = tsc.get_service_stats( raw_response_hook=self.override_response_body_with_live_status) # Assert self._assert_stats_default(stats)
def test_delete_table_with_non_existing_table_fail_not_exist( self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # Arrange ts = TableServiceClient( self.account_url(tables_cosmos_account_name, "cosmos"), tables_primary_cosmos_account_key) table_name = self._get_table_reference() ts.delete_table(table_name) if self.is_live: sleep(SLEEP_DELAY)
def test_create_table_if_exists(self, tables_storage_account_name, tables_primary_storage_account_key): account_url = self.account_url(tables_storage_account_name, "table") ts = TableServiceClient(credential=tables_primary_storage_account_key, endpoint=account_url) table_name = self._get_table_reference() t0 = ts.create_table(table_name) t1 = ts.create_table_if_not_exists(table_name) assert t0 is not None assert t1 is not None assert t0.table_name == t1.table_name ts.delete_table(table_name)
def test_unicode_create_table_unicode_name(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # Arrange url = self.account_url(tables_cosmos_account_name, "cosmos") ts = TableServiceClient(url, tables_primary_cosmos_account_key) table_name = u'啊齄丂狛狜' # Act with pytest.raises(HttpResponseError): ts.create_table(table_name) if self.is_live: sleep(SLEEP_DELAY)
def create_table(self): from azure.data.tables import TableServiceClient from azure.core.exceptions import ResourceExistsError table_service_client = TableServiceClient(account_url=self.account_url, credential=self.access_key) try: table_created = table_service_client.create_table( table_name=self.table_name) print(table_created.table_name) except ResourceExistsError: print("TableExists")