StateMachineTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 44.07 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 26
loc 59
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_workflow_start() 13 13 2
A test_workflow_first_completed() 13 13 2
A setUp() 0 11 3
A test_workflow_invalid_input_abort() 0 6 1
A test_workflow_unknown_abort() 0 6 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
"""Unit tests for pydecider.state_machine
2
"""
3
4
import os
5
import sys
6
import unittest
7
8
sys.path.append(
9
    os.path.realpath(
10
        os.path.join(
11
            os.path.dirname(__file__),
12
            '..'
13
        )
14
    )
15
)
16
17
import copy
18
import mock
0 ignored issues
show
Unused Code introduced by
The import mock seems to be unused.
Loading history...
19
import yaml
0 ignored issues
show
Configuration introduced by
The import yaml could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
20
21
import pydecider
22
import pydecider.plan
23
import pydecider.state_machine
24
25
26
class StateMachineTest(unittest.TestCase):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
27
    MY_DIR = os.path.realpath(os.path.dirname(__file__))
28
29
    def setUp(self):
30
        # Load reference plan
31
        with open(os.path.join(self.MY_DIR, 'plan_hello.yml')) as f:
0 ignored issues
show
Coding Style Naming introduced by
The name f does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
32
            plan_data = yaml.load(f)
33
        self.plan = pydecider.plan.Plan.from_data(plan_data)
34
        # Load reference events
35
        with open(os.path.join(self.MY_DIR, 'plan_hello_events.yml')) as f:
0 ignored issues
show
Coding Style Naming introduced by
The name f does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
36
            events_data = yaml.load(f)
37
        self.events = events_data['events']
38
        # Create a state machine
39
        self.statemachine = pydecider.state_machine.StateMachine(self.plan)
40
41 View Code Duplication
    def test_workflow_start(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
42
        results = self.statemachine.eval(self.events[:3])
43
        self.assertTrue(self.statemachine.state.is_in_state('running'))
44
        self.assertEquals(
45
            [
46
                (result.name, result.activity, result.activity_input)
47
                 for result in results
48
            ],
49
            [
50
                (
51
                    'saying_hi',
52
                    self.plan.activities['HelloWorld'],
53
                    {'who': 'world'}
54
                )
55
            ]
56
        )
57
58
    def test_workflow_unknown_abort(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
59
        myevents = copy.deepcopy(self.events[:3])
60
        myevents[-1]['eventType'] = 'Foo'
61
        results = self.statemachine.eval(myevents[:3])
62
        self.assertTrue(self.statemachine.state.is_in_state('failed'))
63
        self.assertEquals([], results)
64
65
    def test_workflow_invalid_input_abort(self):
0 ignored issues
show
Coding Style Naming introduced by
The name test_workflow_invalid_input_abort does not conform to the method naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
66
        myevents = copy.deepcopy(self.events[:3])
67
        myevents[0]['eventType'] = 'Foo'
68
        results = self.statemachine.eval(myevents[:3])
69
        self.assertTrue(self.statemachine.state.is_in_state('failed'))
70
        self.assertEquals([], results)
71
72 View Code Duplication
    def test_workflow_first_completed(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
        results = self.statemachine.eval(self.events[:13])
74
        self.assertTrue(self.statemachine.state.is_in_state('running'))
75
        self.assertEquals(
76
            [
77
                (result.name, result.activity, result.activity_input)
78
                 for result in results
79
            ],
80
            [
81
                (
82
                    'saying_hi_again',
83
                    self.plan.activities['HelloWorld'],
84
                    {'who': 'world'}
85
                )
86
            ]
87
        )
88
89
90
if __name__ == '__main__':
91
    import logging
92
    logging.basicConfig(level=logging.DEBUG)
93
    unittest.main()
94