aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/utils/tests/test_jsonable.py
blob: 73d65c121f27c1e2c1de2788acef43750bfa698a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# ==============================================================================
# Copyright (C) 2019 - Philip Paquette, Steven Bocco
#
#  This program is free software: you can redistribute it and/or modify it under
#  the terms of the GNU Affero General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option) any
#  later version.
#
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
#  details.
#
#  You should have received a copy of the GNU Affero General Public License along
#  with this program.  If not, see <https://www.gnu.org/licenses/>.
# ==============================================================================
""" Test Jsonable. """
import ujson as json

from diplomacy.utils import parsing
from diplomacy.utils.jsonable import Jsonable
from diplomacy.utils.sorted_dict import SortedDict
from diplomacy.utils.sorted_set import SortedSet

class MyJsonable(Jsonable):
    """ Example of class derived from Jsonable. """
    __slots__ = ('field_a', 'field_b', 'field_c', 'field_d', 'field_e', 'field_f', 'field_g')

    model = {
        'field_a': bool,
        'field_b': str,
        'field_c': parsing.OptionalValueType(float),
        'field_d': parsing.DefaultValueType(str, 'super'),
        'field_e': parsing.SequenceType(int),
        'field_f': parsing.SequenceType(float, sequence_builder=SortedSet.builder(float)),
        'field_g': parsing.DefaultValueType(parsing.DictType(str, int, SortedDict.builder(str, int)), {'x': -1})
    }

    def __init__(self, **kwargs):
        """ Constructor """
        self.field_a = None
        self.field_b = None
        self.field_c = None
        self.field_d = None
        self.field_e = None
        self.field_f = None
        self.field_g = {}
        super(MyJsonable, self).__init__(**kwargs)

def test_jsonable_parsing():
    """ Test parsing for Jsonable. """

    attributes = ('field_a', 'field_b', 'field_c', 'field_d', 'field_e', 'field_f', 'field_g')

    # Building and validating
    my_jsonable = MyJsonable(field_a=False, field_b='test', field_e={1}, field_f=[6.5])
    for attribute_name in attributes:
        assert hasattr(my_jsonable, attribute_name)
    assert isinstance(my_jsonable.field_a, bool)
    assert isinstance(my_jsonable.field_b, str)
    assert my_jsonable.field_c is None
    assert isinstance(my_jsonable.field_d, str), my_jsonable.field_d
    assert isinstance(my_jsonable.field_e, list)
    assert isinstance(my_jsonable.field_f, SortedSet)
    assert isinstance(my_jsonable.field_g, SortedDict)
    assert my_jsonable.field_d == 'super'
    assert my_jsonable.field_e == [1]
    assert my_jsonable.field_f == SortedSet(float, (6.5,))
    assert len(my_jsonable.field_g) == 1 and my_jsonable.field_g['x'] == -1

    # Building from its json representation and validating
    from_json = MyJsonable.from_dict(json.loads(json.dumps(my_jsonable.to_dict())))
    for attribute_name in attributes:
        assert hasattr(from_json, attribute_name), attribute_name
    assert from_json.field_a == my_jsonable.field_a
    assert from_json.field_b == my_jsonable.field_b
    assert from_json.field_c == my_jsonable.field_c
    assert from_json.field_d == my_jsonable.field_d
    assert from_json.field_e == my_jsonable.field_e
    assert from_json.field_f == my_jsonable.field_f
    assert from_json.field_g == my_jsonable.field_g