Debian Codegraph @

test/test_importer.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # -*- coding: utf-8 -*-
2 """
3 Tests for behave.importing.
4 The module provides a lazy-loading/importing mechanism.
5 """
6
7 from __future__ import absolute_import
8 from behave.importer import LazyObject, LazyDict, load_module, parse_scoped_name
9 from behave.formatter.base import Formatter
10 from nose.tools import eq_, assert_raises
11 import sys
12 import types
13 # import unittest
14
15
16 class TestTheory(object): pass
17 class ImportModuleTheory(TestTheory):
18 """
19 Provides a test theory for importing modules.
20 """
21
22 @classmethod
23 def ensure_module_is_not_imported(cls, module_name):
24 if module_name in sys.modules:
25 del sys.modules[module_name]
26 cls.assert_module_is_not_imported(module_name)
27
28 @staticmethod
29 def assert_module_is_imported(module_name):
30 module = sys.modules.get(module_name, None)
31 assert module_name in sys.modules
32 assert module is not None
33
34 @staticmethod
35 def assert_module_is_not_imported(module_name):
36 assert module_name not in sys.modules
37
38 @staticmethod
39 def assert_module_with_name(module, name):
40 assert isinstance(module, types.ModuleType)
41 eq_(module.__name__, name)
42
43
44 class TestLoadModule(object):
45 theory = ImportModuleTheory
46
47 def test_load_module__should_fail_for_unknown_module(self):
48 assert_raises(ImportError, load_module, "__unknown_module__")
49
50 def test_load_module__should_succeed_for_already_imported_module(self):
51 module_name = "behave.importer"
52 self.theory.assert_module_is_imported(module_name)
53
54 module = load_module(module_name)
55 self.theory.assert_module_with_name(module, module_name)
56 self.theory.assert_module_is_imported(module_name)
57
58 def test_load_module__should_succeed_for_existing_module(self):
59 module_name = "test._importer_candidate"
60 self.theory.ensure_module_is_not_imported(module_name)
61
62 module = load_module(module_name)
63 self.theory.assert_module_with_name(module, module_name)
64 self.theory.assert_module_is_imported(module_name)
65
66 class TestLazyObject(object):
67
68 def test_get__should_succeed_for_known_object(self):
69 lazy = LazyObject("behave.importer", "LazyObject")
70 value = lazy.get()
71 assert value is LazyObject
72
73 lazy2 = LazyObject("behave.importer:LazyObject")
74 value2 = lazy2.get()
75 assert value2 is LazyObject
76
77 lazy3 = LazyObject("behave.formatter.steps", "StepsFormatter")
78 value3 = lazy3.get()
79 assert issubclass(value3, Formatter)
80
81 def test_get__should_fail_for_unknown_module(self):
82 lazy = LazyObject("__unknown_module__", "xxx")
83 assert_raises(ImportError, lazy.get)
84
85 def test_get__should_fail_for_unknown_object_in_module(self):
86 lazy = LazyObject("test._importer_candidate", "xxx")
87 assert_raises(ImportError, lazy.get)
88
89
90 class LazyDictTheory(TestTheory):
91
92 @staticmethod
93 def safe_getitem(data, key):
94 return dict.__getitem__(data, key)
95
96 @classmethod
97 def assert_item_is_lazy(cls, data, key):
98 value = cls.safe_getitem(data, key)
99 cls.assert_is_lazy_object(value)
100
101 @classmethod
102 def assert_item_is_not_lazy(cls, data, key):
103 value = cls.safe_getitem(data, key)
104 cls.assert_is_not_lazy_object(value)
105
106 @staticmethod
107 def assert_is_lazy_object(obj):
108 assert isinstance(obj, LazyObject)
109
110 @staticmethod
111 def assert_is_not_lazy_object(obj):
112 assert not isinstance(obj, LazyObject)
113
114
115 class TestLazyDict(object):
116 theory = LazyDictTheory
117
118 def test_unknown_item_access__should_raise_keyerror(self):
119 lazy_dict = LazyDict({"alice": 42})
120 item_access = lambda key: lazy_dict[key]
121 assert_raises(KeyError, item_access, "unknown")
122
123 def test_plain_item_access__should_succeed(self):
124 theory = LazyDictTheory
125 lazy_dict = LazyDict({"alice": 42})
126 theory.assert_item_is_not_lazy(lazy_dict, "alice")
127
128 value = lazy_dict["alice"]
129 eq_(value, 42)
130
131 def test_lazy_item_access__should_load_object(self):
132 ImportModuleTheory.ensure_module_is_not_imported("inspect")
133 lazy_dict = LazyDict({"alice": LazyObject("inspect:ismodule")})
134 self.theory.assert_item_is_lazy(lazy_dict, "alice")
135 self.theory.assert_item_is_lazy(lazy_dict, "alice")
136
137 value = lazy_dict["alice"]
138 self.theory.assert_is_not_lazy_object(value)
139 self.theory.assert_item_is_not_lazy(lazy_dict, "alice")
140
141 def test_lazy_item_access__should_fail_with_unknown_module(self):
142 lazy_dict = LazyDict({"bob": LazyObject("__unknown_module__", "xxx")})
143 item_access = lambda key: lazy_dict[key]
144 assert_raises(ImportError, item_access, "bob")
145
146 def test_lazy_item_access__should_fail_with_unknown_object(self):
147 lazy_dict = LazyDict({
148 "bob": LazyObject("behave.importer", "XUnknown")
149 })
150 item_access = lambda key: lazy_dict[key]
151 assert_raises(ImportError, item_access, "bob")