Scott Baker | d443ea7 | 2018-08-07 13:50:06 -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 |
Scott Baker | e7b55e4 | 2019-04-01 17:18:03 -0700 | [diff] [blame^] | 16 | import os |
| 17 | import sys |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 18 | from mock import patch, Mock, MagicMock |
| 19 | |
Scott Baker | e7b55e4 | 2019-04-01 17:18:03 -0700 | [diff] [blame^] | 20 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 21 | |
| 22 | # mocking XOS exception, as they're based in Django |
Scott Baker | e7b55e4 | 2019-04-01 17:18:03 -0700 | [diff] [blame^] | 23 | |
| 24 | |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 25 | class Exceptions: |
| 26 | XOSValidationError = Exception |
| 27 | XOSProgrammingError = Exception |
| 28 | XOSPermissionDenied = Exception |
| 29 | |
Scott Baker | e7b55e4 | 2019-04-01 17:18:03 -0700 | [diff] [blame^] | 30 | |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 31 | class XOS: |
| 32 | exceptions = Exceptions |
| 33 | |
Scott Baker | e7b55e4 | 2019-04-01 17:18:03 -0700 | [diff] [blame^] | 34 | |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 35 | class TestFabricCrossconnectModels(unittest.TestCase): |
| 36 | def setUp(self): |
| 37 | |
| 38 | self.sys_path_save = sys.path |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 39 | |
| 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 Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 48 | modules = { |
Scott Baker | 63afcc1 | 2019-02-01 15:41:46 -0800 | [diff] [blame] | 49 | 'xos': MagicMock(), |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 50 | '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 Baker | e7b55e4 | 2019-04-01 17:18:03 -0700 | [diff] [blame^] | 122 | |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 123 | if __name__ == '__main__': |
| 124 | unittest.main() |