Wei-Yu Chen | ad55cb8 | 2022-02-15 20:07:01 +0800 | [diff] [blame] | 1 | # 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 Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 5 | |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 6 | import unittest |
| 7 | |
| 8 | from tr069.models import DeviceIdStruct |
| 9 | from spyne import ComplexModelBase |
| 10 | |
| 11 | |
| 12 | class 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)) |