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