def test_aad_query_list_tables(self, tables_storage_account_name): try: account_url = self.account_url(tables_storage_account_name, "table") ts = TableServiceClient(credential=self.get_token_credential(), endpoint=account_url) table_name1 = self._get_table_reference(prefix="table1") table_name2 = self._get_table_reference(prefix="table2") table_name3 = self._get_table_reference(prefix="table3") table_name4 = self._get_table_reference(prefix="table4") ts.create_table(table_name1) ts.create_table(table_name2) ts.create_table(table_name3) ts.create_table(table_name4) count = 0 for table in ts.list_tables(): count += 1 assert count == 4 query_filter = "TableName eq '{}'".format(table_name2) count = 0 for table in ts.query_tables(query_filter): count += 1 assert table.name == table_name2 assert count == 1 finally: for table in ts.list_tables(): ts.delete_table(table.name)
def test_locale(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 = (self._get_table_reference()) init_locale = locale.getlocale() if os.name == "nt": culture = "Spanish_Spain" elif os.name == 'posix': culture = 'es_ES.UTF-8' else: culture = 'es_ES.utf8' locale.setlocale(locale.LC_ALL, culture) e = None # Act ts.create_table(table) resp = ts.list_tables() e = sys.exc_info()[0] # Assert assert e is None ts.delete_table(table) locale.setlocale(locale.LC_ALL, init_locale[0] or 'en_US') if self.is_live: sleep(SLEEP_DELAY)
def test_locale(self, resource_group, location, storage_account, storage_account_key): # Arrange ts = TableServiceClient(self.account_url(storage_account, "table"), storage_account_key) table = (self._get_table_reference()) init_locale = locale.getlocale() if os.name == "nt": culture = "Spanish_Spain" elif os.name == 'posix': culture = 'es_ES.UTF-8' else: culture = 'es_ES.utf8' locale.setlocale(locale.LC_ALL, culture) e = None # Act ts.create_table(table) resp = ts.list_tables() e = sys.exc_info()[0] # Assert self.assertIsNone(e) ts.delete_table(table) locale.setlocale(locale.LC_ALL, init_locale[0] or 'en_US')
def test_query_tables_per_page(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 = "mytable" for i in range(5): ts.create_table(table_name + str(i)) query_filter = "TableName eq 'mytable0' or TableName eq 'mytable1' or TableName eq 'mytable2'" table_count = 0 page_count = 0 for table_page in ts.query_tables(query_filter, results_per_page=2).by_page(): temp_count = 0 for table in table_page: temp_count += 1 assert temp_count <= 2 page_count += 1 table_count += temp_count assert page_count == 2 assert table_count == 3 self._delete_all_tables(ts) if self.is_live: sleep(SLEEP_DELAY)
def test_query_tables_per_page(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange account_url = self.account_url(tables_storage_account_name, "table") ts = TableServiceClient(credential=tables_primary_storage_account_key, endpoint=account_url) table_name = "mytable" for i in range(5): ts.create_table(table_name + str(i)) query_filter = "TableName eq 'mytable0' or TableName eq 'mytable1' or TableName eq 'mytable2'" table_count = 0 page_count = 0 for table_page in ts.query_tables(query_filter, results_per_page=2).by_page(): temp_count = 0 for table in table_page: temp_count += 1 assert temp_count <= 2 page_count += 1 table_count += temp_count assert page_count == 2 assert table_count == 3 self._delete_all_tables(ts)
def test_unicode_create_table_unicode_name(self): # Arrange url = self.account_url(self.tables_cosmos_account_name, "cosmos") ts = TableServiceClient(url, self.credential) table_name = u'啊齄丂狛狜' # Act with pytest.raises(ValueError): ts.create_table(table_name)
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.credential) 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_create_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.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_unicode_create_table_unicode_name(self, resource_group, location, storage_account, storage_account_key): # Arrange url = self.account_url(storage_account, "table") if 'cosmos' in url: pytest.skip("Cosmos URLs support unicode table names") ts = TableServiceClient(url, storage_account_key) table_name = u'啊齄丂狛狜' # Act with self.assertRaises(HttpResponseError): # not supported - table name must be alphanumeric, lowercase ts.create_table(table_name)
def test_unicode_create_table_unicode_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 = u'啊齄丂狛狜' 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 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_if_exists(self): from azure.data.tables import TableServiceClient from azure.core.exceptions import ResourceExistsError # create table table_service_client = TableServiceClient(account_url=self.account_url, credential=self.access_key) table_service_client.create_table(table_name=self.table_name) try: # try to create existing table, ResourceExistsError will be thrown table_service_client.create_table(table_name=self.table_name) except ResourceExistsError: print("TableExists")
def test_delete_table_invalid_name(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) 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) if self.is_live: sleep(SLEEP_DELAY)
def test_unicode_create_table_unicode_name(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange url = self.account_url(cosmos_account, "cosmos") if 'cosmos' in url: pytest.skip("Cosmos URLs support unicode table names") ts = TableServiceClient(url, cosmos_account_key) table_name = u'啊齄丂狛狜' # Act with self.assertRaises(HttpResponseError): ts.create_table(table_name) if self.is_live: sleep(SLEEP_DELAY)
def test_unicode_create_table_unicode_name(self, resource_group, location, storage_account, storage_account_key): # Arrange url = self.account_url(storage_account, "table") if 'cosmos' in url: pytest.skip("Cosmos URLs do notsupport unicode table names") ts = TableServiceClient(url, storage_account_key) table_name = u'啊齄丂狛狜' # Act with self.assertRaises(ValueError) as excinfo: ts.create_table(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_unicode_create_table_unicode_name(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange url = self.account_url(cosmos_account, "cosmos") ts = TableServiceClient(url, cosmos_account_key) table_name = u'啊齄丂狛狜' # Act with pytest.raises(HttpResponseError): ts.create_table(table_name) if self.is_live: sleep(SLEEP_DELAY)
def test_create_properties(self, tables_storage_account_name, tables_primary_storage_account_key): # # Arrange 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() # Act created = ts.create_table(table_name) # Assert assert created.table_name == table_name properties = ts.get_service_properties() ts.set_service_properties(analytics_logging=TableAnalyticsLogging(write=True)) # have to wait for return to service p = ts.get_service_properties() # have to wait for return to service ts.set_service_properties( minute_metrics=TableMetrics( enabled=True, include_apis=True, retention_policy=TableRetentionPolicy(enabled=True, days=5) ) ) ps = ts.get_service_properties() ts.delete_table(table_name)
def test_aad_create_table(self, tables_storage_account_name): try: account_url = self.account_url(tables_storage_account_name, "table") ts = TableServiceClient(credential=self.get_token_credential(), endpoint=account_url) table_name = self._get_table_reference() ts.create_table(table_name) if table_name not in [t.name for t in ts.list_tables()]: raise AssertionError("Table could not be found") ts.delete_table(table_name) if table_name in [t.name for t in ts.list_tables()]: raise AssertionError("Table was not deleted") finally: ts.delete_table(table_name)
def test_create_table_fail_on_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() # btable_client = ts.get_table_client(table_name) # Act created = ts.create_table(table_name) with self.assertRaises(ResourceExistsError): ts.create_table(table_name) # Assert self.assertTrue(created) # existing = list(ts.query_tables(query_options=QueryOptions(filter="TableName eq '{}'".format(table_name)))) # self.assertEqual(existing[0], [table_name]) ts.delete_table(table_name)
def test_create_table_fail_on_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 created = ts.create_table(table_name) with self.assertRaises(ResourceExistsError): ts.create_table(table_name) name_filter = "TableName eq '{}'".format(table_name) existing = list(ts.query_tables(filter=name_filter)) # Assert self.assertIsNotNone(created) ts.delete_table(table_name)
def test_create_table_fail_on_exist(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange ts = TableServiceClient(self.account_url(cosmos_account, "cosmos"), cosmos_account_key) table_name = self._get_table_reference() # Act created = ts.create_table(table_name) with self.assertRaises(ResourceExistsError): ts.create_table(table_name) # Assert self.assertTrue(created) ts.delete_table(table_name) if self.is_live: sleep(SLEEP_DELAY)
def test_create_properties(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() # Act created = ts.create_table(table_name) # Assert assert created.table_name == table_name # properties = ts.get_service_properties() ts.set_service_properties(analytics_logging=TableAnalyticsLogging( write=True)) p = ts.get_service_properties() ts.set_service_properties(minute_metrics=Metrics( enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5))) ps = ts.get_service_properties() ts.delete_table(table_name) if self.is_live: sleep(SLEEP_DELAY)
def test_create_properties(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 created = ts.create_table(table_name) # Assert assert created.table_name == table_name properties = ts.get_service_properties() print(properties) ts.set_service_properties(analytics_logging=TableAnalyticsLogging( write=True)) # have to wait for return to service p = ts.get_service_properties() # have to wait for return to service ts.set_service_properties(minute_metrics=Metrics( enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5))) ps = ts.get_service_properties() print(ps) print(p) ts.delete_table(table_name)
def test_create_table_fail_on_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() # Act created = ts.create_table(table_name) with pytest.raises(ResourceExistsError): ts.create_table(table_name) # Assert assert created ts.delete_table(table_name) if self.is_live: sleep(SLEEP_DELAY)
def test_create_table_fail_on_exist(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange 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() # Act created = ts.create_table(table_name) with pytest.raises(ResourceExistsError): ts.create_table(table_name) name_filter = "TableName eq '{}'".format(table_name) existing = list(ts.query_tables(name_filter)) # Assert assert created is not None ts.delete_table(table_name)
def create_table(client: TableServiceClient, name: str): Validation.validateTableName(name) try: table_item = client.create_table(table_name=name) print('Created table {}!'.format(table_item.table_name)) except ResourceExistsError: raise TableAlreadyExistsError()
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_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 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")