File: test__context_managers.py

package info (click to toggle)
python-pyforge 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: python: 3,666; makefile: 12; sh: 7
file content (91 lines) | stat: -rw-r--r-- 3,644 bytes parent folder | download
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
import types
from .ut_utils import ForgeTestCase
from forge import UnexpectedCall
from forge import UnexpectedSetattr

class ContextManager(object):
    def __enter__(self):
        raise NotImplementedError()
    def __exit__(self, t, v, tb):
        raise NotImplementedError()

class ContextManagerTest(ForgeTestCase):
    def setUp(self):
        super(ContextManagerTest, self).setUp()
        self.obj = self.forge.create_mock(ContextManager)
        self.checkpoint = self.forge.create_function_stub(lambda: None)
    def tearDown(self):
        self.assertTrue(self.forge.queue.is_empty())
        self.forge.verify()
        super(ContextManagerTest, self).tearDown()
    def test__expecting_context(self):
        with self.obj:
            self.checkpoint()
        self.forge.replay()
        with self.obj:
            self.checkpoint()
    def test__expecting_context_with_exception_inside(self):
        with self.obj:
            self.checkpoint()
        my_exception = Exception()
        self.forge.replay()
        with self.assertRaises(UnexpectedCall) as caught:
            with self.obj:
                raise my_exception
        caught = caught.exception
        self.assertEqual(len(caught.expected), 1)
        self.assertIs(caught.expected[0].target, self.checkpoint)
        self.assertIsSameMethod(caught.got.target.__forge__.original,
                                ContextManager.__exit__)
        self.assertIs(caught.got.args['t'], Exception)
        self.assertIs(caught.got.args['v'], my_exception)
        self.assertIsNotNone(caught.got.args['tb'])
        self.assertEqual(len(self.forge.queue), 2)
        self.forge.reset()
    def test__expecting_context_with_unexpected_call_inside(self):
        stub = self.forge.create_function_stub(lambda arg: None)
        with self.obj:
            stub(1)
        self.forge.replay()
        with self.assertRaises(UnexpectedCall) as caught:
            with self.obj:
                stub(2)
        caught = caught.exception
        self.assertEqual(len(caught.expected), 1)
        self.assertIs(caught.expected[0].target, stub)
        self.assertIs(caught.got.target, stub)
        self.assertIs(caught.expected[0].args['arg'], 1)
        self.assertIs(caught.got.args['arg'], 2)
        self.assertEqual(len(self.forge.queue), 2)
        self.forge.reset()
    def test__expecting_context_with_unexpected_setattr_inside(self):
        with self.obj:
            pass
        self.forge.replay()
        with self.assertRaises(UnexpectedSetattr) as caught:
            with self.obj:
                self.obj.a = 2
        caught = caught.exception
        self.assertEqual(len(caught.expected), 1)
        self.assertIs(caught.expected[0].target, self.obj.__forge__.get_attribute('__exit__'))
        self.assertEqual(len(caught.expected[0].args), 3)
        self.assertTrue(all(x is None for x in caught.expected[0].args.values()))
        self.assertIs(caught.got.target, self.obj)
        self.assertEqual(caught.got.name, 'a')
        self.assertEqual(caught.got.value, 2)
        self.assertEqual(len(self.forge.queue), 1)
        self.forge.reset()
    def test__enter_returning_value(self):
        self.obj.__enter__().and_return(2)
        self.checkpoint()
        self.obj.__exit__(None, None, None)
        self.forge.replay()
        with self.obj as value:
            self.checkpoint()
        self.assertEqual(value, 2)
    def assertIsSameMethod(self, a, b):
        if not isinstance(a, types.FunctionType):
            a = a.__func__
        if not isinstance(b, types.FunctionType):
            b = b.__func__
        return a is b