Completed
Pull Request — master (#145)
by
unknown
53s
created

test_set_running_command_as_pending()   A

Complexity

Conditions 3

Size

Total Lines 16

Duplication

Lines 32
Ratio 200 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
dl 32
loc 16
rs 9.4285
1
import os
2
import unittest
3
import tempfile as tmp
4
import shutil
5
6
from smartdispatch.command_manager import CommandManager
7
from nose.tools import assert_equal, assert_true
8
9
10
class CommandFilesTests(unittest.TestCase):
11
12
    def setUp(self):
13
        self._base_dir = tmp.mkdtemp()
14
        self.nb_commands = 3
15
        self.command1 = "1\n"
16
        self.command2 = "2\n"
17
        self.command3 = "3\n"
18
19
        command_filename = os.path.join(self._base_dir, "commands.txt")
20
21
        with open(command_filename, "w+") as commands_file:
22
            commands_file.write(self.command1 + self.command2 + self.command3)
23
24
        self.command_manager = CommandManager(command_filename)
25
26
    def tearDown(self):
27
        shutil.rmtree(self._base_dir)
28
29
    def test_set_commands_to_run(self):
30
        # SetUp
31
        commands = ["4", "5", "6"]
32
33
        # The function to test
34
        self.command_manager.set_commands_to_run(commands)
35
36
        # Test validation
37
        with open(self.command_manager._commands_filename, "r") as commands_file:
38
            assert_equal(commands_file.read(), self.command1 + self.command2 + self.command3 + "4\n5\n6\n")
39
40
        assert_true(not os.path.isfile(self.command_manager._running_commands_filename))
41
42
        assert_true(not os.path.isfile(self.command_manager._finished_commands_filename))
43
44
    def test_get_failed_commands(self):
45
        # Setup
46
        command = self.command_manager.get_command_to_run()
47
        self.command_manager.set_running_command_as_finished(command, 1)
48
49
        # The function to test
50
        failed_commands = self.command_manager.get_failed_commands()
51
52
        # Test validation
53
        assert_equal(len(failed_commands), 1)
54
        assert_equal(failed_commands[0], self.command1)
55
56
    def test_get_failed_commands_empty(self):
57
        # The function to test
58
        failed_commands = self.command_manager.get_failed_commands()
59
60
        # Test validation
61
        assert_equal(len(failed_commands), 0)
62
63
    def test_get_command_to_run(self):
64
        # The function to test
65
        command = self.command_manager.get_command_to_run()
66
67
        # Test validation
68
        assert_equal(command, self.command1.strip())
69
70
        with open(self.command_manager._commands_filename, "r") as commands_file:
71
            assert_equal(commands_file.read(), self.command2 + self.command3)
72
73
        with open(self.command_manager._running_commands_filename, "r") as running_commands_file:
74
            assert_equal(running_commands_file.read(), self.command1)
75
76
        assert_true(not os.path.isfile(self.command_manager._finished_commands_filename))
77
78
    def test_get_nb_commands_to_run(self):
79
        assert_equal(self.command_manager.get_nb_commands_to_run(), self.nb_commands)
80
81 View Code Duplication
    def test_set_running_command_as_finished(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
82
        # SetUp
83
        command = self.command_manager.get_command_to_run()
84
85
        # The function to test
86
        self.command_manager.set_running_command_as_finished(command)
87
88
        # Test validation
89
        with open(self.command_manager._commands_filename, "r") as commands_file:
90
            assert_equal(commands_file.read(), self.command2 + self.command3)
91
92
        with open(self.command_manager._running_commands_filename, "r") as running_commands_file:
93
            assert_equal(running_commands_file.read(), "")
94
95
        with open(self.command_manager._finished_commands_filename, "r") as finished_commands_file:
96
            assert_equal(finished_commands_file.read(), self.command1)
97
98
        assert_true(not os.path.isfile(self.command_manager._failed_commands_filename))
99
100 View Code Duplication
    def test_set_running_command_as_pending(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
101
        # SetUp
102
        command = self.command_manager.get_command_to_run()
103
104
        # The function to test
105
        self.command_manager.set_running_command_as_pending(command)
106
107
        # Test validation
108
        with open(self.command_manager._commands_filename, "r") as commands_file:
109
            assert_equal(commands_file.read(), self.command2 + self.command3 + self.command1)
110
111
        with open(self.command_manager._running_commands_filename, "r") as running_commands_file:
112
            assert_equal(running_commands_file.read(), "")
113
114
        assert_true(not os.path.isfile(self.command_manager._finished_commands_filename))
115
        assert_true(not os.path.isfile(self.command_manager._failed_commands_filename))
116
117
    def test_set_running_command_as_failed(self):
118
        # SetUp
119
        command = self.command_manager.get_command_to_run()
120
        error_code = 1
121
122
        # The function to test
123
        self.command_manager.set_running_command_as_finished(command, error_code)
124
125
        # Test validation
126
        with open(self.command_manager._commands_filename, "r") as commands_file:
127
            assert_equal(commands_file.read(), self.command2 + self.command3)
128
129
        with open(self.command_manager._running_commands_filename, "r") as running_commands_file:
130
            assert_equal(running_commands_file.read(), "")
131
132
        with open(self.command_manager._failed_commands_filename, "r") as failed_commands_file:
133
            assert_equal(failed_commands_file.read(), self.command1)
134
135
        assert_true(not os.path.isfile(self.command_manager._finished_commands_filename))
136
137
    def test_reset_running_commands(self):
138
        # SetUp
139
        self.command_manager.get_command_to_run()
140
141
        # The function to test
142
        self.command_manager.reset_running_commands()
143
144
        # Test validation
145
        with open(self.command_manager._commands_filename, "r") as commands_file:
146
            assert_equal(commands_file.read(), self.command1 + self.command2 + self.command3)
147
148
        with open(self.command_manager._running_commands_filename, "r") as running_commands_file:
149
            assert_equal(running_commands_file.read(), "")
150
151
        assert_true(not os.path.isfile(self.command_manager._finished_commands_filename))
152