1#!/usr/bin/env python3
2# Copyright (c) Facebook, Inc. and its affiliates.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import unittest
17
18import convertible.ttypes as ttypes
19import convertible.types as types
20from thrift.util.converter import to_py_struct
21
22
23class ConverterTest(unittest.TestCase):
24    def test_simple(self) -> None:
25        simple = to_py_struct(
26            ttypes.Simple,
27            types.Simple(
28                intField=42,
29                strField="simple",
30                intList=[1, 2, 3],
31                strSet={"hello", "world"},
32                strToIntMap={"one": 1, "two": 2},
33                color=types.Color.GREEN,
34                name_="renamed",
35            ),
36        )
37        self.assertEqual(simple.intField, 42)
38        self.assertEqual(simple.strField, "simple")
39        self.assertEqual(simple.intList, [1, 2, 3])
40        self.assertEqual(simple.strSet, {"hello", "world"})
41        self.assertEqual(simple.strToIntMap, {"one": 1, "two": 2})
42        self.assertEqual(simple.color, ttypes.Color.GREEN)
43        self.assertEqual(simple.name, "renamed")
44
45    def test_nested(self) -> None:
46        nested = to_py_struct(
47            ttypes.Nested,
48            types.Nested(
49                simpleField=types.Simple(
50                    intField=42,
51                    strField="simple",
52                    intList=[1, 2, 3],
53                    strSet={"hello", "world"},
54                    strToIntMap={"one": 1, "two": 2},
55                    color=types.Color.NONE,
56                ),
57                simpleList=[
58                    types.Simple(
59                        intField=200,
60                        strField="face",
61                        intList=[4, 5, 6],
62                        strSet={"keep", "calm"},
63                        strToIntMap={"three": 3, "four": 4},
64                        color=types.Color.RED,
65                    ),
66                    types.Simple(
67                        intField=404,
68                        strField="b00k",
69                        intList=[7, 8, 9],
70                        strSet={"carry", "on"},
71                        strToIntMap={"five": 5, "six": 6},
72                        color=types.Color.GREEN,
73                    ),
74                ],
75                colorToSimpleMap={
76                    types.Color.BLUE: types.Simple(
77                        intField=500,
78                        strField="internal",
79                        intList=[10],
80                        strSet={"server", "error"},
81                        strToIntMap={"seven": 7, "eight": 8, "nine": 9},
82                        color=types.Color.BLUE,
83                    )
84                },
85            ),
86        )
87        self.assertEqual(nested.simpleField.intField, 42)
88        self.assertEqual(nested.simpleList[0].intList, [4, 5, 6])
89        self.assertEqual(nested.simpleList[1].strSet, {"carry", "on"})
90        self.assertEqual(
91            nested.colorToSimpleMap[ttypes.Color.BLUE].color, ttypes.Color.BLUE
92        )
93
94    def test_simple_union(self) -> None:
95        simple_union = to_py_struct(ttypes.Union, types.Union(intField=42))
96        self.assertEqual(simple_union.get_intField(), 42)
97
98    def test_union_with_containers(self) -> None:
99        union_with_list = to_py_struct(ttypes.Union, types.Union(intList=[1, 2, 3]))
100        self.assertEqual(union_with_list.get_intList(), [1, 2, 3])
101
102    def test_complex_union(self) -> None:
103        complex_union = to_py_struct(
104            ttypes.Union,
105            types.Union(
106                simple_=types.Simple(
107                    intField=42,
108                    strField="simple",
109                    intList=[1, 2, 3],
110                    strSet={"hello", "world"},
111                    strToIntMap={"one": 1, "two": 2},
112                    color=types.Color.NONE,
113                    name_="renamed",
114                )
115            ),
116        )
117        self.assertEqual(complex_union.get_simpleField().intField, 42)
118        self.assertEqual(complex_union.get_simpleField().name, "renamed")
119