|
14 | 14 |
|
15 | 15 | import base64 |
16 | 16 | import datetime |
| 17 | +import decimal |
17 | 18 | import io |
18 | 19 | import json |
19 | 20 |
|
@@ -377,6 +378,79 @@ def test_accepts_partial_iso_format(self): |
377 | 378 | self.assertEqual(body['Timestamp'], 0) |
378 | 379 |
|
379 | 380 |
|
| 381 | +class TestJSONFloatSerialization(unittest.TestCase): |
| 382 | + def setUp(self): |
| 383 | + self.model = { |
| 384 | + 'metadata': { |
| 385 | + 'protocol': 'json', |
| 386 | + 'apiVersion': '2014-01-01', |
| 387 | + 'jsonVersion': '1.1', |
| 388 | + 'targetPrefix': 'foo', |
| 389 | + }, |
| 390 | + 'documentation': '', |
| 391 | + 'operations': { |
| 392 | + 'TestOperation': { |
| 393 | + 'name': 'TestOperation', |
| 394 | + 'http': { |
| 395 | + 'method': 'POST', |
| 396 | + 'requestUri': '/', |
| 397 | + }, |
| 398 | + 'input': {'shape': 'InputShape'}, |
| 399 | + } |
| 400 | + }, |
| 401 | + 'shapes': { |
| 402 | + 'InputShape': { |
| 403 | + 'type': 'structure', |
| 404 | + 'members': { |
| 405 | + 'Double': {'shape': 'DoubleType'}, |
| 406 | + 'Float': {'shape': 'FloatType'}, |
| 407 | + }, |
| 408 | + }, |
| 409 | + 'DoubleType': { |
| 410 | + 'type': 'double', |
| 411 | + }, |
| 412 | + 'FloatType': { |
| 413 | + 'type': 'float', |
| 414 | + }, |
| 415 | + }, |
| 416 | + } |
| 417 | + self.service_model = ServiceModel(self.model) |
| 418 | + |
| 419 | + def serialize_to_request(self, input_params): |
| 420 | + request_serializer = serialize.create_serializer( |
| 421 | + self.service_model.metadata['protocol'] |
| 422 | + ) |
| 423 | + return request_serializer.serialize_to_request( |
| 424 | + input_params, self.service_model.operation_model('TestOperation') |
| 425 | + ) |
| 426 | + |
| 427 | + def test_accepts_decimal_with_precision_above_floats(self): |
| 428 | + float_string = '0.12345678901234567890' |
| 429 | + float_as_float = float( |
| 430 | + float_string |
| 431 | + ) # This has less precision; it will be lost on serialization |
| 432 | + float_as_decimal = decimal.Decimal(float_string) |
| 433 | + body = json.loads( |
| 434 | + self.serialize_to_request({'Float': float_as_decimal})[ |
| 435 | + 'body' |
| 436 | + ].decode('utf-8') |
| 437 | + ) |
| 438 | + self.assertEqual(decimal.Decimal(body['Float']), float_as_float) |
| 439 | + |
| 440 | + def test_accepts_decimal_with_precision_above_doubles(self): |
| 441 | + double_string = '0.12345678901234567890' |
| 442 | + double_as_float = float( |
| 443 | + double_string |
| 444 | + ) # This has less precision; it will be lost on serialization |
| 445 | + double_as_decimal = decimal.Decimal(double_string) |
| 446 | + body = json.loads( |
| 447 | + self.serialize_to_request({'Double': double_as_decimal})[ |
| 448 | + 'body' |
| 449 | + ].decode('utf-8') |
| 450 | + ) |
| 451 | + self.assertEqual(decimal.Decimal(body['Double']), double_as_float) |
| 452 | + |
| 453 | + |
380 | 454 | class TestInstanceCreation(unittest.TestCase): |
381 | 455 | def setUp(self): |
382 | 456 | self.model = { |
|
0 commit comments