File: test_decoder.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 (330 lines) | stat: -rw-r--r-- 15,751 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# 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 uuid
from datetime import datetime, timezone
from math import isnan

from azure.data.tables import TableClient, EdmType, EntityProperty
from azure.data.tables._common_conversion import _to_utc_datetime

from _shared.testcase import TableTestCase, _convert_to_entity

from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy
from preparers import tables_decorator


def _clean(entity):
    cleaned = {}
    for key, value in entity.items():
        if isinstance(value, float) and isnan(value):
            value = "NaN"
        cleaned[key] = value
    return cleaned


class TestTableDecoder(AzureRecordedTestCase, TableTestCase):
    @tables_decorator
    @recorded_by_proxy
    def test_decode_entity_basic(self, tables_storage_account_name, tables_primary_storage_account_key):
        table_name = self.get_resource_name("uttable01")
        url = self.account_url(tables_storage_account_name, "table")
        responses = {}

        def callback(response):
            responses["response_body"] = response.http_response.json()

        # Test basic string, int32 and bool data
        with TableClient(url, table_name, credential=tables_primary_storage_account_key) as client:
            client.create_table()
            test_entity = {"PartitionKey": "PK", "RowKey": "RK", "Data1": 1, "Data2": True}
            created = client.create_entity(test_entity)
            result = client.get_entity("PK", "RK", raw_response_hook=callback)
            old_result = _convert_to_entity(responses["response_body"])
            assert result == old_result, f"Old:\n'{old_result}'\ndoes not match new:\n'{result}'."

            test_entity = {"PartitionKey": "PK", "RowKey": "RK'@*$!%"}
            created = client.create_entity(test_entity)
            result = client.get_entity("PK", "RK'@*$!%", raw_response_hook=callback)
            old_result = _convert_to_entity(responses["response_body"])
            assert result == old_result, f"Old:\n'{old_result}'\ndoes not match new:\n'{result}'."

            result = client.query_entities("PartitionKey eq 'PK'", raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert new == old, f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            result = client.list_entities(raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert new == old, f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            client.delete_table()

    @tables_decorator
    @recorded_by_proxy
    def test_decode_entity_type_conversion(
        self, tables_storage_account_name, tables_primary_storage_account_key, **kwargs
    ):
        recorded_variables = kwargs.pop("variables", {})
        recorded_uuid = self.set_uuid_variable(recorded_variables, "uuid", uuid.uuid4())
        table_name = self.get_resource_name("uttable02")
        url = self.account_url(tables_storage_account_name, "table")
        responses = {}

        def callback(response):
            responses["response_body"] = response.http_response.json()

        # All automatically detected data types
        with TableClient(url, table_name, credential=tables_primary_storage_account_key) as client:
            client.create_table()
            test_entity = {
                "PartitionKey": "PK",
                "RowKey": "RK",
                "Data1": 12345,
                "Data2": False,
                "Data3": b"testdata",
                "Data4": self.get_datetime(),
                "Data5": recorded_uuid,
                "Data6": "Foobar",
                "Data7": 3.14,
                "Data8": None,
            }
            created = client.create_entity(test_entity)
            result = client.get_entity("PK", "RK", raw_response_hook=callback)
            old_result = _convert_to_entity(responses["response_body"])
            assert result == old_result, f"Old:\n'{old_result}'\ndoes not match new:\n'{result}'."

            result = client.query_entities("PartitionKey eq 'PK'", raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert new == old, f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            result = client.list_entities(raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert new == old, f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            client.delete_table()
        return recorded_variables

    @tables_decorator
    @recorded_by_proxy
    def test_decode_entity_tuples(self, tables_storage_account_name, tables_primary_storage_account_key, **kwargs):
        recorded_variables = kwargs.pop("variables", {})
        recorded_uuid = self.set_uuid_variable(recorded_variables, "uuid", uuid.uuid4())
        table_name = self.get_resource_name("uttable03")
        url = self.account_url(tables_storage_account_name, "table")
        responses = {}

        def callback(response):
            responses["response_body"] = response.http_response.json()

        # Explicit datatypes using Tuple definition
        with TableClient(url, table_name, credential=tables_primary_storage_account_key) as client:
            client.create_table()
            test_entity = {
                "PartitionKey": "PK",
                "RowKey": "RK1",
                "Data1": (12345, EdmType.INT32),
                "Data2": (False, "Edm.Boolean"),
                "Data3": EntityProperty(value=b"testdata", edm_type=EdmType.BINARY),
                "Data4": EntityProperty(
                    datetime(year=2022, month=4, day=1, hour=9, minute=30, second=45, tzinfo=timezone.utc),
                    "Edm.DateTime",
                ),
                "Data5": EntityProperty(recorded_uuid, "Edm.Guid"),
                "Data6": ("Foobar", EdmType.STRING),
                "Data7": (3.14, EdmType.DOUBLE),
                "Data8": (2**60, "Edm.Int64"),
            }
            created = client.create_entity(test_entity)
            result = client.get_entity("PK", "RK1", raw_response_hook=callback)
            old_result = _convert_to_entity(responses["response_body"])
            assert result == old_result, f"Old:\n'{old_result}'\ndoes not match new:\n'{result}'."

            test_entity = {
                "PartitionKey": "PK",
                "RowKey": "RK2",
                "Data1": ("12345", EdmType.INT32),
                "Data2": ("False", "Edm.Boolean"),
                "Data3": (None, EdmType.STRING),
                "Data4": EntityProperty(
                    _to_utc_datetime(datetime(2022, 4, 1, 9, 30, 45, tzinfo=timezone.utc)), "Edm.DateTime"
                ),
                "Data5": EntityProperty("9b8fbe91-a1d8-46bb-99b6-09e1a6afd9cf", "Edm.Guid"),
                "Data6": (None, EdmType.BOOLEAN),
                "Data7": EntityProperty("3.14", "Edm.Double"),
                "Data8": ("9223372036854775807", "Edm.Int64"),
            }
            created = client.create_entity(test_entity)
            result = client.get_entity("PK", "RK2", raw_response_hook=callback)
            old_result = _convert_to_entity(responses["response_body"])
            assert result == old_result, f"Old:\n'{old_result}'\ndoes not match new:\n'{result}'."

            result = client.query_entities("PartitionKey eq 'PK'", raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert new == old, f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            result = client.list_entities(raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert new == old, f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            client.delete_table()
        return recorded_variables

    @tables_decorator
    @recorded_by_proxy
    def test_decode_entity_raw(self, tables_storage_account_name, tables_primary_storage_account_key, **kwargs):
        recorded_variables = kwargs.pop("variables", {})
        recorded_uuid = self.set_uuid_variable(recorded_variables, "uuid", uuid.uuid4())
        table_name = self.get_resource_name("uttable04")
        url = self.account_url(tables_storage_account_name, "table")
        responses = {}

        def callback(response):
            responses["response_body"] = response.http_response.json()

        # Raw payload with existing EdmTypes
        with TableClient(url, table_name, credential=tables_primary_storage_account_key) as client:
            client.create_table()
            dt = self.get_datetime()
            guid = recorded_uuid
            test_entity = {
                "PartitionKey": "PK",
                "PartitionKey@odata.type": "Edm.String",
                "RowKey": "RK",
                "RowKey@odata.type": "Edm.String",
                "Data1": "12345",
                "Data1@odata.type": "Edm.Int32",
                "Data2": "False",
                "Data2@odata.type": "Edm.Boolean",
                "Data3": b"testdata",
                "Data3@odata.type": "Edm.Binary",
                "Data4": dt,
                "Data4@odata.type": "Edm.DateTime",
                "Data5": guid,
                "Data5@odata.type": "Edm.Guid",
                "Data6": "Foobar",
                "Data6@odata.type": "Edm.String",
                "Data7": "3.14",
                "Data7@odata.type": "Edm.Double",
                "Data8": "1152921504606846976",
                "Data8@odata.type": "Edm.Int64",
            }
            created = client.create_entity(test_entity)
            result = client.get_entity("PK", "RK", raw_response_hook=callback)
            old_result = _convert_to_entity(responses["response_body"])
            assert result == old_result, f"Old:\n'{old_result}'\ndoes not match new:\n'{result}'."

            result = client.query_entities("PartitionKey eq 'PK'", raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert new == old, f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            result = client.list_entities(raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert new == old, f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            client.delete_table()
        return recorded_variables

    @tables_decorator
    @recorded_by_proxy
    def test_decode_entity_atypical_values(self, tables_storage_account_name, tables_primary_storage_account_key):
        table_name = self.get_resource_name("uttable05")
        url = self.account_url(tables_storage_account_name, "table")
        responses = {}

        def callback(response):
            responses["response_body"] = response.http_response.json()

        with TableClient(url, table_name, credential=tables_primary_storage_account_key) as client:
            client.create_table()
            # Non-UTF8 characters in both keys and properties
            non_utf8_char = "你好"
            test_entity = {"PartitionKey": "PK", "RowKey": non_utf8_char, "Data": non_utf8_char}
            created = client.create_entity(test_entity)
            result = client.get_entity("PK", non_utf8_char, raw_response_hook=callback)
            old_result = _convert_to_entity(responses["response_body"])
            assert result == old_result, f"Old:\n'{old_result}'\ndoes not match new:\n'{result}'."

            max_int64 = 9223372036854775807
            # Valid int64 value with Edm
            test_entity = {"PartitionKey": "PK", "RowKey": "RK2", "Data": (max_int64, "Edm.Int64")}
            created = client.create_entity(test_entity)
            result = client.get_entity("PK", "RK2", raw_response_hook=callback)
            old_result = _convert_to_entity(responses["response_body"])
            assert result == old_result, f"Old:\n'{old_result}'\ndoes not match new:\n'{result}'."

            # Infinite float values
            test_entity = {
                "PartitionKey": "PK",
                "RowKey": "RK4",
                "Data1": float("nan"),
                "Data2": float("inf"),
                "Data3": float("-inf"),
            }
            created = client.create_entity(test_entity)
            result = client.get_entity("PK", "RK4", raw_response_hook=callback)
            old_result = _convert_to_entity(responses["response_body"])
            assert _clean(result) == _clean(old_result), f"Old:\n'{old_result}'\ndoes not match new:\n'{result}'."

            result = client.query_entities("PartitionKey eq 'PK'", raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert str(new) == str(old), f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            result = client.list_entities(raw_response_hook=callback)
            result_copy = []
            for e in result:
                result_copy.append(e)
            old_result = [_convert_to_entity(t) for t in responses["response_body"]["value"]]
            assert len(result_copy) == len(old_result)
            for new, old in zip(result_copy, old_result):
                assert str(new) == str(old), f"Old:\n'{old}'\ndoes not match new:\n'{new}'."

            client.delete_table()