File: test_table_service_properties_async.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (203 lines) | stat: -rw-r--r-- 8,490 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import time
import pytest

from devtools_testutils import AzureRecordedTestCase
from devtools_testutils.aio import recorded_by_proxy_async

from azure.core.exceptions import ResourceNotFoundError, HttpResponseError

from azure.data.tables import TableAnalyticsLogging, TableMetrics, TableRetentionPolicy, TableCorsRule
from azure.data.tables.aio import TableServiceClient

from _shared.testcase import TableTestCase
from async_preparers import tables_decorator_async

# ------------------------------------------------------------------------------


class TestTableServicePropertiesAsync(AzureRecordedTestCase, TableTestCase):
    @tables_decorator_async
    @recorded_by_proxy_async
    async def test_table_service_properties_async(
        self, tables_storage_account_name, tables_primary_storage_account_key
    ):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        tsc = TableServiceClient(url, credential=tables_primary_storage_account_key, logging_enable=True)
        # Act
        resp = await tsc.set_service_properties(
            analytics_logging=TableAnalyticsLogging(),
            hour_metrics=TableMetrics(),
            minute_metrics=TableMetrics(),
            cors=list(),
        )

        # Assert
        assert resp is None
        if self.is_live:
            time.sleep(45)
        self._assert_properties_default(await tsc.get_service_properties())

    # --Test cases per feature ---------------------------------------
    @tables_decorator_async
    @recorded_by_proxy_async
    async def test_set_logging_async(self, tables_storage_account_name, tables_primary_storage_account_key):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        tsc = TableServiceClient(url, credential=tables_primary_storage_account_key)
        logging = TableAnalyticsLogging(
            read=True, write=True, delete=True, retention_policy=TableRetentionPolicy(enabled=True, days=5)
        )

        # Act
        await tsc.set_service_properties(analytics_logging=logging)

        # Assert
        if self.is_live:
            time.sleep(45)
        received_props = await tsc.get_service_properties()
        self._assert_logging_equal(received_props["analytics_logging"], logging)

    @tables_decorator_async
    @recorded_by_proxy_async
    async def test_set_hour_metrics_async(self, tables_storage_account_name, tables_primary_storage_account_key):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        tsc = TableServiceClient(url, credential=tables_primary_storage_account_key)
        hour_metrics = TableMetrics(
            enabled=True, include_apis=True, retention_policy=TableRetentionPolicy(enabled=True, days=5)
        )

        # Act
        await tsc.set_service_properties(hour_metrics=hour_metrics)

        # Assert
        if self.is_live:
            time.sleep(45)
        received_props = await tsc.get_service_properties()
        self._assert_metrics_equal(received_props["hour_metrics"], hour_metrics)

    @tables_decorator_async
    @recorded_by_proxy_async
    async def test_set_minute_metrics_async(self, tables_storage_account_name, tables_primary_storage_account_key):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        tsc = TableServiceClient(url, credential=tables_primary_storage_account_key)
        minute_metrics = TableMetrics(
            enabled=True, include_apis=True, retention_policy=TableRetentionPolicy(enabled=True, days=5)
        )

        # Act
        await tsc.set_service_properties(minute_metrics=minute_metrics)

        # Assert
        if self.is_live:
            time.sleep(45)
        received_props = await tsc.get_service_properties()
        self._assert_metrics_equal(received_props["minute_metrics"], minute_metrics)

    @tables_decorator_async
    @recorded_by_proxy_async
    async def test_set_cors_async(self, tables_storage_account_name, tables_primary_storage_account_key):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        tsc = TableServiceClient(url, credential=tables_primary_storage_account_key)
        cors_rule1 = TableCorsRule(["www.xyz.com"], ["GET"])

        allowed_origins = ["www.xyz.com", "www.ab.com", "www.bc.com"]
        allowed_methods = ["GET", "PUT"]
        max_age_in_seconds = 500
        exposed_headers = ["x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc", "x-ms-meta-bcd"]
        allowed_headers = ["x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz", "x-ms-meta-foo"]
        cors_rule2 = TableCorsRule(allowed_origins, allowed_methods)
        cors_rule2.max_age_in_seconds = max_age_in_seconds
        cors_rule2.exposed_headers = exposed_headers
        cors_rule2.allowed_headers = allowed_headers

        cors = [cors_rule1, cors_rule2]

        # Act
        await tsc.set_service_properties(cors=cors)

        # Assert
        if self.is_live:
            time.sleep(45)
        received_props = await tsc.get_service_properties()
        self._assert_cors_equal(received_props["cors"], cors)

    # --Test cases for errors ---------------------------------------
    @tables_decorator_async
    @recorded_by_proxy_async
    async def test_too_many_cors_rules_async(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
        )
        cors = []
        for i in range(0, 6):
            cors.append(TableCorsRule(["www.xyz.com"], ["GET"]))

        # Assert
        with pytest.raises(HttpResponseError):
            await tsc.set_service_properties(cors=cors)

    @tables_decorator_async
    @recorded_by_proxy_async
    async def test_retention_too_long_async(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
        )
        minute_metrics = TableMetrics(
            enabled=True, include_apis=True, retention_policy=TableRetentionPolicy(enabled=True, days=366)
        )

        # Assert
        with pytest.raises(HttpResponseError):
            await tsc.set_service_properties(minute_metrics=minute_metrics)

    @tables_decorator_async
    @recorded_by_proxy_async
    async def test_client_with_url_ends_with_table_name(
        self, tables_storage_account_name, tables_primary_storage_account_key
    ):
        url = self.account_url(tables_storage_account_name, "table")
        table_name = self.get_resource_name("mytable")
        invalid_url = url + "/" + table_name
        tsc = TableServiceClient(invalid_url, credential=tables_primary_storage_account_key)

        with pytest.raises(ResourceNotFoundError) as exc:
            await tsc.create_table(table_name)
        assert ("table specified does not exist") in str(exc.value)
        assert ("Please check your account URL.") in str(exc.value)

        with pytest.raises(ResourceNotFoundError) as exc:
            await tsc.create_table_if_not_exists(table_name)
        assert ("table specified does not exist") in str(exc.value)
        assert ("Please check your account URL.") in str(exc.value)

        with pytest.raises(HttpResponseError) as exc:
            await tsc.set_service_properties(analytics_logging=TableAnalyticsLogging(write=True))
        assert ("URI is invalid") in str(exc.value)
        assert ("Please check your account URL.") in str(exc.value)

        with pytest.raises(HttpResponseError) as exc:
            await tsc.get_service_properties()
        assert ("URI is invalid") in str(exc.value)
        assert ("Please check your account URL.") in str(exc.value)

        await tsc.delete_table(table_name)


class TestTableUnitTest(TableTestCase):
    @pytest.mark.asyncio
    async def test_retention_no_days_async(self):
        # Assert
        pytest.raises(ValueError, TableRetentionPolicy, enabled=True)