Exemplo n.º 1
0
    def should_throw_inputs_and_outputs(self):
        flow = base.Flow()
        operations = [base.Flow() for _ in range(3)]
        for op in operations:
            op.throw = Mock()
            flow.operations.append(op)

        destination = base.Node()
        origin = base.Node()

        _input = base.Item()
        output = base.Item()
        resource = base.Resource()
        output.resource = _input.resource = resource

        _input.consume = Mock()
        output.produce = Mock()
        flow.origin = flow.destination = None
        flow.inputs = [_input]
        flow.outputs = [output]

        flow.throw()
        _input.consume.assert_not_called()
        output.produce.assert_not_called()
        for op in operations:
            op.throw.assert_called_with()

        flow.origin = origin
        flow.destination = destination

        flow.throw()
        _input.consume.asseert_called_with(flow)
        output.produce.assert_called_with(flow)
Exemplo n.º 2
0
    def should_consume_tokens(self):
        flow = base.Flow()
        flow.origin = base.Node(key='node_0')
        flow.destination = base.Node(key='node_1')

        self.item.qty = 3
        self.item.produce(flow)

        assert self.item.avalaible_tokens[0].qty == 3
        # 3 units on node_1
        self.session.add(self.item)
        self.session.commit()

        next_flow = base.Flow()
        next_flow.origin = flow.destination
        self.item.qty = 1
        self.item.consume(next_flow)
        self.session.commit()

        assert len(self.item.avalaible_tokens) == 1
        assert self.item.avalaible_tokens[0].qty == 2
        self.item.qty = None
        self.item.consume(next_flow)
        self.session.commit()
        assert len(self.item.avalaible_tokens) == 0
Exemplo n.º 3
0
 def should_raise_exception_when_qty_is_too_large(self):
     consumer = base.Flow()
     try:
         self.token.consume(consumer, 11)
         pytest.fail('StockException should be raised')
     except base.StockException:
         pass
Exemplo n.º 4
0
    def should_create_operations(self):
        path = base.Path()
        steps = [base.Path() for _ in range(3)]
        for step in steps:
            path.steps.append(step)
            step.create_flow = Mock()
            step.create_flow.return_value = base.Flow()
        flow = base.Flow(path=path)
        flow.responsible = base.Node(key='resp')

        calls = []
        for step, operation in zip(steps, flow.op_creator()):
            assert operation == step.create_flow.return_value
            step.create_flow.assert_called_with()
            assert operation.responsible == flow.responsible
            assert operation.parent == flow
Exemplo n.º 5
0
    def should_cancel_flow(self, mock_datetime):
        flow = base.Flow()

        flow.cancel()

        assert flow.finished_on == mock_datetime.now.return_value
        mock_datetime.now.assert_called_with()
        assert flow.state == 'cancelled'
Exemplo n.º 6
0
    def should_persist_basic_relationships(self):
        flow = base.Flow()
        role = base.Node()
        role.key = 'role'
        flow.path = base.Path(role=role)

        responsible = base.Node()
        responsible.key = 'responsible'
        flow.responsible = responsible
        op = base.Flow()
        flow.operations.append(op)
        self.session.add(flow)
        self.session.commit()

        assert op.parent == flow
        assert flow.path.role.key == 'role'
        assert flow.responsible.key == 'responsible'
Exemplo n.º 7
0
 def setup_method(self, method):
     super().setup_method(method)
     resource = base.Resource()
     resource.key = 'resource'
     item = base.Item()
     item.resource = resource
     item.tracking = '123456789'
     node = base.Node()
     flow = base.Flow()
     self.token = base.Token(item=item, node=node, qty=10, producer=flow)
Exemplo n.º 8
0
    def should_execute(self):
        flow = base.Flow()

        flow.execute()

        path = base.Path()
        path.method = Mock()
        flow.path = path
        flow.execute()
        path.method.assert_called_with(flow)
Exemplo n.º 9
0
    def should_close(self):
        flow = base.Flow()
        flow.allocate = Mock()
        flow.throw = Mock()

        flow.finished_on = Mock()
        flow.close()

        flow.allocate.assert_called_with()
        flow.throw.assert_called_with()
Exemplo n.º 10
0
    def should_get_stocks(self):
        flow_1 = base.Flow()
        flow_1.destination = node_0 = base.Node(key='node_0')

        flow_2 = base.Flow()
        flow_2.destination = node_1 = base.Node(key='node_1')

        self.item.qty = 1
        self.item.produce(flow_1)

        self.item.qty = 2
        self.item.produce(flow_2)
        self.session.commit()

        assert len(self.item.avalaible_tokens) == 2

        stocks = self.item.get_stocks()

        assert len(stocks) == 2
        assert node_0 in stocks
        assert node_1 in stocks
        assert stocks[node_1][0].qty + stocks[node_0][0].qty == 3
Exemplo n.º 11
0
    def should_start_(self, mock_datetime):
        dinamic_field = Mock()
        flow = base.Flow()

        flow.start(d_field=dinamic_field)

        assert flow.inputs == []
        assert flow.outputs == []
        assert flow.d_field == dinamic_field
        assert not flow.origin
        assert not flow.destination
        assert flow.state == 'started'
        mock_datetime.now.assert_called_with()
        assert flow.started_on == mock_datetime.now.return_value
Exemplo n.º 12
0
    def should_finish_flow(self, mock_datetime):
        flow = base.Flow()
        flow.state = 'started'

        flow.finish()
        mock_datetime.now.assert_called_with()
        assert flow.finished_on == mock_datetime.now.return_value
        assert flow.state == 'finished'

        flow.state = 'ongoing'
        flow.finish()
        assert flow.state == 'finished'

        flow.state = 'other'
        flow.finish()
        assert flow.state == 'other'
Exemplo n.º 13
0
    def should_runs(self):
        flow = base.Flow()
        flow.start = Mock()
        flow.op_creator = Mock()
        flow.op_creator.return_value = [Mock()]
        flow.execute = Mock()
        flow.finish = Mock()
        flow.close = Mock()
        field = Mock()

        flow.run(field=field)

        flow.start.assert_called_with(field=field)
        flow.op_creator.return_value[0].run.assert_called_with()
        flow.execute.assert_called_with()
        flow.finish.assert_called_with()
        flow.close.assert_called_with()
Exemplo n.º 14
0
    def should_produce_tokens(self):
        flow = base.Flow()
        flow.destination = base.Node(key='destination')
        flow.origin = base.Node(key='origin')

        self.item.qty = 2
        self.item.produce(flow)

        self.session.add(self.item)
        self.session.commit()

        other = self.session.query(base.Item).one()
        assert other.avalaible_tokens
        token = other.avalaible_tokens[0]
        assert token.qty == 2
        assert token.node == flow.destination
        assert token.consumer is None
        assert token.producer == flow
Exemplo n.º 15
0
    def should_consume_all_when_qty_is_none(self):
        flow = base.Flow()
        self.token.consume(flow)
        self.session.commit()

        assert len(self.token.item.avalaible_tokens) == 0
Exemplo n.º 16
0
    def should_split_when_consume_is_less_then_token_qty(self):
        flow = base.Flow()
        self.token.consume(flow, 5)
        self.session.commit()

        assert self.token.item.avalaible_tokens[0].qty == 5
Exemplo n.º 17
0
    def should_allocate_origin_and_destination(self):
        flow = base.Flow()
        operations = [base.Flow() for _ in range(3)]
        for op in operations:
            op.allocate = Mock()
            flow.operations.append(op)

        with pytest.raises(base.FlowAllocateException):
            flow.allocate()

        flow.finished_on = Mock()
        flow.state = 'finished'

        destination = base.Node()
        origin = base.Node()
        from_node = base.Node()
        to_node = base.Node()

        path = base.Path()
        path.from_node = from_node
        path.to_node = to_node

        flow.path = path

        flow.origin = origin
        flow.destination = destination
        flow.allocate()

        for op in operations:
            op.allocate.assert_called_with()

        assert flow.origin == origin
        assert flow.destination == destination

        flow.origin = flow.destination = None
        flow.allocate()

        assert flow.origin == from_node
        assert flow.destination == to_node

        parent_path = base.Path()
        parent_path.from_node = from_node
        parent_path.to_node = to_node
        path.parent = parent_path

        path.from_node = path.to_node = None
        flow.origin = flow.destination = None

        flow.allocate()
        assert flow.origin == from_node
        assert flow.destination == to_node

        flow.state = 'cancelled'
        flow.destination = destination
        flow.origin = origin

        flow.allocate()

        assert flow.destination == origin
        for op in operations:
            assert op.state == 'cancelled'