Completed
Push — master ( e0da72...49d54f )
by Philip
25s
created

TestJSONSerializer.test_serialize_object()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 7
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
import uuid
4
from datetime import date, datetime
5
6
import pytz
0 ignored issues
show
Configuration introduced by
The import pytz 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.serializers import json_serializer
9
10
11
class TestJSONSerializer(unittest.TestCase):
12
13
    def test_serialize_uuid(self):
14
        uid = uuid.uuid4()
15
16
        expected = '"{}"'.format(uid)
17
        result = self._serialize(uid)
18
19
        self.assertEquals(expected, result)
20
21
    def test_serialize_date(self):
22
        d = date(2014, 7, 14)
23
24
        expected = '"{}"'.format(d.strftime('%Y-%m-%d'))
25
        result = self._serialize(d)
26
27
        self.assertEquals(expected, result)
28
29
    def test_serialize_datetime(self):
30
        dt = datetime(2015, 3, 13, 20, 33, 22, 567)
31
32
        expected = '"{}Z"'.format(dt.strftime('%Y-%m-%dT%H:%M:%S.%f'))
33
        result = self._serialize(dt)
34
35
        self.assertEquals(expected, result)
36
37
    def test_serialize_utc_datetime(self):
38
        dt = datetime(2015, 3, 13, 20, 33, 22, 567, tzinfo=pytz.UTC)
39
40
        expected = '"{}Z"'.format(dt.strftime('%Y-%m-%dT%H:%M:%S.%f'))
41
        result = self._serialize(dt)
42
43
        self.assertEqual(expected, result)
44
45
    def test_serialize_aware_datetime(self):
46
        eastern_zone = pytz.timezone('US/Eastern')
47
        dt = eastern_zone.localize(datetime(2015, 3, 13, 20, 33, 22, 567))
48
49
        expected = '"2015-03-13T20:33:22.000567-04:00"'
50
        result = self._serialize(dt)
51
52
        self.assertEqual(expected, result)
53
54
    def test_serialize_object(self):
55
        data = {'hello': 'world'}
56
57
        expected = json.dumps(data)
58
        result = json_serializer(data)
59
60
        self.assertEqual(expected, result)
61
62
    def _serialize(self, obj):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
63
        return json.dumps(obj, default=json_serializer)
64