Completed
Push — master ( 2ae69e...39840a )
by Vincent
01:14
created

ExecutionTestCase.test_print_several_times()   A

Complexity

Conditions 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
1
# coding: utf8
2
3
# Copyright 2013-2015 Vincent Jacques <[email protected]>
4
5
from __future__ import division, absolute_import, print_function
6
7
import datetime
8
9
from ActionTree import *
10
from . import *
11
12
13
class TestHooks(Hooks):
14
    def __init__(self):
15
        self.events = []
16
17
    def action_pending(self, time, action):
18
        assert isinstance(time, datetime.datetime)
19
        self.events.append(("pending", action.label))
20
21
    def action_ready(self, time, action):
22
        assert isinstance(time, datetime.datetime)
23
        self.events.append(("ready", action.label))
24
25
    def action_canceled(self, time, action):
26
        assert isinstance(time, datetime.datetime)
27
        self.events.append(("canceled", action.label))
28
29
    def action_started(self, time, action):
30
        assert isinstance(time, datetime.datetime)
31
        self.events.append(("started", action.label))
32
33
    def action_printed(self, time, action, text):
34
        assert isinstance(time, datetime.datetime)
35
        self.events.append(("printed", action.label, text))
36
37
    def action_successful(self, time, action):
38
        assert isinstance(time, datetime.datetime)
39
        self.events.append(("successful", action.label))
40
41
    def action_failed(self, time, action):
42
        assert isinstance(time, datetime.datetime)
43
        self.events.append(("failed", action.label))
44
45
46
class ExecutionTestCase(ActionTreeTestCase):
47
    def test_successful_action(self):
48
        hooks = TestHooks()
49
        execute(self._action("a"), hooks=hooks)
50
51
        self.assertEqual(
52
            hooks.events,
53
            [
54
                ("pending", "a"),
55
                ("ready", "a"),
56
                ("started", "a"),
57
                ("successful", "a"),
58
            ]
59
        )
60
61
    def test_failed_action(self):
62
        hooks = TestHooks()
63
        execute(self._action("a", exception=Exception()), do_raise=False, hooks=hooks)
64
65
        self.assertEqual(
66
            hooks.events,
67
            [
68
                ("pending", "a"),
69
                ("ready", "a"),
70
                ("started", "a"),
71
                ("failed", "a"),
72
            ]
73
        )
74
75
    def test_failed_dependency(self):
76
        hooks = TestHooks()
77
        a = self._action("a")
78
        b = self._action("b", exception=Exception())
79
        a.add_dependency(b)
80
        execute(a, do_raise=False, hooks=hooks)
81
82
        self.assertEqual(sorted(hooks.events[:2]), [("pending", "a"), ("pending", "b")])
83
        self.assertEqual(
84
            hooks.events[2:],
85
            [
86
                ("ready", "b"),
87
                ("started", "b"),
88
                ("failed", "b"),
89
                ("canceled", "a"),
90
            ]
91
        )
92
93
    def test_successful_action_print(self):
94
        hooks = TestHooks()
95
        execute(self._action("a", print_on_stdout="something"), hooks=hooks)
96
97
        self.assertEqual(
98
            hooks.events,
99
            [
100
                ("pending", "a"),
101
                ("ready", "a"),
102
                ("started", "a"),
103
                ("printed", "a", b"something\n"),
104
                ("successful", "a"),
105
            ]
106
        )
107
108
    def test_failed_action_print(self):
109
        hooks = TestHooks()
110
        execute(self._action("a", print_on_stdout="something", exception=Exception()), do_raise=False, hooks=hooks)
111
112
        self.assertEqual(
113
            hooks.events,
114
            [
115
                ("pending", "a"),
116
                ("ready", "a"),
117
                ("started", "a"),
118
                ("printed", "a", b"something\n"),
119
                ("failed", "a"),
120
            ]
121
        )
122
123
    def test_print_several_times(self):
124
        hooks = TestHooks()
125
        a = self._action("a", print_on_stdout=[("something 1", 0.1), ("something 2", 0.1), ("something 3", 0.1)])
126
        execute(a, hooks=hooks)
127
128
        self.assertEqual(
129
            hooks.events,
130
            [
131
                ("pending", "a"),
132
                ("ready", "a"),
133
                ("started", "a"),
134
                ("printed", "a", b"something 1\n"),
135
                ("printed", "a", b"something 2\n"),
136
                ("printed", "a", b"something 3\n"),
137
                ("successful", "a"),
138
            ]
139
        )
140