blob: faa76ffb7d78503f1fe4ad8da0b2366276678ffd [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13import unittest
14
15from tr069.models import DeviceIdStruct
16from spyne import ComplexModelBase
17
18
19class DeviceIdStructTests(unittest.TestCase):
20 def test_as_dict_memory_leak(self):
21 """
22 Test to ensure as_dict() doesn't leak model instances
23 """
24 thing = DeviceIdStruct(
25 Manufacturer='abc',
26 OUI='def',
27 ProductClass='ghi',
28 SerialNumber='jkl',
29 )
30 res = thing.as_dict()
31 self.assertEqual(
32 {
33 'Manufacturer': 'abc',
34 'OUI': 'def',
35 'ProductClass': 'ghi',
36 'SerialNumber': 'jkl',
37 },
38 res,
39 )
40 # inspect the spyne.util.memoize object that wraps the staticmethod
41 self.assertEqual(1, len(ComplexModelBase.get_flat_type_info.memo))
42
43 # should produce a different result and not grow the size of memo
44 thing.OUI = 'aaaa'
45 res = thing.as_dict()
46 self.assertEqual(
47 {
48 'Manufacturer': 'abc',
49 'OUI': 'aaaa',
50 'ProductClass': 'ghi',
51 'SerialNumber': 'jkl',
52 },
53 res,
54 )
55 self.assertEqual(1, len(ComplexModelBase.get_flat_type_info.memo))
56
57 # use a different object this time. Again should not grow memo
58 thing = DeviceIdStruct(
59 Manufacturer='abc',
60 OUI='def',
61 ProductClass='ghi',
62 SerialNumber='jkl',
63 )
64 res = thing.as_dict()
65 self.assertEqual(
66 {
67 'Manufacturer': 'abc',
68 'OUI': 'def',
69 'ProductClass': 'ghi',
70 'SerialNumber': 'jkl',
71 },
72 res,
73 )
74 self.assertEqual(1, len(ComplexModelBase.get_flat_type_info.memo))