Completed
Pull Request — master (#167)
by
unknown
28s
created

TestSmartScript   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 38.46 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 30
loc 78
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_gpu_check() 0 17 3
A test_cpu_check() 0 17 3
A setUp() 0 3 1
B test_launch_job_check() 30 30 4
A tearDown() 0 2 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import unittest
2
from smartdispatch import smartdispatch_script
3
import subprocess
4
from mock import patch
5
import tempfile as tmp
6
import shutil
7
import six
8
import sys
9
10
class TestSmartScript(unittest.TestCase):
11
12
    def setUp(self):
13
        self._base_dir = tmp.mkdtemp()
14
        smartdispatch_script.LOGS_FOLDERNAME = self._base_dir
15
16
    def tearDown(self):
17
        shutil.rmtree(self._base_dir)
18
19
    def test_gpu_check(self):
20
21
        argv = ['-x', '-g', '2', '-G', '1', '-C', '1', '-q', 'random', '-t', '00:00:10' ,'launch', 'echo', 'testing123']
22
23
        # Test if the check fail
24
        with self.assertRaises(SystemExit) as context:
25
            smartdispatch_script.main(argv=argv)
26
27
        self.assertTrue(context.exception.code, 2)
28
29
        # Test if the test pass
30
        argv[2] = '0'
31
32
        try:
33
            smartdispatch_script.main(argv=argv)
34
        except SystemExit as e:
35
            self.fail("The command failed the check, but it was supposed to pass.")
36
37
38
    def test_cpu_check(self):
39
40
        argv = ['-x', '-c', '2', '-C', '1', '-G', '1', '-t', '00:00:10', '-q', 'random', 'launch', 'echo', 'testing123']
41
42
        # Test if the check fail
43
        with self.assertRaises(SystemExit) as context:
44
            smartdispatch_script.main(argv=argv)
45
46
        self.assertTrue(context.exception.code, 2)
47
48
        # Test if the test pass
49
        argv[2] = '1'
50
51
        try:
52
            smartdispatch_script.main(argv=argv)
53
        except SystemExit as e:
54
            self.fail("The command failed the check, but it was supposed to pass.")
55
56
57
58 View Code Duplication
    @patch('subprocess.check_output')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
59
    def test_launch_job_check(self, mock_check_output):
60
61
        argv = ['-t', '0:0:1', '-G', '1', '-C', '1', '-q', 'random', 'launch', 'echo', 'testing123']
62
63
        # Test if the test pass (i.e the script run normaly)
64
        mock_check_output.side_effect = None
65
        mock_check_output.return_value = ""
66
67
        try:
68
            smartdispatch_script.main(argv=argv)
69
        except SystemExit as e:
70
          self.fail("The launcher had no problem, but the script failed nonetheless.")
71
72
        mock_check_output.side_effect = subprocess.CalledProcessError(1, "echo blabla", "A wild error appeared!")
73
74
        # Test if the test fail.
75
        try:
76
            with self.assertRaises(SystemExit) as context:
77
                smartdispatch_script.main(argv=argv)
78
79
                self.assertTrue(context.exception.code, 2)
80
        
81
        except subprocess.CalledProcessError as e:
82
            # Rerasing the exception
83
            orig_exc_type, orig_exc_value, orig_exc_traceback = sys.exc_info()
84
            
85
            new_exc = Exception("smartdispatch_script.main() raised subprocess.CalledProcessError unexpectedly")
86
            new_exc.reraised = True
87
            six.reraise(type(new_exc), new_exc, orig_exc_traceback)
88