|
| 1 | +import unittest |
| 2 | +# noinspection PyProtectedMember |
| 3 | +from dash import _configs |
| 4 | +from dash import exceptions as _exc |
| 5 | +import os |
| 6 | + |
| 7 | + |
| 8 | +class MyTestCase(unittest.TestCase): |
| 9 | + |
| 10 | + def setUp(self): |
| 11 | + environ = _configs.env_configs() |
| 12 | + |
| 13 | + for k in environ.keys(): |
| 14 | + if k in os.environ: |
| 15 | + os.environ.pop(k) |
| 16 | + |
| 17 | + def test_valid_pathname_prefix_init(self): |
| 18 | + _, routes, req = _configs.pathname_configs() |
| 19 | + |
| 20 | + self.assertEqual('/', routes) |
| 21 | + self.assertEqual('/', req) |
| 22 | + |
| 23 | + _, routes, req = _configs.pathname_configs( |
| 24 | + routes_pathname_prefix='/dash/') |
| 25 | + |
| 26 | + self.assertEqual('/dash/', req) |
| 27 | + |
| 28 | + _, routes, req = _configs.pathname_configs( |
| 29 | + requests_pathname_prefix='/my-dash-app/', |
| 30 | + ) |
| 31 | + |
| 32 | + self.assertEqual(routes, '/') |
| 33 | + self.assertEqual(req, '/my-dash-app/') |
| 34 | + |
| 35 | + _, routes, req = _configs.pathname_configs( |
| 36 | + routes_pathname_prefix='/dash/', |
| 37 | + requests_pathname_prefix='/my-dash-app/dash/' |
| 38 | + ) |
| 39 | + |
| 40 | + self.assertEqual('/dash/', routes) |
| 41 | + self.assertEqual('/my-dash-app/dash/', req) |
| 42 | + |
| 43 | + def test_invalid_pathname_prefix(self): |
| 44 | + with self.assertRaises(_exc.InvalidConfig) as context: |
| 45 | + _, _, _ = _configs.pathname_configs('/my-path', '/another-path') |
| 46 | + |
| 47 | + self.assertTrue('url_base_pathname' in str(context.exception)) |
| 48 | + |
| 49 | + with self.assertRaises(_exc.InvalidConfig) as context: |
| 50 | + _, _, _ = _configs.pathname_configs( |
| 51 | + url_base_pathname='/invalid', |
| 52 | + routes_pathname_prefix='/invalid') |
| 53 | + |
| 54 | + self.assertTrue(str(context.exception).split('.')[0] |
| 55 | + .endswith('`routes_pathname_prefix`')) |
| 56 | + |
| 57 | + with self.assertRaises(_exc.InvalidConfig) as context: |
| 58 | + _, _, _ = _configs.pathname_configs( |
| 59 | + url_base_pathname='/my-path', |
| 60 | + requests_pathname_prefix='/another-path') |
| 61 | + |
| 62 | + self.assertTrue(str(context.exception).split('.')[0] |
| 63 | + .endswith('`requests_pathname_prefix`')) |
| 64 | + |
| 65 | + with self.assertRaises(_exc.InvalidConfig) as context: |
| 66 | + _, _, _ = _configs.pathname_configs('my-path') |
| 67 | + |
| 68 | + self.assertTrue('start with `/`' in str(context.exception)) |
| 69 | + |
| 70 | + with self.assertRaises(_exc.InvalidConfig) as context: |
| 71 | + _, _, _ = _configs.pathname_configs('/my-path') |
| 72 | + |
| 73 | + self.assertTrue('end with `/`' in str(context.exception)) |
| 74 | + |
| 75 | + def test_pathname_prefix_from_environ_app_name(self): |
| 76 | + os.environ['DASH_APP_NAME'] = 'my-dash-app' |
| 77 | + _, routes, req = _configs.pathname_configs() |
| 78 | + self.assertEqual('/my-dash-app/', req) |
| 79 | + self.assertEqual('/', routes) |
| 80 | + |
| 81 | + def test_pathname_prefix_environ_routes(self): |
| 82 | + os.environ['DASH_ROUTES_PATHNAME_PREFIX'] = '/routes/' |
| 83 | + _, routes, req = _configs.pathname_configs() |
| 84 | + self.assertEqual('/routes/', routes) |
| 85 | + |
| 86 | + def test_pathname_prefix_environ_requests(self): |
| 87 | + os.environ['DASH_REQUESTS_PATHNAME_PREFIX'] = '/requests/' |
| 88 | + _, routes, req = _configs.pathname_configs() |
| 89 | + self.assertEqual('/requests/', req) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + unittest.main() |
0 commit comments