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