File: late_imports_test.py

package info (click to toggle)
python-mockito 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 528 kB
  • sloc: python: 4,102; makefile: 206
file content (97 lines) | stat: -rw-r--r-- 2,584 bytes parent folder | download | duplicates (2)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

import pytest

from mockito.utils import get_obj, get_obj_attr_tuple

import sys


PY3 = sys.version_info >= (3,)


def foo():
    pass

class TestLateImports:

    def testOs(self):
        import os
        assert get_obj('os') is os

    def testOsPath(self):
        import os.path
        assert get_obj('os.path') is os.path

    def testOsPathExists(self):
        import os.path
        assert get_obj('os.path.exists') is os.path.exists

    def testOsPathWhatever(self):
        with pytest.raises(AttributeError) as exc:
            get_obj('os.path.whatever')

        assert str(exc.value) == "module 'os.path' has no attribute 'whatever'"

    def testOsPathExistsForever(self):
        with pytest.raises(AttributeError) as exc:
            get_obj('os.path.exists.forever')

        assert str(exc.value) == \
            "object 'os.path.exists' has no attribute 'forever'"

    def testOsPathExistsForeverAndEver(self):
        with pytest.raises(AttributeError) as exc:
            get_obj('os.path.exists.forever.and.ever')

        assert str(exc.value) == \
            "object 'os.path.exists' has no attribute 'forever'"

    def testUnknownMum(self):
        with pytest.raises(ImportError) as exc:
            assert get_obj('mum') is foo
        if PY3:
            assert str(exc.value) == "No module named 'mum'"
        else:
            assert str(exc.value) == "No module named mum"

    def testUnknownMumFoo(self):
        with pytest.raises(ImportError) as exc:
            assert get_obj('mum.foo') is foo
        if PY3:
            assert str(exc.value) == "No module named 'mum'"
        else:
            assert str(exc.value) == "No module named mum"

    def testReturnGivenObject(self):
        import os
        assert get_obj(os) == os
        assert get_obj(os.path) == os.path
        assert get_obj(2) == 2

    def testDisallowRelativeImports(self):
        with pytest.raises(TypeError):
            get_obj('.mum')

class TestReturnTuple:
    def testOs(self):
        with pytest.raises(TypeError):
            get_obj_attr_tuple('os')

    def testOsPath(self):
        import os
        assert get_obj_attr_tuple('os.path') == (os, 'path')

    def testOsPathExists(self):
        import os
        assert get_obj_attr_tuple('os.path.exists') == (os.path, 'exists')

    def testOsPathExistsNot(self):
        import os
        assert get_obj_attr_tuple('os.path.exists.not') == (
            os.path.exists, 'not')

    def testDisallowRelativeImports(self):
        with pytest.raises(TypeError):
            get_obj('.mum')