async 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()
        await ts.delete_table(table_name)

        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_create_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:
            await ts.create_table(table_name=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)
    async 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"),
                                 credential=tables_primary_storage_account_key)

        # Act
        stats = await tsc.get_service_stats(
            raw_response_hook=self.override_response_body_with_live_status)
        # Assert
        self._assert_stats_default(stats)
    async 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:
            await 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)
    async 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:
                await self.ts.create_table(self.table_name)
            except ResourceExistsError:
                pass

        self.test_tables = []
Ejemplo n.º 6
0
    async def test_batch_sas_auth(self, tables_cosmos_account_name,
                                  tables_primary_cosmos_account_key):
        set_bodiless_matcher()

        # Arrange
        await self._set_up(tables_cosmos_account_name,
                           tables_primary_cosmos_account_key,
                           url="cosmos")
        try:
            token = self.generate_sas(
                generate_table_sas,
                tables_primary_cosmos_account_key,
                self.table_name,
                permission=TableSasPermissions(add=True,
                                               read=True,
                                               update=True,
                                               delete=True),
                expiry=datetime.utcnow() + timedelta(hours=1),
                start=datetime.utcnow() - timedelta(minutes=1),
            )
            token = AzureSasCredential(token)

            # Act
            service = TableServiceClient(
                self.account_url(tables_cosmos_account_name, "cosmos"),
                credential=token,
            )
            table = service.get_table_client(self.table_name)

            entity = TableEntity()
            entity['PartitionKey'] = 'batch_inserts'
            entity['test'] = EntityProperty(True, EdmType.BOOLEAN)
            entity['test2'] = 'value'
            entity['test3'] = 3
            entity['test4'] = EntityProperty(1234567890, EdmType.INT32)

            batch = []
            transaction_count = 0
            for i in range(10):
                entity['RowKey'] = str(i)
                batch.append(('create', entity.copy()))
                transaction_count += 1
            transaction_result = await table.submit_transaction(batch)

            assert transaction_result is not None

            total_entities = 0
            async for e in table.list_entities():
                total_entities += 1

            assert total_entities == transaction_count
        finally:
            await self._tear_down()
    async def test_user_agent_default_async(self, tables_storage_account_name, tables_primary_storage_account_key):
        service = TableServiceClient(self.account_url(tables_storage_account_name, "table"), credential=tables_primary_storage_account_key)

        def callback(response):
            assert 'User-Agent' in response.http_request.headers
            assert response.http_request.headers['User-Agent'] in "azsdk-python-data-tables/{} Python/{} ({})".format(
                    VERSION,
                    platform.python_version(),
                    platform.platform())

        tables = service.list_tables(raw_response_hook=callback)
        assert tables is not None
    async def _set_up(self, account_name, credential, url="table"):
        account_url = self.account_url(account_name, url)
        self.ts = TableServiceClient(account_url, credential=credential)
        self.table_name = self.get_resource_name("uttable")
        self.table = self.ts.get_table_client(self.table_name)
        if self.is_live:
            try:
                await self.ts.create_table(table_name=self.table_name)
            except ResourceExistsError:
                pass

        self.query_tables = []
Ejemplo n.º 9
0
    async def test_create_client_with_api_version(self):
        url = self.account_url(self.tables_storage_account_name, "table")
        client = TableServiceClient(
            url, credential=self.tables_primary_storage_account_key)
        assert client._client._config.version == "2019-02-02"
        table = client.get_table_client('tablename')
        assert table._client._config.version == "2019-02-02"

        client = TableServiceClient(
            url,
            credential=self.tables_primary_storage_account_key,
            api_version="2019-07-07")
        assert client._client._config.version == "2019-07-07"
        table = client.get_table_client('tablename')
        assert table._client._config.version == "2019-07-07"

        with pytest.raises(ValueError):
            TableServiceClient(
                url,
                credential=self.tables_primary_storage_account_key,
                api_version="foo")
    async def test_create_table_if_exists_new_table(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()

        t = await ts.create_table_if_not_exists(table_name)

        self.assertIsNotNone(t)
        self.assertEqual(t.table_name, table_name)
        await ts.delete_table(table_name)
    async 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 = await tsc.get_service_stats(
            raw_response_hook=self.
            override_response_body_with_unavailable_status)
        self._assert_stats_unavailable(stats)

        self.sleep(SLEEP_DELAY)
    async def test_user_agent_default_async(self, resource_group, location, cosmos_account, cosmos_account_key):
        service = TableServiceClient(self.account_url(cosmos_account, "cosmos"), credential=cosmos_account_key)

        def callback(response):
            assert 'User-Agent' in response.http_request.headers
            assert response.http_request.headers['User-Agent'] == "azsdk-python-storage-table/{} Python/{} ({})".format(
                    VERSION,
                    platform.python_version(),
                    platform.platform())

        tables = service.list_tables(raw_response_hook=callback)
        assert tables is not None
    async def test_create_table(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 = await ts.create_table(table_name=table_name)

        # Assert
        assert created.table_name == table_name

        await ts.delete_table(table_name=table_name)
    async 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 = await self._create_table(ts)

        # Act
        # deleted = table.delete_table()
        deleted = await ts.delete_table(table_name=table.table_name)

        # Assert
        self.assertIsNone(deleted)
    async def test_too_many_cors_rules_async(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
        # Arrange
        tsc = TableServiceClient(self.account_url(tables_cosmos_account_name, "cosmos"), tables_primary_cosmos_account_key)
        cors = []
        for i in range(0, 6):
            cors.append(CorsRule(['www.xyz.com'], ['GET']))

        # Assert
        with pytest.raises(HttpResponseError):
            await tsc.set_service_properties(None, None, None, cors)
        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_too_many_cors_rules_async(self, resource_group, location, cosmos_account, cosmos_account_key):
        # Arrange
        tsc = TableServiceClient(self.account_url(cosmos_account, "cosmos"), cosmos_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)
        if self.is_live:
            sleep(SLEEP_DELAY)
Ejemplo n.º 17
0
    async def test_create_service_with_custom_account_endpoint_path_async(
            self, resource_group, location, storage_account,
            storage_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(
                storage_account.name, storage_account_key, custom_account_url)

            # Act
            service = service_type[0].from_connection_string(conn_string,
                                                             table_name="foo")

            # Assert
            self.assertEqual(service.account_name, storage_account.name)
            self.assertEqual(service.credential.account_name,
                             storage_account.name)
            self.assertEqual(service.credential.account_key,
                             storage_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'))
Ejemplo n.º 18
0
    async 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:
            await 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)
 async 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:
             await self.ts.create_table(self.table_name)
         except ResourceExistsError:
             pass
     self.test_tables = []
    async def test_user_agent_append(self, resource_group, location, cosmos_account, cosmos_account_key):
        # TODO: fix this one
        service = TableServiceClient(self.account_url(cosmos_account, "cosmos"), credential=cosmos_account_key)

        def callback(response):
            assert 'User-Agent' in response.http_request.headers
            assert response.http_request.headers['User-Agent'] == "azsdk-python-storage-tables/{} Python/{} ({}) customer_user_agent".format(
                    VERSION,
                    platform.python_version(),
                    platform.platform())

        custom_headers = {'User-Agent': 'customer_user_agent'}
        tables = service.list_tables(raw_response_hook=callback, headers=custom_headers)
Ejemplo n.º 21
0
    async def test_new_invalid_key(self, tables_storage_account_name, tables_primary_storage_account_key):
        invalid_key = tables_primary_storage_account_key.named_key.key[0:-6] + "==" # cut off a bit from the end to invalidate
        tables_primary_storage_account_key = AzureNamedKeyCredential(tables_storage_account_name, invalid_key)
        credential = AzureNamedKeyCredential(name=tables_storage_account_name, key=tables_primary_storage_account_key.named_key.key)
        self.ts = TableServiceClient(self.account_url(tables_storage_account_name, "table"), credential=credential)
        self.table_name = self.get_resource_name('uttable')
        self.table = self.ts.get_table_client(self.table_name)

        entity = self._create_random_entity_dict('001', 'batch_negative_1')

        batch = [('create', entity)]
        with pytest.raises(ClientAuthenticationError):
            resp = await self.table.submit_transaction(batch)
    async 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 = await ts.create_table(table_name=table_name)
        with self.assertRaises(ResourceExistsError):
            await ts.create_table(table_name=table_name)

        # Assert
        self.assertTrue(created)
        await ts.delete_table(table_name=table_name)
Ejemplo n.º 23
0
    async def test_unicode_create_table_unicode_name(self):
        # Arrange
        account_url = self.account_url(self.tables_storage_account_name, "table")
        tsc = TableServiceClient(account_url, credential=self.tables_primary_storage_account_key)

        table_name = u'啊齄丂狛狜'

        # Act
        with pytest.raises(ValueError) as excinfo:
            await tsc.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)
    async def test_user_agent_append(self, tables_storage_account_name, tables_primary_storage_account_key):
        # TODO: fix this one
        service = TableServiceClient(self.account_url(tables_storage_account_name, "table"), credential=tables_primary_storage_account_key)

        def callback(response):
            assert 'User-Agent' in response.http_request.headers
            assert response.http_request.headers['User-Agent'] == "azsdk-python-data-tables/{} Python/{} ({}) customer_user_agent".format(
                    VERSION,
                    platform.python_version(),
                    platform.platform())

        custom_headers = {'User-Agent': 'customer_user_agent'}
        tables = service.list_tables(raw_response_hook=callback, headers=custom_headers)
Ejemplo n.º 25
0
    async def test_too_many_cors_rules_async(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)
Ejemplo n.º 26
0
    async def test_create_table_unicode_name(
            self, tables_cosmos_account_name,
            tables_primary_cosmos_account_key):
        # Arrange
        ts = TableServiceClient(self.account_url(tables_cosmos_account_name,
                                                 "cosmos"),
                                credential=tables_primary_cosmos_account_key)
        table_name = u'啊齄丂狛狜'

        client = await ts.create_table(table_name)
        assert client.table_name == table_name

        await ts.delete_table(table_name)
    async def test_retention_too_long_async(self, resource_group, location, cosmos_account, cosmos_account_key):
        # Arrange
        tsc = TableServiceClient(self.account_url(cosmos_account, "cosmos"), cosmos_account_key)
        minute_metrics = Metrics(enabled=True, include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True, days=366))

        await tsc.set_service_properties(None, None, minute_metrics)
        # Assert
        pytest.raises(HttpResponseError,
                          tsc.set_service_properties,
                          None, None, minute_metrics)
        if self.is_live:
            sleep(SLEEP_DELAY)
Ejemplo n.º 28
0
    async def test_batch_sas_auth(self, tables_storage_account_name,
                                  tables_primary_storage_account_key):
        # Arrange
        await self._set_up(tables_storage_account_name,
                           tables_primary_storage_account_key)
        try:

            token = generate_table_sas(
                tables_storage_account_name,
                tables_primary_storage_account_key,
                self.table_name,
                permission=TableSasPermissions(add=True,
                                               read=True,
                                               update=True,
                                               delete=True),
                expiry=datetime.utcnow() + timedelta(hours=1),
                start=datetime.utcnow() - timedelta(minutes=1),
            )
            token = AzureSasCredential(token)

            # Act
            service = TableServiceClient(
                self.account_url(tables_storage_account_name, "table"),
                credential=token,
            )
            table = service.get_table_client(self.table_name)

            entity = TableEntity()
            entity.PartitionKey = 'batch_inserts'
            entity.test = EntityProperty(True)
            entity.test2 = 'value'
            entity.test3 = 3
            entity.test4 = EntityProperty(1234567890)

            batch = table.create_batch()
            transaction_count = 0
            for i in range(10):
                entity.RowKey = str(i)
                batch.create_entity(entity)
                transaction_count += 1
            transaction_result = await table.send_batch(batch)

            assert transaction_result is not None

            total_entities = 0
            async for e in table.list_entities():
                total_entities += 1

            assert total_entities == transaction_count
        finally:
            await self._tear_down()
    async def test_set_table_acl_with_empty_signed_identifier(
            self, tables_storage_account_name,
            tables_primary_storage_account_key):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        ts = TableServiceClient(url,
                                credential=tables_primary_storage_account_key)
        table = await self._create_table(ts)
        try:
            dt = datetime(2021, 6, 8, 2, 10, 9)
            signed_identifiers = {
                'null':
                None,
                'empty':
                TableAccessPolicy(start=None, expiry=None, permission=None),
                'partial':
                TableAccessPolicy(permission='r'),
                'full':
                TableAccessPolicy(start=dt, expiry=dt, permission='r')
            }
            await table.set_table_access_policy(signed_identifiers)
            acl = await table.get_table_access_policy()
            assert acl is not None
            assert len(acl) == 4
            assert acl['null'] is None
            assert acl['empty'] is None
            assert acl['partial'] is not None
            assert acl['partial'].permission == 'r'
            assert acl['partial'].expiry is None
            assert acl['partial'].start is None
            assert acl['full'] is not None
            assert acl['full'].permission == 'r'
            self._assert_policy_datetime(dt, acl['full'].expiry)
            self._assert_policy_datetime(dt, acl['full'].start)

            signed_identifiers.pop('empty')
            signed_identifiers['partial'] = None

            await table.set_table_access_policy(signed_identifiers)
            acl = await table.get_table_access_policy()
            assert acl is not None
            assert len(acl) == 3
            assert 'empty' not in acl
            assert acl['null'] is None
            assert acl['partial'] is None
            assert acl['full'] is not None
            assert acl['full'].permission == 'r'
            self._assert_policy_datetime(dt, acl['full'].expiry)
            self._assert_policy_datetime(dt, acl['full'].start)
        finally:
            await ts.delete_table(table.table_name)
Ejemplo n.º 30
0
    async def test_create_table(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
        table = ts.get_table_client(table_name)
        created = await table.create_table()

        # Assert
        assert created.name == table_name
        await ts.delete_table(table_name=table_name)