blob: 25f83edad63172214cf48d9e53d1cf1a5ca1a9f9 [file] [log] [blame]
Scott Bakerd443ea72018-08-07 13:50:06 -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
Scott Bakere7b55e42019-04-01 17:18:03 -070016import os
17import sys
Scott Bakerd443ea72018-08-07 13:50:06 -070018from mock import patch, Mock, MagicMock
19
Scott Bakere7b55e42019-04-01 17:18:03 -070020test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Scott Bakerd443ea72018-08-07 13:50:06 -070021
22# mocking XOS exception, as they're based in Django
Scott Bakere7b55e42019-04-01 17:18:03 -070023
24
Scott Bakerd443ea72018-08-07 13:50:06 -070025class Exceptions:
26 XOSValidationError = Exception
27 XOSProgrammingError = Exception
28 XOSPermissionDenied = Exception
29
Scott Bakere7b55e42019-04-01 17:18:03 -070030
Scott Bakerd443ea72018-08-07 13:50:06 -070031class XOS:
32 exceptions = Exceptions
33
Scott Bakere7b55e42019-04-01 17:18:03 -070034
Scott Bakerd443ea72018-08-07 13:50:06 -070035class TestFabricCrossconnectModels(unittest.TestCase):
36 def setUp(self):
37
38 self.sys_path_save = sys.path
Scott Bakerd443ea72018-08-07 13:50:06 -070039
40 self.xos = XOS
41
42 self.models_decl = Mock()
43 self.models_decl.BNGPortMapping_decl = MagicMock
44 self.models_decl.BNGPortMapping_decl.save = Mock()
45 self.models_decl.BNGPortMapping_decl.objects = Mock()
46 self.models_decl.BNGPortMapping_decl.objects.filter.return_value = []
47
Scott Bakerd443ea72018-08-07 13:50:06 -070048 modules = {
Scott Baker63afcc12019-02-01 15:41:46 -080049 'xos': MagicMock(),
Scott Bakerd443ea72018-08-07 13:50:06 -070050 'xos.exceptions': self.xos.exceptions,
51 'models_decl': self.models_decl
52 }
53
54 self.module_patcher = patch.dict('sys.modules', modules)
55 self.module_patcher.start()
56
57 self.volt = Mock()
58
59 from models import BNGPortMapping
60
61 self.BNGPortMapping = BNGPortMapping()
62
63 def tearDown(self):
64 sys.path = self.sys_path_save
65
66 def test_validate_range_single(self):
67 bpm = self.BNGPortMapping()
68 bpm.validate_range("123")
69
70 def test_validate_range_commas(self):
71 bpm = self.BNGPortMapping()
72 bpm.validate_range("123, 456")
73
74 def test_validate_range_ANY(self):
75 bpm = self.BNGPortMapping()
76 bpm.validate_range("ANY")
77 bpm.validate_range("any")
78
79 def test_validate_range_dash(self):
80 bpm = self.BNGPortMapping()
81 bpm.validate_range("123-456")
82
83 def test_validate_dash_commas(self):
84 bpm = self.BNGPortMapping()
85 bpm.validate_range("123-456, 789 - 1000")
86
87 def test_validate_range_empty(self):
88 bpm = self.BNGPortMapping()
89 with self.assertRaises(Exception) as e:
90 bpm.validate_range("")
91
92 self.assertEqual(e.exception.message, 'Malformed range ')
93
94 def test_validate_range_none(self):
95 bpm = self.BNGPortMapping()
96 with self.assertRaises(Exception) as e:
97 bpm.validate_range("")
98
99 self.assertEqual(e.exception.message, 'Malformed range ')
100
101 def test_validate_range_all(self):
102 bpm = self.BNGPortMapping()
103 with self.assertRaises(Exception) as e:
104 bpm.validate_range("badstring")
105
106 self.assertEqual(e.exception.message, 'Malformed range badstring')
107
108 def test_validate_half_range(self):
109 bpm = self.BNGPortMapping()
110 with self.assertRaises(Exception) as e:
111 bpm.validate_range("123-")
112
113 self.assertEqual(e.exception.message, 'Malformed range 123-')
114
115 def test_validate_half_comma(self):
116 bpm = self.BNGPortMapping()
117 with self.assertRaises(Exception) as e:
118 bpm.validate_range("123,")
119
120 self.assertEqual(e.exception.message, 'Malformed range 123,')
121
Scott Bakere7b55e42019-04-01 17:18:03 -0700122
Scott Bakerd443ea72018-08-07 13:50:06 -0700123if __name__ == '__main__':
124 unittest.main()