parse_foobar()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
1
import json
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import unittest
3
from datetime import datetime
4
from uuid import UUID
5
6
import iso8601
0 ignored issues
show
Configuration introduced by
The import iso8601 could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This 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 files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

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