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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import base64
from datetime import date, datetime, time, timedelta, tzinfo
from enum import Enum
import json
import sys
from azure.core.serialization import AzureJSONEncoder, NULL
import pytest
def _expand_value(obj):
try:
try:
return obj.to_dict()
except AttributeError:
if isinstance(obj, Enum):
return obj.value
elif isinstance(obj, list):
return [_expand_value(item) for item in obj]
elif isinstance(obj, dict):
return _expand_dict(obj)
else:
return _expand_dict(vars(obj))
except TypeError:
return obj
def _expand_dict(d):
return dict((key, _expand_value(value)) for key, value in d.items())
class DatetimeSubclass(datetime):
"""datetime.datetime subclass that tests datetimes without a type() of datetime.datetime"""
class SerializerMixin(object):
"""Mixin that provides methods for representing a model as a dictionary"""
def to_dict(self):
return _expand_value(vars(self))
class NegativeUtcOffset(tzinfo):
"""tzinfo class with UTC offset of -12 hours"""
_offset = timedelta(seconds=-43200)
_dst = timedelta(0)
_name = "-1200"
def utcoffset(self, dt):
return self.__class__._offset
def dst(self, dt):
return self.__class__._dst
def tzname(self, dt):
return self.__class__._name
class PositiveUtcOffset(tzinfo):
"""tzinfo class with UTC offset of +12 hours"""
_offset = timedelta(seconds=43200)
_dst = timedelta(0)
_name = "+1200"
def utcoffset(self, dt):
return self.__class__._offset
def dst(self, dt):
return self.__class__._dst
def tzname(self, dt):
return self.__class__._name
def test_NULL_is_falsy():
assert NULL is not False
assert bool(NULL) is False
assert NULL is NULL
@pytest.fixture
def json_dumps_with_encoder():
def func(obj):
return json.dumps(obj, cls=AzureJSONEncoder)
return func
def test_bytes(json_dumps_with_encoder):
test_bytes = b"mybytes"
result = json.loads(json_dumps_with_encoder(test_bytes))
assert base64.b64decode(result) == test_bytes
def test_byte_array_ascii(json_dumps_with_encoder):
test_byte_array = bytearray("mybytes", "ascii")
result = json.loads(json_dumps_with_encoder(test_byte_array))
assert base64.b64decode(result) == test_byte_array
def test_byte_array_utf8(json_dumps_with_encoder):
test_byte_array = bytearray("mybytes", "utf-8")
result = json.loads(json_dumps_with_encoder(test_byte_array))
assert base64.b64decode(result) == test_byte_array
def test_byte_array_utf16(json_dumps_with_encoder):
test_byte_array = bytearray("mybytes", "utf-16")
result = json.loads(json_dumps_with_encoder(test_byte_array))
assert base64.b64decode(result) == test_byte_array
def test_dictionary_basic(json_dumps_with_encoder):
test_obj = {
"string": "myid",
"number": 42,
"boolean": True,
"list_of_string": [1, 2, 3],
"dictionary_of_number": {"pi": 3.14},
}
complex_serialized = json_dumps_with_encoder(test_obj)
assert json.dumps(test_obj) == complex_serialized
assert json.loads(complex_serialized) == test_obj
def test_model_basic(json_dumps_with_encoder):
class BasicModel(SerializerMixin):
def __init__(self):
self.string = "myid"
self.number = 42
self.boolean = True
self.list_of_ints = [1, 2, 3]
self.dictionary_of_number = {"pi": 3.14}
self.bytes_data = b"data as bytes"
expected = BasicModel()
expected_bytes = "data as bytes" if sys.version_info.major == 2 else "ZGF0YSBhcyBieXRlcw==" # cspell:disable-line
expected_dict = {
"string": "myid",
"number": 42,
"boolean": True,
"list_of_ints": [1, 2, 3],
"dictionary_of_number": {"pi": 3.14},
"bytes_data": expected_bytes,
}
assert json.loads(json_dumps_with_encoder(expected.to_dict())) == expected_dict
def test_dictionary_datetime(json_dumps_with_encoder):
test_obj = {
"timedelta": timedelta(1),
"date": date(2021, 5, 12),
"datetime": datetime.strptime("2012-02-24T00:53:52.780Z", "%Y-%m-%dT%H:%M:%S.%fZ"),
"time": time(11, 12, 13),
}
expected = {
"timedelta": "P1DT00H00M00S",
"date": "2021-05-12",
"datetime": "2012-02-24T00:53:52.780000Z",
"time": "11:12:13",
}
assert json.loads(json_dumps_with_encoder(test_obj)) == expected
def test_model_datetime(json_dumps_with_encoder):
class DatetimeModel(SerializerMixin):
def __init__(self):
self.timedelta = timedelta(1)
self.date = date(2021, 5, 12)
self.datetime = datetime.strptime("2012-02-24T00:53:52.780Z", "%Y-%m-%dT%H:%M:%S.%fZ")
self.time = time(11, 12, 13)
expected = DatetimeModel()
expected_dict = {
"timedelta": "P1DT00H00M00S",
"date": "2021-05-12",
"datetime": "2012-02-24T00:53:52.780000Z",
"time": "11:12:13",
}
assert json.loads(json_dumps_with_encoder(expected.to_dict())) == expected_dict
def test_model_key_vault(json_dumps_with_encoder):
class Attributes(SerializerMixin):
def __init__(self):
self.enabled = True
self.not_before = datetime.strptime("2012-02-24T00:53:52.780Z", "%Y-%m-%dT%H:%M:%S.%fZ")
self.expires = datetime.strptime("2032-02-24T00:53:52.780Z", "%Y-%m-%dT%H:%M:%S.%fZ")
self.created = datetime.strptime("2020-02-24T00:53:52.780Z", "%Y-%m-%dT%H:%M:%S.%fZ")
self.updated = datetime.strptime("2021-02-24T00:53:52.780Z", "%Y-%m-%dT%H:%M:%S.%fZ")
class ResourceId(SerializerMixin):
def __init__(self):
self.source_id = "source-id"
self.vault_url = "vault-url"
self.name = "name"
self.version = None
class Identifier(SerializerMixin):
def __init__(self):
self._resource_id = ResourceId()
class Properties(SerializerMixin):
def __init__(self):
self._attributes = Attributes()
self._id = "id"
self._vault_id = Identifier()
self._thumbprint = b"thumbprint bytes"
self._tags = None
expected = Properties()
expected_bytes = (
"thumbprint bytes" if sys.version_info.major == 2 else "dGh1bWJwcmludCBieXRlcw=="
) # cspell:disable-line
expected_dict = {
"_attributes": {
"enabled": True,
"not_before": "2012-02-24T00:53:52.780000Z",
"expires": "2032-02-24T00:53:52.780000Z",
"created": "2020-02-24T00:53:52.780000Z",
"updated": "2021-02-24T00:53:52.780000Z",
},
"_id": "id",
"_vault_id": {
"_resource_id": {
"source_id": "source-id",
"vault_url": "vault-url",
"name": "name",
"version": None,
},
},
"_thumbprint": expected_bytes,
"_tags": None,
}
assert json.loads(json_dumps_with_encoder(expected.to_dict())) == expected_dict
def test_serialize_datetime(json_dumps_with_encoder):
date_obj = datetime.strptime("2015-01-01T00:00:00", "%Y-%m-%dT%H:%M:%S")
date_str = json_dumps_with_encoder(date_obj)
assert date_str == '"2015-01-01T00:00:00Z"'
date_obj = datetime.strptime("1999-12-31T23:59:59", "%Y-%m-%dT%H:%M:%S").replace(tzinfo=NegativeUtcOffset())
date_str = json_dumps_with_encoder(date_obj)
assert date_str == '"2000-01-01T11:59:59Z"'
date_obj = datetime.strptime("2015-06-01T16:10:08.0121", "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=PositiveUtcOffset())
date_str = json_dumps_with_encoder(date_obj)
assert date_str == '"2015-06-01T04:10:08.012100Z"'
date_obj = datetime.min
date_str = json_dumps_with_encoder(date_obj)
assert date_str == '"0001-01-01T00:00:00Z"'
date_obj = datetime.max
date_str = json_dumps_with_encoder(date_obj)
assert date_str == '"9999-12-31T23:59:59.999999Z"'
date_obj = datetime.strptime("2012-02-24T00:53:52.000001Z", "%Y-%m-%dT%H:%M:%S.%fZ")
date_str = json_dumps_with_encoder(date_obj)
assert date_str == '"2012-02-24T00:53:52.000001Z"'
date_obj = datetime.strptime("2012-02-24T00:53:52.780Z", "%Y-%m-%dT%H:%M:%S.%fZ")
date_str = json_dumps_with_encoder(date_obj)
assert date_str == '"2012-02-24T00:53:52.780000Z"'
def test_serialize_datetime_subclass(json_dumps_with_encoder):
date_obj = DatetimeSubclass.strptime("2012-02-24T00:53:52.780Z", "%Y-%m-%dT%H:%M:%S.%fZ")
date_str = json_dumps_with_encoder(date_obj)
assert date_str == '"2012-02-24T00:53:52.780000Z"'
def test_serialize_time(json_dumps_with_encoder):
time_str = json_dumps_with_encoder(time(11, 22, 33))
assert time_str == '"11:22:33"'
time_str = json_dumps_with_encoder(time(11, 22, 33, 444444))
assert time_str == '"11:22:33.444444"'
class BasicEnum(Enum):
val = "Basic"
class StringEnum(str, Enum):
val = "string"
class IntEnum(int, Enum):
val = 1
class FloatEnum(float, Enum):
val = 1.5
def test_dictionary_enum(json_dumps_with_encoder):
test_obj = {"basic": BasicEnum.val}
with pytest.raises(TypeError):
json_dumps_with_encoder(test_obj)
test_obj = {
"basic": BasicEnum.val.value,
"string": StringEnum.val.value,
"int": IntEnum.val.value,
"float": FloatEnum.val.value,
}
expected = {"basic": "Basic", "string": "string", "int": 1, "float": 1.5}
serialized = json_dumps_with_encoder(test_obj)
assert json.dumps(test_obj) == serialized
assert json.loads(serialized) == expected
def test_model_enum(json_dumps_with_encoder):
class BasicEnumModel:
def __init__(self):
self.basic = BasicEnum.val
with pytest.raises(TypeError):
json_dumps_with_encoder(BasicEnumModel())
class EnumModel(SerializerMixin):
def __init__(self):
self.basic = BasicEnum.val.value
self.string = StringEnum.val
self.int = IntEnum.val
self.float = FloatEnum.val
expected = EnumModel()
expected_dict = {"basic": "Basic", "string": "string", "int": 1, "float": 1.5}
assert json.loads(json_dumps_with_encoder(expected.to_dict())) == expected_dict
def test_dictionary_none(json_dumps_with_encoder):
assert json_dumps_with_encoder(None) == json.dumps(None)
test_obj = {"entry": None}
assert json.loads(json_dumps_with_encoder(test_obj)) == test_obj
def test_model_none(json_dumps_with_encoder):
class NoneModel(SerializerMixin):
def __init__(self):
self.entry = None
expected = NoneModel()
expected_dict = {"entry": None}
assert json.loads(json_dumps_with_encoder(expected.to_dict())) == expected_dict
def test_dictionary_empty_collections(json_dumps_with_encoder):
test_obj = {
"dictionary": {},
"list": [],
}
assert json.dumps(test_obj) == json_dumps_with_encoder(test_obj)
assert json.loads(json_dumps_with_encoder(test_obj)) == test_obj
def test_model_empty_collections(json_dumps_with_encoder):
class EmptyCollectionsModel(SerializerMixin):
def __init__(self):
self.dictionary = {}
self.list = []
expected = EmptyCollectionsModel()
expected_dict = {
"dictionary": {},
"list": [],
}
assert json.loads(json_dumps_with_encoder(expected.to_dict())) == expected_dict
def test_model_inheritance(json_dumps_with_encoder):
class ParentModel(SerializerMixin):
def __init__(self):
self.parent = "parent"
class ChildModel(ParentModel):
def __init__(self):
super(ChildModel, self).__init__()
self.child = "child"
expected = ChildModel()
expected_dict = {
"parent": "parent",
"child": "child",
}
assert json.loads(json_dumps_with_encoder(expected.to_dict())) == expected_dict
def test_model_recursion(json_dumps_with_encoder):
class RecursiveModel(SerializerMixin):
def __init__(self):
self.name = "it's me!"
self.list_of_me = None
self.dict_of_me = None
self.dict_of_list_of_me = None
self.list_of_dict_of_me = None
expected = RecursiveModel()
expected.list_of_me = [RecursiveModel()]
expected.dict_of_me = {"me": RecursiveModel()}
expected.dict_of_list_of_me = {"many mes": [RecursiveModel()]}
expected.list_of_dict_of_me = [{"me": RecursiveModel()}]
expected_dict = {
"name": "it's me!",
"list_of_me": [
{
"name": "it's me!",
"list_of_me": None,
"dict_of_me": None,
"dict_of_list_of_me": None,
"list_of_dict_of_me": None,
}
],
"dict_of_me": {
"me": {
"name": "it's me!",
"list_of_me": None,
"dict_of_me": None,
"dict_of_list_of_me": None,
"list_of_dict_of_me": None,
}
},
"dict_of_list_of_me": {
"many mes": [
{
"name": "it's me!",
"list_of_me": None,
"dict_of_me": None,
"dict_of_list_of_me": None,
"list_of_dict_of_me": None,
}
]
},
"list_of_dict_of_me": [
{
"me": {
"name": "it's me!",
"list_of_me": None,
"dict_of_me": None,
"dict_of_list_of_me": None,
"list_of_dict_of_me": None,
}
}
],
}
assert json.loads(json_dumps_with_encoder(expected.to_dict())) == expected_dict
|