| 1 |
import types |
| 2 |
import unittest |
| 3 |
import warnings |
| 4 |
|
| 5 |
from websockets.imports import * |
| 6 |
|
| 7 |
|
| 8 |
foo = object() |
| 9 |
|
| 10 |
bar = object() |
| 11 |
|
| 12 |
|
| 13 |
class ImportsTests(unittest.TestCase): |
| 14 |
def setUp(self): |
| 15 |
self.mod = types.ModuleType("tests.test_imports.test_alias") |
| 16 |
self.mod.__package__ = self.mod.__name__ |
| 17 |
|
| 18 |
def test_get_alias(self): |
| 19 |
lazy_import( |
| 20 |
vars(self.mod), |
| 21 |
aliases={"foo": "...test_imports"}, |
| 22 |
) |
| 23 |
|
| 24 |
self.assertEqual(self.mod.foo, foo) |
| 25 |
|
| 26 |
def test_get_deprecated_alias(self): |
| 27 |
lazy_import( |
| 28 |
vars(self.mod), |
| 29 |
deprecated_aliases={"bar": "...test_imports"}, |
| 30 |
) |
| 31 |
|
| 32 |
with warnings.catch_warnings(record=True) as recorded_warnings: |
| 33 |
warnings.simplefilter("always") |
| 34 |
self.assertEqual(self.mod.bar, bar) |
| 35 |
|
| 36 |
self.assertEqual(len(recorded_warnings), 1) |
| 37 |
warning = recorded_warnings[0].message |
| 38 |
self.assertEqual( |
| 39 |
str(warning), "tests.test_imports.test_alias.bar is deprecated" |
| 40 |
) |
| 41 |
self.assertEqual(type(warning), DeprecationWarning) |
| 42 |
|
| 43 |
def test_dir(self): |
| 44 |
lazy_import( |
| 45 |
vars(self.mod), |
| 46 |
aliases={"foo": "...test_imports"}, |
| 47 |
deprecated_aliases={"bar": "...test_imports"}, |
| 48 |
) |
| 49 |
|
| 50 |
self.assertEqual( |
| 51 |
[item for item in dir(self.mod) if not item[:2] == item[-2:] == "__"], |
| 52 |
["bar", "foo"], |
| 53 |
) |
| 54 |
|
| 55 |
def test_attribute_error(self): |
| 56 |
lazy_import(vars(self.mod)) |
| 57 |
|
| 58 |
with self.assertRaises(AttributeError) as raised: |
| 59 |
self.mod.foo |
| 60 |
|
| 61 |
self.assertEqual( |
| 62 |
str(raised.exception), |
| 63 |
"module 'tests.test_imports.test_alias' has no attribute 'foo'", |
| 64 |
) |