Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2tests/st2tests/mocks/sensor.py (2 issues)

1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
"""
17
Mock classes for use in pack testing.
18
"""
19
20
from __future__ import absolute_import
21
22
from logging import RootLogger
23
24
from mock import Mock
25
26
from st2common.models.api.trace import TraceContext
27
from st2reactor.container.sensor_wrapper import SensorService
28
from st2tests.mocks.datastore import MockDatastoreService
29
30
__all__ = [
31
    'MockSensorWrapper',
32
    'MockSensorService'
33
]
34
35
36
class MockSensorWrapper(object):
37
    def __init__(self, pack, class_name):
38
        self._pack = pack
39
        self._class_name = class_name
40
41
42
class MockSensorService(SensorService):
43
    """
44
    Mock SensorService for use in testing.
45
    """
46
47
    def __init__(self, sensor_wrapper):
48
        self._sensor_wrapper = sensor_wrapper
49
50
        # Holds a mock logger instance
51
        # We use a Mock class so use can assert logger was called with particular arguments
52
        self._logger = Mock(spec=RootLogger)
53
54
        # Holds a list of triggers which were dispatched
55
        self.dispatched_triggers = []
56
57
        self._datastore_service = MockDatastoreService(logger=self._logger,
58
                                                       pack_name=self._sensor_wrapper._pack,
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _pack was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
59
                                                       class_name=self._sensor_wrapper._class_name)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _class_name was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
60
61
    @property
62
    def datastore_service(self):
63
        return self._datastore_service
64
65
    def get_logger(self, name):
66
        """
67
        Return mock logger instance.
68
69
        Keep in mind that this method returns Mock class instance which means you can use all the
70
        usual Mock class methods to assert that a particular message has been logged / logger has
71
        been called with particular arguments.
72
        """
73
        return self._logger
74
75
    def dispatch(self, trigger, payload=None, trace_tag=None):
76
        trace_context = TraceContext(trace_tag=trace_tag) if trace_tag else None
77
        return self.dispatch_with_context(trigger=trigger, payload=payload,
78
                                          trace_context=trace_context)
79
80
    def dispatch_with_context(self, trigger, payload=None, trace_context=None):
81
        item = {
82
            'trigger': trigger,
83
            'payload': payload,
84
            'trace_context': trace_context
85
        }
86
        self.dispatched_triggers.append(item)
87
        return item
88