4 # Copyright 2016 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
29 from rift
.package
.convert
import (
30 ProtoMessageSerializer
,
31 UnknownExtensionError
,
36 gi
.require_version('RwpersonDbYang', '1.0')
37 gi
.require_version('RwProjectPersonDbYang', '1.0')
38 gi
.require_version('RwYang', '1.0')
40 from gi
.repository
import (
42 RwProjectPersonDbYang
,
46 from rift
.package
.convert
import SerializationError
49 class TestSerializer(unittest
.TestCase
):
51 self
._serializer
= ProtoMessageSerializer(
53 RwpersonDbYang
.Person
,
54 RwProjectPersonDbYang
,
55 RwProjectPersonDbYang
.YangData_RwProject_Project_Person
,
58 self
._sample
_person
= RwpersonDbYang
.Person(name
="Fred")
59 self
._project
_person
= RwProjectPersonDbYang
.YangData_RwProject_Project_Person(name
="Fred")
60 self
._model
= RwYang
.model_create_libyang()
61 self
._model
.load_schema_ypbc(RwpersonDbYang
.get_schema())
63 def test_from_xml_file(self
):
64 sample_person_xml
= self
._sample
_person
.to_xml_v2(self
._model
)
65 with io
.StringIO(sample_person_xml
) as file_hdl
:
66 person
= self
._serializer
.from_file_hdl(file_hdl
, ".xml")
67 self
.assertEqual(person
, self
._sample
_person
)
69 def test_from_yaml_file(self
):
70 sample_person_yaml
= self
._sample
_person
.to_yaml(self
._model
)
71 with io
.StringIO(sample_person_yaml
) as file_hdl
:
73 person
= self
._serializer
.from_file_hdl(file_hdl
, ".yml")
74 self
.assertEqual(person
, self
._project
_person
)
76 def test_from_json_file(self
):
77 sample_person_json
= self
._sample
_person
.to_json(self
._model
)
78 with io
.StringIO(sample_person_json
) as file_hdl
:
80 person
= self
._serializer
.from_file_hdl(file_hdl
, ".json")
81 self
.assertEqual(person
, self
._project
_person
)
83 def test_unknown_file_extension(self
):
84 with io
.StringIO("asdf") as file_hdl
:
85 with self
.assertRaises(UnknownExtensionError
):
86 self
._serializer
.from_file_hdl(file_hdl
, ".foo")
88 def test_raises_serialization_error(self
):
89 with io
.StringIO('</foo>') as file_hdl
:
90 with self
.assertRaises(SerializationError
):
91 person
= self
._serializer
.from_file_hdl(file_hdl
, ".json")
94 def test_to_json_string(self
):
95 json_str
= self
._serializer
.to_json_string(self
._sample
_person
)
97 person
= RwpersonDbYang
.Person
.from_json(self
._model
, json_str
)
98 self
.assertEqual(person
, self
._sample
_person
)
100 def test_to_json_string_invalid_type(self
):
101 with self
.assertRaises(SerializationError
):
102 self
._serializer
.to_json_string(RwpersonDbYang
.FlatPerson(name
="bob"))
105 def main(argv
=sys
.argv
[1:]):
106 logging
.basicConfig(format
='TEST %(message)s')
108 runner
= xmlrunner
.XMLTestRunner(output
=os
.environ
["RIFT_MODULE_TEST"])
109 parser
= argparse
.ArgumentParser()
110 parser
.add_argument('-v', '--verbose', action
='store_true')
111 parser
.add_argument('-n', '--no-runner', action
='store_true')
113 args
, unknown
= parser
.parse_known_args(argv
)
117 # Set the global logging level
118 logging
.getLogger().setLevel(logging
.DEBUG
if args
.verbose
else logging
.ERROR
)
120 # The unittest framework requires a program name, so use the name of this
121 # file instead (we do not want to have to pass a fake program name to main
122 # when this is called from the interpreter).
123 unittest
.main(argv
=[__file__
] + unknown
+ ["-v"], testRunner
=runner
)
125 if __name__
== '__main__':