Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 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 |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 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. |
| 14 | |
| 15 | import unittest |
| 16 | from mock import patch, Mock, MagicMock |
| 17 | |
| 18 | # mocking XOS exception, as they're based in Django |
| 19 | class Exceptions: |
| 20 | XOSValidationError = Exception |
| 21 | |
| 22 | class XOS: |
| 23 | exceptions = Exceptions |
| 24 | |
| 25 | class TestOLTDeviceModel(unittest.TestCase): |
| 26 | def setUp(self): |
| 27 | self.xos = XOS |
| 28 | |
| 29 | self.models_decl = Mock() |
| 30 | self.models_decl.OLTDevice_decl = MagicMock |
| 31 | self.models_decl.OLTDevice_decl.delete = Mock() |
| 32 | |
| 33 | modules = { |
| 34 | 'xos.exceptions': self.xos.exceptions, |
| 35 | 'models_decl': self.models_decl, |
| 36 | } |
| 37 | |
| 38 | self.module_patcher = patch.dict('sys.modules', modules) |
| 39 | self.module_patcher.start() |
| 40 | |
| 41 | from models import OLTDevice |
| 42 | |
| 43 | print OLTDevice |
| 44 | |
| 45 | self.olt_device = OLTDevice() |
| 46 | self.olt_device.id = None # this is a new model |
| 47 | self.olt_device.is_new = True |
| 48 | self.olt_device.device_id = 1234 |
| 49 | |
| 50 | |
| 51 | def test_delete(self): |
| 52 | self.olt_device.delete() |
| 53 | self.models_decl.OLTDevice_decl.delete.assert_called() |
| 54 | |
| 55 | def test_prevent_delete(self): |
| 56 | |
| 57 | onu1 = Mock() |
| 58 | onu1.id = 1 |
| 59 | |
| 60 | pon1 = Mock() |
| 61 | pon1.onu_devices.all.return_value = [onu1] |
| 62 | |
| 63 | self.olt_device.pon_ports.all.return_value = [pon1] |
| 64 | |
| 65 | volt_si_1 = Mock() |
| 66 | volt_si_1.onu_device_id = onu1.id |
| 67 | |
| 68 | with patch.object(self.olt_device, "get_volt_si")as volt_si_get: |
| 69 | volt_si_get.return_value = [volt_si_1] |
| 70 | with self.assertRaises(Exception) as e: |
| 71 | self.olt_device.delete() |
| 72 | |
| 73 | self.assertEqual(e.exception.message, 'OLT "1234" can\'t be deleted as it has subscribers associated with its ONUs') |
| 74 | self.models_decl.OLTDevice_decl.delete.assert_not_called() |
| 75 | |
| 76 | if __name__ == '__main__': |
| 77 | unittest.main() |