blob: 9abe764a4e6edad37db51ce4732d3f8f0c7e32bd [file] [log] [blame]
Wei-Yu Chenad55cb82022-02-15 20:07:01 +08001# SPDX-FileCopyrightText: 2020 The Magma Authors.
2# SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org>
3#
4# SPDX-License-Identifier: BSD-3-Clause
Wei-Yu Chen49950b92021-11-08 19:19:18 +08005
Wei-Yu Chen49950b92021-11-08 19:19:18 +08006import unittest
7
8from tr069.models import DeviceIdStruct
9from spyne import ComplexModelBase
10
11
12class DeviceIdStructTests(unittest.TestCase):
13 def test_as_dict_memory_leak(self):
14 """
15 Test to ensure as_dict() doesn't leak model instances
16 """
17 thing = DeviceIdStruct(
18 Manufacturer='abc',
19 OUI='def',
20 ProductClass='ghi',
21 SerialNumber='jkl',
22 )
23 res = thing.as_dict()
24 self.assertEqual(
25 {
26 'Manufacturer': 'abc',
27 'OUI': 'def',
28 'ProductClass': 'ghi',
29 'SerialNumber': 'jkl',
30 },
31 res,
32 )
33 # inspect the spyne.util.memoize object that wraps the staticmethod
34 self.assertEqual(1, len(ComplexModelBase.get_flat_type_info.memo))
35
36 # should produce a different result and not grow the size of memo
37 thing.OUI = 'aaaa'
38 res = thing.as_dict()
39 self.assertEqual(
40 {
41 'Manufacturer': 'abc',
42 'OUI': 'aaaa',
43 'ProductClass': 'ghi',
44 'SerialNumber': 'jkl',
45 },
46 res,
47 )
48 self.assertEqual(1, len(ComplexModelBase.get_flat_type_info.memo))
49
50 # use a different object this time. Again should not grow memo
51 thing = DeviceIdStruct(
52 Manufacturer='abc',
53 OUI='def',
54 ProductClass='ghi',
55 SerialNumber='jkl',
56 )
57 res = thing.as_dict()
58 self.assertEqual(
59 {
60 'Manufacturer': 'abc',
61 'OUI': 'def',
62 'ProductClass': 'ghi',
63 'SerialNumber': 'jkl',
64 },
65 res,
66 )
67 self.assertEqual(1, len(ComplexModelBase.get_flat_type_info.memo))