portfoliome /
foil
| 1 | import json |
||
|
0 ignored issues
–
show
|
|||
| 2 | import unittest |
||
| 3 | from datetime import datetime |
||
| 4 | from uuid import UUID |
||
| 5 | |||
| 6 | import iso8601 |
||
|
0 ignored issues
–
show
The import
iso8601 could not be resolved.
This can be caused by one of the following: 1. Missing DependenciesThis error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands. # .scrutinizer.yml
before_commands:
- sudo pip install abc # Python2
- sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use
the command for the correct version.
2. Missing __init__.py filesThis error could also result from missing Loading history...
|
|||
| 7 | |||
| 8 | from foil.deserializers import make_json_decoder_hook |
||
| 9 | |||
| 10 | |||
| 11 | def parse_foobar(value): |
||
| 12 | if value == 'foobar': |
||
| 13 | return True |
||
| 14 | else: |
||
| 15 | return value |
||
| 16 | |||
| 17 | |||
| 18 | class TestJSONDeserializer(unittest.TestCase): |
||
| 19 | def test_json_decoder_hook(self): |
||
| 20 | serialized_data = json.dumps( |
||
| 21 | {'time': '2017-01-19T21:41:18.056446Z', |
||
| 22 | 'date': '2013-04-05', 'date_str': '2013-04-05', |
||
| 23 | 'non_date': '8570', 'id': '060444c9-e2d7-4a55-964d-e495f2d5527f', |
||
| 24 | 'description': 'foo', 'data': {'count': 4}, |
||
| 25 | 'foobar_field': 'foobar'} |
||
| 26 | ) |
||
| 27 | converters = {'date_str': str} |
||
| 28 | extra_decoders = (parse_foobar,) |
||
| 29 | object_hook = make_json_decoder_hook( |
||
| 30 | converters=converters, extra_str_decoders=extra_decoders |
||
| 31 | ) |
||
| 32 | |||
| 33 | expected = { |
||
| 34 | 'time': datetime(2017, 1, 19, 21, 41, 18, 56446, |
||
| 35 | tzinfo=iso8601.UTC), |
||
| 36 | 'date': datetime(2013, 4, 5).date(), |
||
| 37 | 'date_str': '2013-04-05', 'non_date': '8570', |
||
| 38 | 'id': UUID('060444c9-e2d7-4a55-964d-e495f2d5527f', version=4), |
||
| 39 | 'description': 'foo', 'data': {'count': 4}, 'foobar_field': True |
||
| 40 | } |
||
| 41 | result = json.loads(serialized_data, object_hook=object_hook) |
||
| 42 | |||
| 43 | self.assertEqual(expected, result) |
||
| 44 | |||
| 45 | def test_nested_converters(self): |
||
| 46 | serialized_data = json.dumps( |
||
| 47 | {'time': {'date': '2014-04-01', 'time': '03:33:23'}} |
||
| 48 | ) |
||
| 49 | converters = {'date': str} |
||
| 50 | object_hook = make_json_decoder_hook(converters=converters) |
||
| 51 | |||
| 52 | expected = {'time': {'date': '2014-04-01', 'time': '03:33:23'}} |
||
| 53 | result = json.loads(serialized_data, object_hook=object_hook) |
||
| 54 | |||
| 55 | self.assertEqual(expected, result) |
||
| 56 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.