-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathtest_importstring.py
More file actions
65 lines (46 loc) · 1.66 KB
/
Copy pathtest_importstring.py
File metadata and controls
65 lines (46 loc) · 1.66 KB
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
65
"""Tests for IPython.utils.importstring."""
# -----------------------------------------------------------------------------
# Copyright (C) 2013 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# -----------------------------------------------------------------------------
import os
import os.path
import sys
import pytest
from IPython.utils.importstring import import_item
# -----------------------------------------------------------------------------
# Tests
# -----------------------------------------------------------------------------
def test_import_plain():
os2 = import_item("os")
assert os is os2
def test_import_nested():
path2 = import_item("os.path")
assert os.path is path2
def test_import_raises():
pytest.raises(ImportError, import_item, "IPython.foobar")
@pytest.mark.parametrize("name,expected", [
("os", os),
("sys", sys),
("os.path", os.path),
])
def test_import_returns_correct_object(name, expected):
assert import_item(name) is expected
@pytest.mark.parametrize("bad_name", [
"IPython.nonexistent_module",
"completely.fake.module",
"os.nonexistent_attribute",
])
def test_import_invalid_raises_importerror(bad_name):
with pytest.raises(ImportError):
import_item(bad_name)
def test_import_deep_nested():
from collections import abc
result = import_item("collections.abc")
assert result is abc
def test_import_result_is_callable_for_functions():
result = import_item("os.path.join")
assert callable(result)
assert result("a", "b") == os.path.join("a", "b")