blob: 5da95834c73b8d88df6a2ef23978fbae5c078e27 [file] [log] [blame]
Matteo Scandolo2360fd92018-05-29 17:27:51 -07001# 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
15import unittest
16from mock import patch, Mock, MagicMock
17
18# mocking XOS exception, as they're based in Django
19class Exceptions:
20 XOSValidationError = Exception
21
22class XOS:
23 exceptions = Exceptions
24
25class 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
Matteo Scandolo2360fd92018-05-29 17:27:51 -070043 self.olt_device = OLTDevice()
44 self.olt_device.id = None # this is a new model
45 self.olt_device.is_new = True
46 self.olt_device.device_id = 1234
47
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070048 def test_create_mac_address(self):
49 from models import OLTDevice
50 olt = OLTDevice()
51
52 olt.host = "1.1.1.1"
53 olt.port = "9101"
54 olt.mac_address = "00:0c:d5:00:05:40"
55
56 with self.assertRaises(Exception) as e:
57 olt.save()
58
59 self.assertEqual(e.exception.message,
60 "You can't specify both host/port and mac_address for OLTDevice [host=%s, port=%s, mac_address=%s]" % (olt.host, olt.port, olt.mac_address))
Matteo Scandolo2360fd92018-05-29 17:27:51 -070061
62 def test_delete(self):
63 self.olt_device.delete()
64 self.models_decl.OLTDevice_decl.delete.assert_called()
65
66 def test_prevent_delete(self):
67
68 onu1 = Mock()
69 onu1.id = 1
70
71 pon1 = Mock()
72 pon1.onu_devices.all.return_value = [onu1]
73
74 self.olt_device.pon_ports.all.return_value = [pon1]
75
76 volt_si_1 = Mock()
77 volt_si_1.onu_device_id = onu1.id
78
79 with patch.object(self.olt_device, "get_volt_si")as volt_si_get:
80 volt_si_get.return_value = [volt_si_1]
81 with self.assertRaises(Exception) as e:
82 self.olt_device.delete()
83
84 self.assertEqual(e.exception.message, 'OLT "1234" can\'t be deleted as it has subscribers associated with its ONUs')
85 self.models_decl.OLTDevice_decl.delete.assert_not_called()
86
87if __name__ == '__main__':
88 unittest.main()