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
|
import pytest
from databases.importer import ImportFromStringError, import_from_string
def test_invalid_format():
with pytest.raises(ImportFromStringError) as exc_info:
import_from_string("example:")
expected = 'Import string "example:" must be in format "<module>:<attribute>".'
assert exc_info.match(expected)
def test_invalid_module():
with pytest.raises(ImportFromStringError) as exc_info:
import_from_string("module_does_not_exist:myattr")
expected = 'Could not import module "module_does_not_exist".'
assert exc_info.match(expected)
def test_invalid_attr():
with pytest.raises(ImportFromStringError) as exc_info:
import_from_string("tempfile:attr_does_not_exist")
expected = 'Attribute "attr_does_not_exist" not found in module "tempfile".'
assert exc_info.match(expected)
def test_internal_import_error():
with pytest.raises(ImportError):
import_from_string("tests.importer.raise_import_error:myattr")
def test_valid_import():
instance = import_from_string("tempfile:TemporaryFile")
from tempfile import TemporaryFile
assert instance == TemporaryFile
|