Completed
Pull Request — master (#694)
by Eric
02:14
created

TestSerialization   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 13
Bugs 1 Features 5
Metric Value
c 13
b 1
f 5
dl 0
loc 20
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A test_deserialize() 0 10 3
A test_serialize_return_type() 0 7 3
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
"""
5
test_serializers
6
----------------
7
8
Tests for `cookiecutter.serialization` serializer classes.
9
"""
10
11
from __future__ import unicode_literals
12
13
import pytest
14
15
from cookiecutter.serialization import \
16
    JsonSerializer, PickleSerializer, AbstractSerializer
17
18
19
class __Dummy(object):
20
    """
21
    fixture class
22
    """
23
24
    def __init__(self, name):
25
        self.__name = name
26
27
    def get_name(self):
28
        return self.__name
29
30
    def __eq__(self, other):
31
        return self.get_name() == other
32
33
34
class __Py27Serializer(AbstractSerializer):
35
    """
36
    fixture class to check some non covered part of the AbstractSerializer
37
    under python 2.7
38
    """
39
40
    def _do_serialize(self, subject):
41
        """
42
        serialize a given subject to its JSON representation
43
        :param subject: the subject to serialize
44
        """
45
        return subject
46
47
    def _do_deserialize(self, bstring):
48
        """
49
        deserialize a given JSON string to its Python object
50
        :param bstring: the bytes string to deserialize
51
        """
52
        return bstring
53
54
55
@pytest.fixture
56
def get_serializers():
57
    """
58
    serializer provider
59
    """
60
    return {
61
        JsonSerializer: {"key": "value"},
62
        PickleSerializer: __Dummy('dummy'),
63
        __Py27Serializer: 'string'
64
    }
65
66
67
class TestSerialization(object):
68
69
    def test_serialize_return_type(self):
70
        """
71
        The serialize method should return a bytes string
72
        """
73
        serializers = get_serializers()
74
        for kclass in serializers:
75
            assert type(kclass().serialize(serializers[kclass])) == bytes
76
77
    def test_deserialize(self):
78
        """
79
        The deserialize method should return the previous object
80
        """
81
        serializers = get_serializers()
82
        for kclass in serializers:
83
            serializer = kclass()
84
            obj = serializers[kclass]
85
            string = serializer.serialize(obj)
86
            assert obj == serializer.deserialize(string)
87