Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame^] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | import unittest |
| 14 | |
| 15 | from tr069.models import DeviceIdStruct |
| 16 | from spyne import ComplexModelBase |
| 17 | |
| 18 | |
| 19 | class 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)) |