| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from mistral_lib import actions |
| 14 |
from mistral_lib.actions.providers import python |
| 15 |
from mistral_lib.tests import base as tests_base |
| 16 |
|
| 17 |
|
| 18 |
class HelloAction(actions.Action): |
| 19 |
"""I help with testing.""" |
| 20 |
|
| 21 |
def __init__(self, f_name, l_name): |
| 22 |
super(HelloAction, self).__init__() |
| 23 |
|
| 24 |
self._f_name = f_name |
| 25 |
self._l_name = l_name |
| 26 |
|
| 27 |
def run(self, context): |
| 28 |
return 'Hello %s %s!' % (self._f_name, self._l_name) |
| 29 |
|
| 30 |
|
| 31 |
class TestActionProvider(actions.ActionProvider): |
| 32 |
def __init__(self, name): |
| 33 |
super(TestActionProvider, self).__init__(name) |
| 34 |
|
| 35 |
self.action_descs = {} |
| 36 |
|
| 37 |
def add_action_descriptor(self, action_desc): |
| 38 |
self.action_descs[action_desc.name] = action_desc |
| 39 |
|
| 40 |
def find(self, action_name, namespace=None): |
| 41 |
return self.action_descs.get(action_name) |
| 42 |
|
| 43 |
def find_all(self, namespace=None, limit=None, sort_fields=None, |
| 44 |
sort_dirs=None, **filters): |
| 45 |
return self.action_descs.values() |
| 46 |
|
| 47 |
|
| 48 |
class TestActionProviders(tests_base.TestCase): |
| 49 |
def test_python_action_descriptor(self): |
| 50 |
action_desc = python.PythonActionDescriptor('test_action', HelloAction) |
| 51 |
|
| 52 |
# Check descriptor attributes. |
| 53 |
self.assertEqual('test_action', action_desc.name) |
| 54 |
self.assertEqual(HelloAction, action_desc.action_class) |
| 55 |
self.assertEqual( |
| 56 |
HelloAction.__module__ + '.' + HelloAction.__name__, |
| 57 |
action_desc.action_class_name |
| 58 |
) |
| 59 |
self.assertIsNone(action_desc.action_class_attributes) |
| 60 |
self.assertEqual('I help with testing.', action_desc.description) |
| 61 |
self.assertEqual('f_name, l_name', action_desc.params_spec) |
| 62 |
|
| 63 |
# Instantiate the action and check how it works. |
| 64 |
action = action_desc.instantiate( |
| 65 |
{'f_name': 'Jhon', 'l_name': 'Doe'}, |
| 66 |
{} |
| 67 |
) |
| 68 |
|
| 69 |
res = action.run(None) |
| 70 |
|
| 71 |
self.assertEqual('Hello Jhon Doe!', res) |
| 72 |
|
| 73 |
def test_composite_action_provider(self): |
| 74 |
# Check empty provider. |
| 75 |
composite_provider = actions.CompositeActionProvider('test', []) |
| 76 |
|
| 77 |
self.assertEqual(0, len(composite_provider.find_all())) |
| 78 |
|
| 79 |
# Add two test providers. |
| 80 |
provider1 = TestActionProvider('provider1') |
| 81 |
|
| 82 |
action_desc1 = python.PythonActionDescriptor('action1', HelloAction) |
| 83 |
action_desc2 = python.PythonActionDescriptor('action2', HelloAction) |
| 84 |
action_desc3 = python.PythonActionDescriptor('action3', HelloAction) |
| 85 |
action_desc4 = python.PythonActionDescriptor('action4', HelloAction) |
| 86 |
|
| 87 |
provider1.add_action_descriptor(action_desc1) |
| 88 |
provider1.add_action_descriptor(action_desc2) |
| 89 |
|
| 90 |
provider2 = TestActionProvider('provider2') |
| 91 |
|
| 92 |
provider2.add_action_descriptor(action_desc3) |
| 93 |
provider2.add_action_descriptor(action_desc4) |
| 94 |
|
| 95 |
composite_provider = actions.CompositeActionProvider( |
| 96 |
'test', |
| 97 |
[provider1, provider2] |
| 98 |
) |
| 99 |
|
| 100 |
self.assertEqual(4, len(composite_provider.find_all())) |
| 101 |
|
| 102 |
self.assertEqual(action_desc1, composite_provider.find('action1')) |
| 103 |
self.assertEqual(action_desc2, composite_provider.find('action2')) |
| 104 |
self.assertEqual(action_desc3, composite_provider.find('action3')) |
| 105 |
self.assertEqual(action_desc4, composite_provider.find('action4')) |