1 # Copyright (c) 2018 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from yardstick.common import exceptions
16 from yardstick.common.messaging import payloads
17 from yardstick.tests.unit import base as ut_base
20 class _DummyPayload(payloads.Payload):
21 REQUIRED_FIELDS = {'version', 'key1', 'key2'}
24 class PayloadTestCase(ut_base.BaseUnitTestCase):
27 payload = _DummyPayload(version=1, key1='value1', key2='value2')
28 self.assertEqual(1, payload.version)
29 self.assertEqual('value1', payload.key1)
30 self.assertEqual('value2', payload.key2)
31 self.assertEqual(3, len(payload._fields))
33 def test__init_missing_required_fields(self):
34 with self.assertRaises(exceptions.PayloadMissingAttributes):
35 _DummyPayload(key1='value1', key2='value2')
37 def test_obj_to_dict(self):
38 payload = _DummyPayload(version=1, key1='value1', key2='value2')
39 payload_dict = payload.obj_to_dict()
40 self.assertEqual({'version': 1, 'key1': 'value1', 'key2': 'value2'},
43 def test_dict_to_obj(self):
44 _dict = {'version': 2, 'key1': 'value100', 'key2': 'value200'}
45 payload = _DummyPayload.dict_to_obj(_dict)
46 self.assertEqual(set(_dict.keys()), payload._fields)