test_representation.py 4.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2015-2019 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""test representation is well taken into account"""

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "02/04/2020"


import unittest
from pypushflow.representation import Scheme, Node, Link
from pypushflow.Workflow import ProcessableWorkflow


def exec_(scheme, input_=None):
    """
    Simple execution procedure of a workflow.

    :param Scheme scheme:
    :param input_: workflow input if any
    """
    assert isinstance(scheme, ProcessableWorkflow)
    scheme._start_actor.trigger(input_)
    scheme._end_actor.join()
payno's avatar
payno committed
47
    return scheme._end_actor.out_data
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70


class TestScheme(unittest.TestCase):
    """Test the Scheme class"""
    def setUp(self):
        self.node1 = Node(processing_pt='pypushflow.test.utils.test_function')
        self.node2 = Node(processing_pt='pypushflow.test.utils.ProcessingClass1')
        self.node3 = Node(processing_pt='pypushflow.test.utils.ProcessingClass2')
        self.link1 = Link(self.node1, self.node2, 'data', 'data')
        self.link2 = Link(self.node2, self.node3, 'data', 'data')

        self.scheme = Scheme(nodes=(self.node1, self.node2, self.node3),
                             links=(self.link1, self.link2))

    def testSchemeDefinition(self):
        """simple test of scheme definition"""
        self.assertEqual(len(self.scheme.nodes), 3)
        self.assertEqual(len(self.scheme.links), 2)
        self.assertTrue(self.scheme.start_nodes() == [self.node1, ])
        self.assertTrue(self.scheme.final_nodes() == [self.node3, ])

    def testProcessing(self):
        processable_workflow = ProcessableWorkflow(scheme=self.scheme)
71
        channel_out, out_ = exec_(scheme=processable_workflow, input_=(None, {'data': 0,}))
payno's avatar
payno committed
72
73
74
75
76
77
        self.assertEqual(out_['data'], 7)


class TestNodeExecution(unittest.TestCase):
    """Insure Node.execute works"""
    def testCase1(self):
78
        'test that a specific function from her name can be executed'
payno's avatar
payno committed
79
80
81
        node = Node(processing_pt='pypushflow.test.utils.test_function')
        res = node.execute(node.process_pt, properties={}, input_name='data',
                           input_data={'data': 0})
82
        self.assertEqual(res, (None, {'data': 2}))
payno's avatar
payno committed
83
84

    def testCase2(self):
85
        'test that an callable class can be executed from her name'
payno's avatar
payno committed
86
87
88
        node = Node(processing_pt='pypushflow.test.utils.ProcessingClass1')
        res = node.execute(node.process_pt, properties={}, input_name='data',
                           input_data={'data': 0})
89
        self.assertEqual(res, (None, {'data': 4}))
payno's avatar
payno committed
90
91

    def testCase3(self):
92
        """Test that a class with handler can be executed"""
payno's avatar
payno committed
93
94
95
        node = Node(processing_pt='pypushflow.test.utils.ProcessingClass2')
        res = node.execute(node.process_pt, properties={}, input_name='data',
                           input_data={'data': 0})
96
        self.assertEqual(res, (None, {'data': 1}))
97

98
99
100
101
    def testCase4(self):
        script = 'pypushflow.test.pythonActorTest.run'
        node = Node(processing_pt=script)
        res = node.execute(node.process_pt, properties={}, input_name='data',
102
                           input_data={'name': 'pythonActorTest'})
103
        self.assertEqual(res, (None, {'reply': 'Hello pythonActorTest!'}))
104

105
106
107

def suite():
    test_suite = unittest.TestSuite()
payno's avatar
payno committed
108
    for ui in (TestScheme, TestNodeExecution, ):
109
110
111
112
113
114
        test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ui))
    return test_suite


if __name__ == '__main__':
    unittest.main(defaultTest="suite")